Skip to content

Instantly share code, notes, and snippets.

@eduardomps
Created July 31, 2016 21:41
Show Gist options
  • Save eduardomps/5b8ce06e22f6fb5a0f4a8778433a9575 to your computer and use it in GitHub Desktop.
Save eduardomps/5b8ce06e22f6fb5a0f4a8778433a9575 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC Localization/ViewLocalization using a shared .resx file
<!--src/Project/Views/Shared/_Layout.cshtml-->
@inject IHtmlLocalizer<SharedResource> Localizer //yes, it also works at the Shared views!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
<!-- snip -->
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<!-- snip -->
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">@Localizer["Home"]</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">@Localizer["About"]</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">@Localizer["Contact"]</a></li>
</ul>
@await Html.PartialAsync("_LoginPartial")
</div>
</div>
</div>
<!--snip-->
</body>
</html>
// src/Projects/Views/_ViewImports.cshtml
//add these two to your ViewImports
@using Project.Resources
@using Microsoft.AspNetCore.Mvc.Localization;
// src/Project/Controllers/HomeController.cs
// using the HomeController as an example
// you don't need it if you only want to localize strings in your views
public IStringLocalizer<SharedResource> _localizer;
// here comes the magic: the DI will inject your localizer
// created by the factory you set up at Startup.ConfigureServices()
public HomeController(IStringLocalizer<SharedResource> localizer)
{
_localizer = localizer;
}
public IActionResult About()
{
// actually using your localizer
// it expects a localized string for the key 'Your application description page.'
// in the current culture of you SharedResource.<culture>.resx
// otherwise it will output the key itself
ViewData["Message"] = _localizer["Your application description page."];
return View();
}
<!--src/Project/Views/Home/Index.cshtml-->
@inject IHtmlLocalizer<SharedResource> Localizer
@{
//add the injection above and then you can use your localizations anywhere on the page
ViewData["Title"] = Localizer["Home Page"];
}
<!-- snip -->
<div class="col-md-3">
<h2>@Localizer["Run & Deploy"]</h2>
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=517851">@Localizer["Run your app"]</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=517853">Run tools such as EF migrations and more</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=398609">Publish to Microsoft Azure Web Apps</a></li>
</ul>
</div>
<!-- snip -->
// src/Project/Resources/SharedResource.cs
namespace Project.Resources
{
//This is a dummy class only, but currently it is required
//It also must be named like your .resx files
//in this case, they would be at Project/Resources/SharedResource.xx-YY.resx
public class SharedResource
{
}
}
// src/Project/Startup.cs
// snipping to the relevant methods
// you shouldn't have to change anything else at Startup
public void ConfigureServices(IServiceCollection services)
{
//add all you want here
//...
//adding the view/localization services
//IMPORTANT: do NOT add 'options.ResourcesPath = 'Resources'
// otherwise it will try to load Project.Resources.Resources.SharedResources
// instead of the correct Project.Resources.SharedResources
services.AddLocalization();
services.AddMvc().AddViewLocalization();
//also important, use this to create your app's IStringLocalizerFactory
services.AddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//etc etc
//your list of supported cultures
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("es"),
new CultureInfo("pt-BR"),
//add more here
};
//setting the options to localize by the HTTP request
app.UseRequestLocalization(new RequestLocalizationOptions
{
// define your default culture
DefaultRequestCulture = new RequestCulture("pt-BR"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment