如何在运行时更改CurrentCulture? [英] How to change CurrentCulture at runtime?

查看:198
本文介绍了如何在运行时更改CurrentCulture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时根据每种区域性的资源文件来更改区域性.

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

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

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 

我可以使用后备资源加载页面,或者在pageload上提供UICulture = "he-IL"值,并且可以使用所需资源很好地加载页面.

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 ..在更改按钮单击事件的值后

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

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

它仍然返回到"en-US"的初始值

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

如何在运行时对UICulture进行更改?

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

2 ..如果例如不知道它是"en-US",我如何引用后备资源文件?

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

推荐答案

更改当前的UI文化:

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

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

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

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.

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.

这篇关于如何在运行时更改CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆