InitializeCulture() on every single page necessary?

我们两清 提交于 2019-12-10 00:00:55

问题


I have a web forms site that needs to be localized. I mean, it is localized, I just need to set the right language according to the domain. Something like:

protected override void InitializeCulture()
{
    var i = Request.Url.Host.ToLower();
    var domain = i.Substring(i.Length - 2, 2);
    if (domain == "se")
    {
        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture("sv-SE");
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo("sv-SE");
    }
    else if (domain == "dk")
    {
        Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("da-DK");
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo("da-DK");
    }
}

My first question is: Do I really have to call InitializeCulture() on every single page for the right resources the be loaded?

Second question. I also have some global resources. If yes to first question, will they be set correctly as well?

Ps. uiCulture="auto" and enableClientBasedCulture="true" in webconfig will not be sufficient (long story).


回答1:


Yes. What you are doing complies with the Microsoft-recommended method. And since your example determines what language to use based on the URL, then each page request may require a different language, so you just couldn't avoid doing this for each individual page.

As per your second question, yes, all resource loading depends on CurrentCulture. So both local and global resources will be affected by your culture initialization.




回答2:


If the language in the application does not differ on a page to page basis then it make sense to reuse the code as answered here. Or better still use http module explained here.



来源:https://stackoverflow.com/questions/9700603/initializeculture-on-every-single-page-necessary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!