Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c# - 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.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" ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...