I have developed my first ASP.NET 5 web application with this characteristics:
- ASP.NET 5 (Core 1.0)
- MVC 6
- EF 7
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:
- Controllers.NameOfController.ca-ES.resx
- Views.NameOfView.ca-ES.resx
- Models.NameOfModel.ca-ES.resx
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.
- I have read, that I have to develop an controller class that beacome the responsible to manage the localization. I'm right?
- I have read, that exists multiple ways to define the language that I have to display: from the query string, from a cookie, from the Accept-Language HTTP header, from the DefaultRequestCulture property of the RequestLocalizationOptions class, from the thread culture. I don't know if I have to consider all of this options or one is enough.
- .resx are active? Are they the good solution? Can I use it in ASP :NET 5?
- How I can do a selector to commute the language?
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.