I started some weeks ago to develop a web application using asp.net core 3.0, now I got to the phase where I must develop the localization service, to simplify my code, I will only write the new "parts" so here they are: startup.cs: the ConfigureServices method:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(option => option.EnableEndpointRouting = false)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fr-FR"),
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddTransient<SharedResources>();
the SharedResources.cs
is located in the app root
the Configure
method
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
_ViewImports.cshtml:
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Backoffice.SharedResources> LangDefault
@addTagHelper *, Localisation
a View:
@using Microsoft.AspNetCore.Mvc.Localization
@model some-model
@inject IViewLocalizer Localizer
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">@Localizer["Sede"]</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">@Localizer["FirstName"]</a>
</li>
</ul>
</div>
my app only gets the french translation from Views.[SomeController].[SomeAction].fr.resx
, I couldn't find how to make a select list to change the language, what I find are either for API apps or an old .net core, I found this guide that helped me, but it missed some points, can you provide an adapted guide or some help? thanks
Here is a demo worked:
/Shared/Partial:
_layout.cshtml:
Startup.cs:
ConfigureServices:
Configure:
HomeController:
Resources:
result: