Reputation: 667
I have developed my first ASP.NET 5 web application with this characteristics:
I'm trying to translate it to multilanguage and that the user could be able to change the language with a selector, but I'm not able to achieving.
I had developed ASP.NET 2.0/3.5 projects before, and I remember that the texts were into resource (.resx) files or into an XML file. But now, as a lot of things that has changed recently in the new ASP, I think the localization also has changed.
I have found very little information about it, and that I have found, doesn't help me enough.
In the official support website doesn't explain this topic yet (docs.asp.net).
I have found an GitHub code sample, and interesting explanation in this website.
But I'm no be able to linking concepts. I'm going to explain the steps that I have done.
I have created a "Resource" folder, into my ASP.NET project. Into this folder, I have created some (.resx) files. The name of this files follows the structure:
In my Startup.cs file, I have defined the following code to set which folder are the text resources:
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
...
}
In the same file, in the Configure method, I have defined:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//template: "{culture?}/{controller}/{action}/{id?}", //we define the structure of the routes
//defaults: new { culture = "ca", controller = "Home", action = "Index" }); //we define the default values
});
And also in the same function, the supported cultures:
//To set the localization configuration
List<CultureInfo> supportedCultures = new List<CultureInfo>();
supportedCultures.Add(new CultureInfo("ca-ES"));
supportedCultures.Add(new CultureInfo("es-ES"));
List<CultureInfo> supportedUICultures = new List<CultureInfo>();
supportedUICultures.Add(new CultureInfo("ca-ES"));
supportedUICultures.Add(new CultureInfo("es-ES"));
RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions();
requestLocalizationOptions.SupportedCultures = supportedCultures;
requestLocalizationOptions.SupportedUICultures = supportedUICultures;
RequestCulture defaultRequestCulture = new RequestCulture("ca-ES");
app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture);
And now is the point that I'm lost.
EDIT:
I have checked the documentation again, and I have seen in the comments that Rick Anderson has been writing an entry. I haven't read it in calm yet, but I share with you if somebody is interested about this topic.
Upvotes: 3
Views: 4050
Reputation: 1372
I have reticently done an example which demonstrates all aspects of localization in ASP.NET 5 (ASP.NET Core 1.0). You can download it and have a look https://github.com/feradz/ASPNetCoreLocalization/wiki. Download and try it.
Answering your questions:
The example demonstrates the following features:
IStringLocalizer
IHtmlLocalizer
IViewLocalizer
Display
attribute of a model property using prior ASP.NET Core 1.0 resourcesEnumHelper
View.cshtml
View.es-ES.cshtml
CookieRequestCultureProvider
Upvotes: 1
Reputation: 31
In the ConfigureServices(...) put the command
services.AddLocalization(options => options.ResourcesPath = "Resources");
It is very important that the command
app.UseRequestLocalization(...)
must be before all other commands in the method Configure(...). Otherwise localization just not works.
Maybe that will help you.
Upvotes: 0