Question

Simple. I want to localize my application. I've googled for days, and tried a million different approaches to reach my resources. The only way I've been succesful is by using the standard asp.net folders "App_LocalResource", making the resource files public and giving them a Custom Tool Name. In the view I can then import the Custom Tool Name with @using.

My issue is that the language/resource items arent changing when I change the culture.

Here is how I change it in global.asax:

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session != null)
        {
            CultureInfo ci = (CultureInfo)this.Session["Culture"];
            if (ci == null)
            {
                string langName = "en";
                string autoLang = "";
                if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                {
                    autoLang = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
                }
                if (autoLang == "da")
                    langName = autoLang;
                ci = new CultureInfo(langName);
                this.Session["Culture"] = ci;
            }

            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
        }
    }

So the culture is either da or en. But I noticed that the names of the resource files has to have a specific syntax. There has to be a default (in this case english) with no country/culture code and other that default has be named like reFile.da-DK.resx. It has to have both language and culture code.

I'm affraid the resource handler can recognize my file, because culture is set to "da" and not "da-DK". If I name my da resouce file to resFile.da.resx I cant import the Custom Tool Name which is my resouce files.

What do I do to solve this?

Was it helpful?

Solution

Use the full culture info string, ex:

var info = new CultureInfo("en-US")

Also for best practice move the code out into the Application_BeginRequest method, that's the standard location you'll see this type of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top