Multilanguage MVC4 or MVC5 website

So you want to create a multilingual project in MVC4 or MVC5? You are new to ASP.NET MVC and you are looking for a working solution, easy to understand and easy to implement? You are in the right place. This solution has been used for a couple of live websites and it worked quite well. Also the Frontend Developer that worked with me was quite happy with the solution. Read on to understand why.

You will find online a couple of tutorials and approaches. One I liked and followed is Creating a Bilingual ASP.NET MVC 3 Application – Part 2: it offers a working culture detection based on the user browser’s settings and it allows the user to browse the site also by entering in the url -just after the domain name- the language abbreviation (like this: http://www.example.com/en/Home/Index ). The solution in the above tutorial allows the developer to save the translations in .resx files (same principle for LocalResources and GlobalResources).

However, the Cultural View Engine for MVC tutorial (or better: Custom View Engine for Localized Views with ASP.NET MVC Razor – for razor cshtml pages) offers the same browsing routes and the possibility to save the translations directly in the view files, making the editing much more user friendly than editing the .resx files in Visual Studio (which, for long and formatted texts, is not user friendly at all).

When I studied these 2 approaches/tutorials, I decided to mix them, making the developer life much easier and flexible.

Imagine the following scenario:

  • Global.resx file for all the common translations for classic UI labels, buttons, tooltips etc.
  • a default ~/Views folder containing the main views and partial views (e.g. Index.cshtml, About.cshtml, _Header.cshtml, _Footer.cshtml) with localized content taken from the .resx files, like this @Global.FooterText
  • some views containing the content not as variable like @Global.FooterText but copied and pasted there directly e.g. “Copyright Foo Bar 2013”, saved e.g. in Example.cshtml
  • the same views as above, with the same name as above and a language suffix e.g. Example.de.cshtml and content translated directly in that file (in this example, “de” is for German)

Resources

My solution

Download

  • MultilanguageMVC4-2013.10.07a.zip (use this as reference only. Create your own project in Visual Studio 2013 in MVC5 to have all the updated packages and dependencies, bootstrap etc.)
  • Update 2016-05-13 I didn’t check it but you may have a look at Xavier educa02 on github (repository based on this work)

Note: in the Areas you can’t have a controller with the same name of a standard controller (or vice-versa). Every controller must have a unique name.

You may find these 2 helpers quite handy (I use GetLanguage in my controllers when I need to process contents based on the language, in example to build an URL I have to insert in a mail sent to the user, or similar things):

  • CultureManager.GetLanguage()
  • CultureManager.GetCulture()

To use/implement this solution in your own project in your solution you need about 5 Minutes (+ a few more minutes to add new languages or remove the ones you do not want to support). Just follow this simple procedure:

  • copy all the files you find under “Code” and put them in a separate class library (and add a reference to it), or copy the “Code” folder directly in your project.
  • Check the modifications to the global.asax.cs file and apply them to your global.asax.cs file (and update your default route in case it’s not Home/Index).
  • If you want more languages or remove some from the default ones I added (en, de, fr, it) you must edit the CultureManager.cs file. Check the constants at the top and the method InitializeSupportedCultures(). If you need more precision in the culture (in my case I use only the 2 letters ISO abbreviation) you may do some refactoring.

Credits

My solution is based on these 2 tutorials:

I downloaded and updated the first tutorial solution and added the LocalizedViewEngine.cs from the 2nd tutorial. Then updated the global.asax.cs file accordingly.

As some people asked for it, I added also an example with Areas. If you don’t need areas, simply delete the “Areas” folder, save all and re-compile.

Enjoy.

UPDATES

Update 2015-02-19
Seems that this blog post is still helpful. Good news, this solution works of course also with MVC5. Don’t update my demo project to MVC5, start a fresh one and follow the procedure to add multi-language views support.

Update 2013-10-21
Added some more useful information on how to use this solution. Hopefully it should be quite easy to implement this in your solution.

Update 2013-10-07
As some people asked for it, I added an example with Areas. I added an Area called “Admin”, a controller called “Account” and 2 test Views: Index and Test. I added a localized Index.de and for Test I used the .resx resources. To register the new area and make it work with my multi language support, see Areas\Admin\AdminAreaRegistration.cs. Hope this helps.

Update 2013-06-05
I’m not 100% about this, but my solution can lead to caching problems.

Symptom: after changing the language to xx I keep seeing the contents of my main View PageFoo.cshtml and not the contents in PageFoo.xx.cshtml.

Solution: I needed -for my production website- to keep the debug option in the web.config file, more precisely in the tranform web.config.live in Visual Studio I had to keep this (as I use the publish function):

<compilation xdt:Transform="RemoveAttributes(debug)" />

If you don’t have a transform file, then the option in the web.config file has to be:

<compilation debug="true" targetFramework="4.0" />

29 thoughts on “Multilanguage MVC4 or MVC5 website

  1. utkarsh bandekar

    Hi,

    i am new to mvc development and i have used ur example for multilingual support in my website. i am not getting how to redirect from one controller method to another controller method with the lang value in the url..
    E.g. i opened the website…initial url is http://localhost/ ,then i change the language to german.. url becomes http://localhost/de ,now login…after successfull login i want the login controller to redirect the user to product listing… ex url will be http://localhost/de/products… can u help me with this… i.e. when i call redirect(“~/products/index”) it automatically redirects to ~/de/products/index

    Reply
    1. Paolo Brocco Post author

      Hi, I haven’t tested this with areas… I suggest you to download the project and to give it a try… maybe you need to add some code for areas.

      Reply
  2. ZX6R

    Hi,
    Great tutorial!

    In some cases I will need to have a language specific view. How can I load Index.de.cshtml instead of the resource file translation if I wanted to use that instead for German?

    Thanks

    Reply
    1. Paolo Brocco Post author

      Hi ZX6R, if you want to access the Index.de.cshtml you need to browse to /de/YourController.

      If you want for a specific page that the contents are always displayed in German even if the user is browsing in another language, then don’t create Index.de.cshtml and put the German texts directly under Index.cshtml: so that page will be displayed always in German.

      I’m not sure if you wanted to achieve this, but I hope this helps… cheers

      Reply
  3. illich

    Hi! Thanks for You good work. But i have problem with you sample: MultilanguageMVC4-2013.10.07.zip
    After i press [Log in] or [Register] i got error like this:

    Multiple types were found that match the controller named ‘Account’. This can happen if the route that services this request (‘{culture}/{controller}/{action}/{id}’) does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the ‘MapRoute’ method that takes a ‘namespaces’ parameter.

    The request for ‘Account’ has found the following matching controllers:
    MultilanguageMVC4.Areas.Admin.Controllers.AccountController
    MultilanguageMVC4.Controllers.AccountController

    Please help. Im not good in MVC 4.
    Thanks.

    Reply
    1. Paolo Brocco Post author

      Hi, in the AdminAreaRegistration.cs file, at line 21 add a “,” at the end of the line, then in a new line add: new string[] { “MultilanguageMVC4.Areas.Admin.Controllers” }.

      Anyway, even adding the namespace doesn’t solve the issue. You simply can’t use -under areas- the same controller name. Delete the AccountController under Admin, that should solve the problem.

      Reply
  4. johny

    Hy, I have a menu that binds to an XML file , and the property is NAvigateToUrl , like “~/Home/Index”
    on click the culture is loosing … have someone ideas how to avoid this ?

    Reply
  5. FabioG

    Hey,
    I know this is a bit old but i’m using this approach and i was wandering if there’s an easy way to read the .resx values from a .js file?

    Reply
  6. Mike Griffin

    For the life of me I cannot migrate this to MVC 5, get all kinds of ambiguous type errors with the same classes now in multiple namespaces. It’s such a great example, I’d love to use it in MVC 5 but need to see it work first

    Reply
    1. Paolo Brocco Post author

      Dear Mike, I just created an MVC (5) project with Visual Studio 2013 (Visual Studio 2013 Community Edition is downloadable for FREE) and added (as I explained in the procedure) the files in the “Code” folder. I did the necessary changes in the global.asax.cs and it just works fine. Why do you want to upgrade from MVC4 to MVC5? It’s generally a bad idea to do this. Create a fresh new project and set it to use my “solution” (framework) by adding the files you need.

      You know what, I’ll create a new project, add source control and comment every commit so one can see how to do this from scratch… and share on github (so, hopefully some people will contribute).

      Reply
      1. Mike Griffin

        Thanks, I am using the Community Edition as well. I want to start on MVC 5 because it’s the latest version, no sense starting a new project on old code. I’m not going to be using the EF but a different ORM and most of the errors were surrounding the EF and Data.Component.DataAnnotations having the same classes in each of them, couldn’t figure it out

        Reply
  7. Jorge

    I got the code to work locally using MVC5 but it does not work when i publish it to Azure, any ideas why?

    Reply
  8. Xavier

    Hi Paolo, thanks a lot for your post, it helped me develop a similar solution in MVC5. The most interesting thing about your code was the treatment of a specific version of the view for a specific language (such as {ViewName}.{CultureId}.cshtml).

    I wanted to let you know that the deployment in Azure lead to some small problems in this part of the code I was mentioning, although I have solved them (in a sub-optimal way, but not too ugly either). Here is the detail of the issue, which also contains the source code of the MVC5 solution in case you were interested:

    http://stackoverflow.com/questions/35068513/multilingual-website-azure-deployment-different-behavior-than-localhost/35082793#35082793

    Reply
    1. Paolo Brocco Post author

      Sorry for the late reply, but as you can imagine, I’m not actively supporting this anymore. In the meantime did you get it working? Dig a bit, when I created the admin examples I tested them and they should work. If you investigate a bit you may find the issue.

      Reply
  9. denise

    how do you use it to work on traditional chinese? zh-TW ??

    it keeps falling back to english

    Reply
    1. denise

      i figured it out.
      i have to update the CultureManager to not just use the 2 code.

      Our site has zh-CN and zh-TW so this is why i need to enable the 5 characters as well (including the dash)

      Reply
  10. Thanh Nguyen

    Nice Tutorial !

    But, I want to specifi a language default when I run Project in Visual Studio? Help me! Thanks you!

    Reply

Leave a Reply to lluthus Cancel reply

Your email address will not be published. Required fields are marked *