在运行时设置区域性时,资源文件的本地化不起作用
本文关键字:源文件 本地化 不起作用 资源 运行时 设置 区域性 | 更新日期: 2023-09-27 18:21:20
我在运行时在代码后面设置页面区域性,如下所示:
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Page.Culture = "fr-FR";
资源文件在GlobalResource文件夹中,我有两个文件:SomeFile.resx
和SomeFile.fr.resx
在标记页面中,我有一个文字如下:
<asp:Literal runat="server" ID="test" Text="<%$ Resources:SomeFile, SomeKey%>" />
如果设置区域性的代码被注释掉,那么文字将取SomeFile.resx
中的值,但如果我运行设置区域性代码,那么我也会得到SomeFile.resx
文件中的值而不是SomeFile.fr.resx
文件中的数值。我还尝试将文化信息设置为"fr",看看这是否有什么不同,但没有。
我需要更改什么?
Microsoft关于定位本地化资源的文章提供了一个很好的例子来说明如何做到这一点。从您所写的内容来看,您似乎没有创建一个新的ResourceManager
对象来使用区域性:
private ResourceManager rm { get; set; }
protected void Page_Load()
{
var newCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
Page.Culture = "fr-FR";
this.rm = new ResourceManager("SomeFile", typeof(Program).Assembly);
this.test.Text = rm.GetString("SomeKey");
}
好吧,对于那些来这里的人,我已经想好了。
1) 将资源文件放在App_GlobalResources
文件夹中。例如,您可能有SomeFile.resx
和SomeFile.fr.resx
2) 在你的代码背后,设置这样的文化:
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Page.Culture = "fr-FR";
3) 然后,要访问这些值,您需要编写以下内容:
string SomeValue = GetGlobalResourceObject("SomeFile", "SomeKey").ToString();
我认为这是最简单的方法。