Join Chris Woodruff for an in-depth discussion in this video Localization based on URL request string, part of ASP.NET Core: Internationalization.
- [Instructor] Now that we have our ASP.NET Core web project let's add the globalization to it. This demo will be very simple, but we will see the details of adding the localization service, and giving it a set of cultures that the application can use. Before we can get into the code, I do need to explain the order of the localization library's work to find the culture to use. There are three methods that your localized app will use to find its culture and they override the ones before it in the list.
The first is by sending a culture on the request URL. That's what we're going to be looking at in this video. The second method is using a cookie and that will come in a later video. The last way, if you don't use a request URL or a cookie, we're going to use the culture that is set in the browser itself and we will get that information through the Accept-language HTTP header. Each of the methods has its own provider that will allow for that method to happen.
Let's look at how to localize our web application. To start, I'm going to to add the following namespaces to the start.cs file. I'm going to to be adding System.Globalization and Microsoft.AspNetCore.Localization.
This gives me the ability to work with the localization libraries of asp.net core. Next, I need to let the application know that I'm going to be using localization. I need to add it to the app object in the configure method. I'm going to use the UseRequestLocalization method that takes a single parameter with the options needed.
These options will be encapsulated in a request localization options object that we will create and pass back from a helper method called BuildLocalizationOptions. Let's take a look at this method in detail. It's going to be private. And we'll have a return type of RequestLocalizationOptions.
And again, the name is BuildLocalizationOptions. The first thing that we need to do is build a list with our supported cultures. I'm going to be adding a number of languages into this list.
As you can see I've added five cultures to this list of supported cultures. US English, Spanish, French, German and Swedish. I'm going to take those supported cultures and use them to create the options object. The request localization options takes three parameters. A default request culture, supported cultures and supported UI cultures.
For the default culture, I'm going to use US English. For the supported cultures, I'm going to use the list supportedCultures that we created previously. And for the supported UI cultures, I'm also going to use that same list. I'm going to return that options back as the return value of this method.
And I'm going to use that method as the parameter for the applications' UseRequestLocalization method. Let's make sure this builds. Now instead of sending back hello world in response to requests made to this web application, I'm going to create some content that returns back some details about the current culture that the application is working with.
I'm going to use a context given in the run method for the application. I'm going to give the status code for the context as a 200 return value. And I'm also going to set the response content type to text, HTML, so instead of hello world I'm going to build a new helper method called BuildResponse that will build an HTML segment that we can return back with the response detailing the information about the current culture.
So, this again, is another private method, it's going to return back a string and again, it's called BuildResponse. I'm going to start out with two variables. The first will hold the culture name and the second will hold the UI culture name. So the current culture name variable will be the English name of the current culture that's set in the application.
The current UI culture name will again come from the applications' CultureInfo. It will get the English name for the current UI culture. Some of the details for the current culture that we're going to return back in this HTML table is the current culture name, current UI culture name, a date time formatted value, a number formatted value and a currency formatted value.
And all those will be dependent on the current culture of the application. Let's, again, see if this builds. At this point I need to save the code that we just typed in. Now let's run the application and see how it works. As you can see in the browser, we have the current culture details of our application.
So remember, US English is the default culture of our application, so if we don't give it the culture we're going to get US English showing up, which is exactly what we have. Now let's try using the German culture in the URL request. What I've shown here is how to set the culture in the request URL. I give it a ?culture = and for German, de-DE.
As you can see in the content of the browser, the details about the current culture in our application have changed to German. We also see that the current date, the formatted number and the currency value all reflect German culture formats. So what happens if I give a language to the application that is not part of its supported cultures? Let's take an example, Italian.
As you can see, the current culture has reverted back to United States English. Why is that? Well, because Italian was not in our list of supported cultures, the application falls back to its default culture, which for this application we set up as United States English. So, really, looking at the code to make an application localized and also looking at the request URL, none of this is really tough.
I want to take it one step further and we're going to move on and go to more localization demos that involve the http-headers and cookies.
- Globalization and localization
- Internationalization considerations
- Creating the ASP.NET Core project
- Using the HTTP Accept-Language header
- Using the ASP.NET Core store project
- Displaying content with the controller
- Modifying the views
- Selecting a culture with cookies
- Systems of measurement formats