How to change CurrentCulture at runtime?
Asked Answered
S

3

28

I need to change cultures at runtime according to resource files for each culture.

I need to change the attributes of the controls in my form, according to two cultures which have designated .resx files

resorces1.aspx.resx // default 
resorces1.aspx.he-IL.resx // hebrew culture 

I can load the page either with the fallback resource, or on pageload give the UICulture = "he-IL" value and it loads fine with the wanted resources.

The problem is I need to make these changes at runtime.

1.. after I change the value on a button click event

    btn_change_Click(....)
    {
        UICulture = "he-IL" ;
    }

It still returns to the initialized value of "en-US"

How can I commit a change to the UICulture at runtime ?

2.. how can i reference the fallback resource file if for instance i don't know it's "en-US" ?

Stalky answered 9/8, 2011 at 17:49 Comment(0)
D
68

Changing the current UI culture:

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("he-IL");

or better, retrieve a cached read-only instance of the he-IL culture:

System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he-IL");

At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property. The UI culture for the thread is set according to the UI culture of the page. For example, if the current UI culture is Spanish, ASP.NET uses the compiled version of the WebResources.es.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the default resource file. In this example, the default resource file is WebResource.resx.

Dhaulagiri answered 9/8, 2011 at 17:56 Comment(5)
ok so lets say i changed the CurrentUICulture how would i change it back the the default culture assuming i do not know what it was ?Stalky
The default culture is the fallback culture right? It is known at compile-time right? It's resorces1.aspx.resx's culture, probably en-US.. Unless I misunderstood your second question it seems fairly easy to do considering that you now know how to change the culture at runtime :)Dhaulagiri
yes it is , ill just save it's name when i load the page . i didn't think before thanks .Stalky
HI I have a doubt here. If I change the culture on every new Thread like below:------ Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture); Does it reloads the resource Files every time?Olaolaf
Works also for WinForms!W
S
2

max set me on the right path , nothing i haven't come across before , but it did help me make a minor adjustment to the msdn documentation on the matter :

http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

    string defaultLanguage = Thread.CurrentThread.CurrentUICulture.ToString();                         
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void InitializeCulture()
    {
        if (Request.Form["ListBox1"] != null)
        {
            String selectedLanguage = Request.Form["ListBox1"];
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture = new CultureInfo(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultLanguage);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(defaultLanguage);
        }            
        base.InitializeCulture();
    }   

the list box contains different cultures the first and selected one is also the default culture , which i save on the page load , on other loads it as no effect because the listbox already as a value .

Stalky answered 10/8, 2011 at 7:5 Comment(0)
P
1

I have not been able to get the "fallback" as described here to work. I'm using Global resource files for language and when the label is missing from the user selected culture file it does not default back to a label in default culture? I ended up creating a method to perform the fallback. I was searching for better ways to temp change the culture (when label not found) and stumbled on this post so I thought I'd and some content.

In a utility class of mine: public String getLabelResource(String sLabelID, String sLangCd) {

        cLogger oLogger = new cLogger();

        try
        {
            Object sLabel;
            sLabel = HttpContext.GetGlobalResourceObject("{filename}", sLabelID);
            if (sLabel.ToString() == "") //label was not found in selected lang
            {
                //default to US language resource label
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
                sLabel = HttpContext.GetGlobalResourceObject("{filename}", sLabelID);
                //switch global lang back to selected
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLangCd);
            }
            return sLabel.ToString();

        }
        catch (Exception ex)
        {
            oLogger.LogWrite("cUtils.cs", "getLabelResource", ex.Message, false);
            return String.Empty;
        }
    }
Paraguay answered 15/8, 2013 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.