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

 

  • Sandeep Pawar

    Wow, fantastic article to implement multilingual in MVC 5.
    Hats off to you boss.

    I followed each step as it is and it work as expected.
    No flaws, no errors….

    Thanks for sharing such nice article

  • Catherine

    Today my app that was using this method for switching languages could no longer detect the namespace App_LocalResources. After looking around for a solution I uninstalled VS 2015, installed VS 2017 and remade my project fresh. This fixed the issue but the language switching links no longer work! I think this has to do with VS 2017 updates. If anyone else has this problem or knows the solution it would be appreciated.

  • Language switching links from this blogpost are just normal anchor tags. They can’t stop working because of updates in VS. May be you have got problems with routing. Anchor adds a /{lang}/ language route to URL.
    You do not have to put resource files into App_LocalResources. This is just default location. But recourse files can be placed in any other folder/namespace and even in a separate project or library. Namespace detection is not a VS feature it is a nature of .NET. It can not stop working because of VS updates.

  • Kevin

    This tutorial provided great assistance to my project.. thanks a lot.
    Is it possible to translate the URL links on the browser