开发者

How to change CurrentCulture at runtime?

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.asp开发者_Go百科x.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" ?


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.


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 .


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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜