Вопрос

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

Это было полезно?

Решение

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" />

Другие советы

In addition to what terjetyl mentioned, in order to be able to change the culture, you need to add additional functionality to your controllers.

First, you need to create the following class (you can place it on the Controllers folder):

public class BaseController : Controller
{
    protected override void ExecuteCore()
    {
        string cultureName = null;

        // Attempt to read the culture cookie from Request
        HttpCookie cultureCookie = Request.Cookies["_culture"];

        // If there is a cookie already with the language, use the value for the translation, else uses the default language configured.
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
        {
            cultureName = ConfigurationManager.AppSettings["DefaultCultureName"];

            cultureCookie = new HttpCookie("_culture");
            cultureCookie.HttpOnly = false; // Not accessible by JS.
            cultureCookie.Expires = DateTime.Now.AddYears(1);
        }

        // Validates the culture name.
        cultureName = CultureHelper.GetImplementedCulture(cultureName); 

        // Sets the new language to the cookie.
        cultureCookie.Value = cultureName;

        // Sets the cookie on the response.
        Response.Cookies.Add(cultureCookie);

        // Modify current thread's cultures            
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        base.ExecuteCore();
    }
}

Then, you need to make every controller on your MVC project to inherit from the created class.

After this, you need to add the following code on namespaces tag on the Web.config on the Views folder.

<add namespace="complete assembly name of the resources project"/>

Finally, you need to add on the button where you change the language, the instructions to set the "_culture" cookie to the correct language code.

Let me know if you have any questions.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top