InitializeCulture() on every single page necessary

2019-04-16 17:39发布

问题:

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.