5
To configure the languages in Asp.net core I used the following configuration:
In the Startup file
public void ConfigureServices(IServiceCollection services) {
services.AddLocalization(opts => {
opts.ResourcesPath = "Resources";
});
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.SubFolder,
opts => {
opts.ResourcesPath = "Resources";
})
.AddDataAnnotationsLocalization()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
I put the translation files here as shown in the image
To access the translation in the controller I used
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
Then to access a specific text I used
ViewData["Contact"] = _localizer.GetString("Contact").Value;
Step into the ViewData["Contact"]
the field with the name Contact which are in the files:
- Controllers.HomeController.en.resx
- Controllers.HomeController.en-PT.resx
These files in your order:
- Views.Shared. _Layout.en-PT.resx
- Views.Shared. _Layout.en.resx
Affect the file _Layout.cshtml
Within the _Layout.cshtml file we can translate into that file without existing controller we use Razor
@inject IViewLocalizer Localizer
And to put a translation variable we use for example:
@Localizer["MenuHome"]
"Menuhome" is the name of the variable declared in the file Views.Shared. _Layout.en.resx and Views.Shared. _Layout.en-PT.resx
Then to see the pages in the different languages just access the link of your site:
https://linkdosite/Controller/Nomination? Culture=en
To show the translation in this case in English
With full namespace does not solve? Ex.: "adopts me.shared.Languages"?
– Leandro Angelo