Search code examples
c#asp.net-mvclocalizationglobalizationmultilingual

Multilanguage application with MVC 4


I'm having a little bit of trouble doing this multilanguage MVC 4 web application, I've looked everywhere but, I haven't found exactly what I wanted.

What I want is: I have my solution divided by 4 project, among them, there's the web MVC 4 project (the main project) and a Resource project, where I created 2 resource files(en-US.resx and pt-BR.resx) I can assing easily a viewBag.title, for example with a pt-BR resource text on a view, like so:

      @using Resources
      @{
           ViewBag.Title = pt_BR.HomeTitle;
      }

The only thing I want to know is how can I store the resource file (pt_BR and en_US) in something and the text will be transformed, like this

      var culture = Resources.en_US; //or var culture = Resources.pt_BR;

and then

       @using Resources
       @{
           ViewBag.Title = culture.HomeTitle;
       }

and then I'll use a resource string from the file the I've selected in the beginning of the application


Solution

  • What you could do is create a Home.resx file for the english texts and a Home.pt-BR.resx file for the portuguese text and then you access them like this

       @{
           ViewBag.Title = Resources.Home.Title;
       }
    

    The Culture of your thread will choose the correct file. You can set the thread culture manually in web.config ex.

    <globalization uiCulture="pt-BR" culture="pt-BR" />