How to change CurrentCulture at runtime?

eran otzap picture eran otzap · Aug 9, 2011 · Viewed 73.2k times · Source

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" ?

Answer

maxbeaudoin picture maxbeaudoin · Aug 9, 2011

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.