Reputation: 41
I am currently trying to wrap my head around how to make a website available in multiple languages but I don't really know where to start. I have read up on the documentation of microsoft -->
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0
I've also followed this stackoverflow thread and am currently on step 5 -->
How to get the .resx file strings in asp.net core
I have currently made a .resx file called "Layout.nl.resx" which should recognize the name of Layout.cshtml. I also have made this in my startup.cs to Localize the path and to configure which Culture info's I will be using.
using System.Globalization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Localization;
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("nl-NL"),
new CultureInfo("en-US"),
new CultureInfo("dk-DK"),
new CultureInfo("de-DE")
};
options.DefaultRequestCulture = new RequestCulture("nl-NL", "nl-NL");
//Formatting Numers, Dates, etc.
options.SupportedCultures = supportedCultures;
//UI strings that we have localized
options.SupportedUICultures = supportedCultures;
});
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<JagerUser> userManager, RoleManager<IdentityRole> roleManager)
{
var supporterCultures = new[] { "nl-NL", "en-US", "dk-DK", "de-DE" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supporterCultures[0])
.AddSupportedCultures(supporterCultures)
.AddSupportedUICultures(supporterCultures);
app.UseRequestLocalization(localizationOptions);
Console.WriteLine("The current Culture Language is: " + CultureInfo.CurrentCulture + "The Thread CultureInfo is: " + Thread.CurrentThread.CurrentCulture);
}
Right now my goal is to change the value of my Headers in Layout.cshtml from 'Products' and 'My Orders' to 'Producten' and 'Mijn Orders' which have been described in the Layout.nl.resx file that is located in the folder 'Resources'.
HomeController
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) });
Console.WriteLine("The new CultureInfo is now: " + CultureInfo.CurrentCulture);
//this will display on the console: The new CultureInfo is now: en-US
return LocalRedirect(returnUrl);
}
@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options
@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions
@{
var requestCulture = Context.Features.Get<IRequestCultureFeature>();
var cultureItems = LocOptions.Value.SupportedUICultures
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
.ToList();
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider.GetType().Name">
<form id="selectLangue" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
method="post" class="form-horizontal" role="form">
<label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>
<select name="culture" onchange="this.form.submit();" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
</select>
</form>
</div>
My question is: how can I access this information from the .resx file, where do I have to initialize the IStringLocalizer<> and to change the Values that are in Layout.cshtml
Update
I have added a dropdown list which now creates a cookie and sets the CultureInfo to the selected language which can be seen in above codefield. My current issue is getting access or being able to use the resource file that I have made. It is called 'Views.Home.Index.nl-NL.resx. because it has to match the name of the view in the folder name (I think).
Upvotes: 1
Views: 3387
Reputation: 41
Okay so after a lot of reading and asking around I found the answer on how to use the resource files.
I have done most of the setup already.
You define the types of culture in your services.Configure()
, you use the serivices.AddLocalization()
to define which path you want your resources to be found in and you use services.AddMvc().AddViewLocalization()
to specify your view format.
then under the public void Configure()
method you will define the supported types of languages you will be looking for. aka Index.en-US.resc. you're using app.UseRequestLocalization()
to add the RequestLocalizationMiddleware which will set the culture based on the information your client is giving you.
Now.. the important part is to understand how you can have access to your resource files is to understand how the pathfinding is setup. in Resource file naming they talk about the path Resources/View/Home/Index.fr.resc In your Resources folder you will have to map a structure just like your normal views are placed in a hierarchy Views -> into Home -> into the Index.cshtml file.
Once that is setup you can then make your content localizable which can be found under Make the app's content localizable and View localization of the documentation :
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0 .
Upvotes: 0