Programmatically change resource file language (resx) in Code Behind

拈花ヽ惹草 提交于 2019-12-20 15:09:08

问题


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?


回答1:


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




回答2:


You might also look into this

http://www.west-wind.com/presentations/wwDbResourceProvider/

I have not used it but I have used other code that Rick has written and it was top notch.



来源:https://stackoverflow.com/questions/1400794/programmatically-change-resource-file-language-resx-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!