简体   繁体   English

以编程方式更改Code Behind中的资源文件语言(resx)

[英]Programmatically change resource file language (resx) in Code Behind

I have a .Net application in C# and I have a file structure something like: 我在C#中有一个.Net应用程序,我有一个类似的文件结构:

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. 我试图以编程方式更改语言,告诉应用程序使用哪个resx文件。 I want to do this in the code behind file (MyPage.aspx.cs). 我想在代码隐藏文件(MyPage.aspx.cs)中执行此操作。

I have tried both of these in the OnPreRender, Page_Init, and Page_Load events: 我在OnPreRender,Page_Init和Page_Load事件中尝试了这两种方法:

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: MyPage.aspx文件包含:

<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? 有任何想法吗?

You must override the InitializeCulture method and put your code there. 您必须覆盖InitializeCulture方法并将代码放在那里。 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 希望这可以帮助

You might also look into this 你也可以看看这个

http://www.west-wind.com/presentations/wwDbResourceProvider/ 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. 我没有使用它,但我使用了Rick编写的其他代码,它是顶级的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM