Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change resource file language (resx) in Code Behind

I have a .Net application in C# and I have a file structure something like:

App_LocalResources
 - MyPage.aspx.resx
 - MyPage.aspx.fr.resx
MyPage.aspx
MyPage.aspx.cs

I am trying to programatically change the language which tells the application which resx file to use. I want to do this in the code behind file (MyPage.aspx.cs).

I have tried both of these in the OnPreRender, Page_Init, and Page_Load events:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");

and it does not work. The page still shows the english language. The MyPage.aspx file has this:

<h3><asp:Literal runat="server" Text="<%$ Resources:pageTitle %>" /></h3>

Note that I cannot care about the browser language. It must over-ride this. I have been searching the web for this solution to no avail. All examples show switching the language the way I have already tried (above) however this does not affect the resource file used. Any ideas?

like image 713
Sherri Avatar asked Sep 09 '09 16:09

Sherri


People also ask

How do I update RESX files?

To edit RESX files, you could use an advanced text editor like XML Editor, but note that simple text editors can corrupt your files. A better way is to use Localazy, crafted with translation file formats in mind.

How do you change the namespace of a RESX file?

The quick answer is: Just open the Properties of the resource file and change the Custom Tool Namespace to the namespace you need. Simple as that. Had a . resx file in <repo_name>/Properties directory, and wanted it to be in my main namespace: <repo_name>.

What is Resx in C#?

resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.


1 Answers

You must override the InitializeCulture method and put your code there. Ex:

protected override void InitializeCulture()
{
   base.InitializeCulture();
   System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
   System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
}

Hope this helps

like image 162
mberube.Net Avatar answered Oct 10 '22 16:10

mberube.Net