【问题标题】:Programmatically change resource file language (resx) in Code Behind以编程方式更改代码隐藏中的资源文件语言 (resx)
【发布时间】:2009-09-09 16:48:13
【问题描述】:

我有一个 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>

请注意,我不关心浏览器语言。它必须超越这一点。我一直在网上搜索此解决方案无济于事。所有示例都显示了以我已经尝试过的方式(如上)切换语言,但这不会影响使用的资源文件。有什么想法吗?

【问题讨论】:

    标签: c# asp.net localization resource-file


    【解决方案1】:

    您必须重写 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");
    }
    

    希望对你有帮助

    【讨论】:

    • MartinB,谢谢您。要么我用错误的术语搜索,要么网上关于这个的信息很少。那工作得很好。我很高兴这将为未来寻找答案的人提供支持。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多