简体   繁体   中英

MVC and Globalization

I have some code that when I step through changes the CurrentCulture and CurrentUICulture , but does not take the relevant string from .resx file, it just takes the one from the default culture. Can anybody help, this is my code :

<map id="langs" name="langselect">
            <area shape="rect" coords="0,0,18,11" alt="en-GB" onmouseover="tooltip.show('en-GB', 60);" onmouseout="tooltip.hide();" onclick="setCulture('en-GB');" />
            <area shape="rect" coords="25,1,41,11" alt="fi-FI" onmouseover="tooltip.show('fi-FI', 48);" onmouseout="tooltip.hide();" onclick="setCulture('fi-FI');"  />
        </map>

function setCulture(culture) {

    $.post('/Gameplan/MainGP/SetCulture', { culture: culture }, function (result) {

    });

}

public void SetCulture(string culture)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

    }

EDIT : I have added the following code, but I still have the same issue :

public void SetCulture(string culture)
    {
        Session["Culture"] = culture;
        Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Culture"].ToString());
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["Culture"].ToString());
    }

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Culture"].ToString());
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["Culture"].ToString());
    }

You need to save the culture somewhere (cookie/session/whatever) and apply it on every request.

In ASP.Net MVC in the global.asax AquireRequestState seems to be an OK place to put the code to apply the culture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM