以编程方式更改代码隐藏中的资源文件语言 (resx)
我有一个 C# 中的 .Net 应用程序,并且有一个类似于以下内容的文件结构:
App_LocalResources
- MyPage.aspx.resx
- MyPage.aspx.fr.resx
MyPage.aspx
MyPage.aspx.cs
我正在尝试以编程方式更改语言,告诉应用程序要使用哪个 resx 文件。我想在代码隐藏文件(MyPage.aspx.cs)中执行此操作。
我已经在 OnPreRender、Page_Init 和 Page_Load 事件中尝试过这两种方法:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
但它不起作用。页面仍然显示英文。 MyPage.aspx 文件具有以下内容:
<h3><asp:Literal runat="server" Text="<%$ Resources:pageTitle %>" /></h3>
请注意,我不关心浏览器语言。它必须超越这一点。我一直在网上搜索此解决方案但无济于事。所有示例都显示按照我已经尝试过的方式(上面)切换语言,但这不会影响所使用的资源文件。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须重写 InitializeCulture 方法并将代码放在那里。例如:
希望这有帮助
You must override the InitializeCulture method and put your code there. Ex:
Hope this helps