C# 在代码隐藏中以编程方式更改资源文件语言 (resx)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1400794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically change resource file language (resx) in Code Behind
提问by Sherri
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. I want to do this in the code behind file (MyPage.aspx.cs).
我正在尝试以编程方式更改告诉应用程序要使用哪个 resx 文件的语言。我想在代码隐藏文件 (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?
请注意,我不关心浏览器语言。它必须超越这一点。我一直在网上搜索此解决方案无济于事。所有示例都显示以我已经尝试过的方式(以上)切换语言,但这不会影响使用的资源文件。有任何想法吗?
采纳答案by mberube.Net
You must override the InitializeCulture method and put your code there. Ex:
您必须覆盖 InitializeCulture 方法并将您的代码放在那里。前任:
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
希望这可以帮助
回答by Frank
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 编写的其他代码,它是一流的。