Using the view localizer 

The ASP.NET Core 3 Framework provides some powerful features for localizing views. You are going to use the view localizer approach in the following example:

  1. Update the ConfigureServices method in the Startup class and add the view localization service to the MVC service declaration:
        services.AddMvc().AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
options => options.ResourcesPath = "Localization");
  1. Modify the Views/ViewImports.cshtml file and add the view localizer functionalities so that they will be available for all the views:
        @using Microsoft.AspNetCore.Mvc.Localization 
        @inject IViewLocalizer Localizer 
  1. Open the home page view and add a new title, which is going to be localized further, as follows:
        <h2>@Localizer["Title"]</h2> 
  1. In the Solution Explorer, go to the Localization folder and create a subfolder called Views. Then, add two new resource files called Home.Index.resx and Home.Index.fr.resx to this folder:

  1. Open the Home.Index.resx file and add an entry for the English title:

  1. Open the Home.Index.fr.resx file and add an entry for the French title:

  1. Start the application and set the user interface language drop-down to English:

  1. Change the application language to French using the User Interface Language Drop-Down. The title should now be displayed in French:

In this section, you've seen how to easily localize your views, but how do you localize forms that are using Data Annotations within your views? Let's look at this in more detail; you will be surprised at what the ASP.NET Core 3 Framework has to offer!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.25.4