如何在运行时更改CurrentCulture?

时间:2011-08-09 17:49:46

标签: c# asp.net culture

我需要根据每种文化的资源文件在运行时更改文化。

根据两种文化,我需要更改表单中控件的属性 已指定.resx文件

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

我可以使用回退资源加载页面,或者在pageload上加载UICulture = "he-IL"值,并且可以使用所需资源加载。

问题是我需要在运行时进行这些更改。

1 ..在我更改按钮点击事件的值

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

它仍然返回到“en-US”的初始化值

如何在运行时提交对UICulture的更改?

2 ..如果我不知道它是“en-US”,我如何引用回退资源文件?

3 个答案:

答案 0 :(得分:44)

更改当前的UI文化:

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

或更好,检索he-IL文化的缓存只读实例:

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

在运行时,ASP.NET使用与CurrentUICulture属性设置最匹配的资源文件。线程的UI文化是根据页面的UI文化设置的。例如,如果当前UI文化是西班牙语,则ASP.NET使用WebResources.es.resx文件的编译版本。 如果当前的UI文化不匹配,ASP.NET将使用资源回退。首先搜索特定区域性的资源。如果没有这些,它会搜索中立文化的资源。如果找不到这些,ASP.NET将加载默认资源文件。在此示例中,默认资源文件是WebResource.resx。

答案 1 :(得分:2)

max让我走上了正确的道路,我之前没有遇到任何事情,但它确实帮助我对msdn文档做了一个小调整:

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();
    }   

列表框包含不同的文化,第一个和选定的一个也是默认文化,我保存在页面加载,在其他加载它没有效果,因为列表框已经作为值。

答案 2 :(得分:1)

我无法获得此处所述的“后备”工作。我正在使用全局资源文件的语言,当用户选择的文化文件中缺少标签时,它不会默认返回默认文化中的标签? 我最终创建了一个执行回退的方法。我正在寻找更好的方法来改变文化(当没有找到标签时)并偶然发现这篇文章,所以我想我和其他一些内容。

在我的实用程序类中:  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;
        }
    }