Setup a multilingual site using ASP.NET MVC5 theo http://blog.chudinov.net/setup-a-multilingual-site-using-asp-net-mvc5/

A multilingual site should translate the following:
  • Date and time formatting
  • Currency
  • Text resources: lables, buttons, validation messages, tooltips
It must be easy to switch languages.
It should be relatively easy to add more languages.
An example ASP.NET MVC 5 project can be downloaded here https://github.com/mchudinov/AspMvc5Multilingual
languages_feature_v3_tcm4-833994


multilang

1. Routing

Add lang parameter to routes in RegisterRoutes method of RouteConfig class. Set constraints to enabled languages. Set default language in default route.

2. Activate culture

Culture can be activated by two different ways: in controller’s Initialize method or by using filter attribute.
2.1 Initialize method
Every controller that needs language data must Initialize culture in it’s Initialize method. Let’s create a base controller class and override Initialize method in it:
All the other language-dependent controllers must be inherited from this base controller class.

2.2 Language localization filter attribute

Create a LocalizationAttribute class inherited from ActionFilterAttribute:

Add FilterConfig class to App_Start folder and add created LocalizationAttribute in it.

Use [Localization] attribute on every controller that needs language information.

3. Create translation resources

3.1 Add App_LocalResources folder

Add Asp_Net folder App_LocalResources to project. Resource files will be placed in it.
asplocalresfolder

3.2 Add resource files

Add language resource files GlobalRes.resx for the default language (English in my case) and files for other languages like GlobalRes.ru.resx to App_LocalResources folder. Two letters in the file name must be region info ISO (country code) as defined in RegionInfo.TwoLetterISORegionName Property. The complete list of region codes can be found at the Wikipedia page ISO 3166-1 alpha-2.
addresfile

3.3 Set resource files properties

  • Resource type: String
  • Build Action: Embedded Resource
  • Custom Tool: PublicResXFileCodeGenerator
  • Access Modifier: Public
resfile

3.4 Add translations for all string resources

3.5 Add reference to resource namespace to Razor in web.config

Make sure you have added this namespace to the ~/Views/web.config file and not to the standard ~/Web.config file:

3.6 Use translated resources in code

DisplayAttribute to define translation for a model’s class name:
[Display(Name = "Name", ResourceType = typeof(GlobalRes))]
Use ErrorMessageResourceType and ErrorMessageResourceName for validation messages:
ErrorMessageResourceType = typeof(GlobalRes), ErrorMessageResourceName = "This_field_is_required")
Use GlobalRes.resname references in Razor files:

4. Switch between languages

Use the following helper class that produces language links based on routing. It creates UrlHelper extension method.
Use language links in for instance _Layout.cshtml

5. Format some numbers as another currency then current culture

System.Globalization.NumberFormatInfo class provides culture-specific information for formatting and parsing numeric values. It’s quite easy to use :

6. Format all numbers as another currency then current culture

7. Add new language

  • Add new language code in routing map
  • Add new resource file and translate all strings in it
  • Add new language switch-link to views

  • Sưu tầm tại nguồn: http://blog.chudinov.net/setup-a-multilingual-site-using-asp-net-mvc5/

Nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com

MVC định dạng tiền tệ vnđ - Format currency vnd in MVC