gpt4 book ai didi

c# - 全局资源下本土化的噩梦

转载 作者:行者123 更新时间:2023-11-30 16:32:47 25 4
gpt4 key购买 nike

我在 App_GlobalResources 下有两个资源文件

MyApp.resx
MyApp.sv.resx

对于那些不知道的人:所有语言都将回退到 MyApp.resx 除了瑞典的UICulture 将使用MyApp.sv.resx

我有一个显示 3 <asp:Label> 的简单页面在女巫中Text属性的名称不同,例如:

    <i>using Resource.Write:</i><br />
<asp:Label ID="Label1" runat="server" />
<hr />

<i>using HttpContext.GetGlobalResourceObject:</i><br />
<asp:Label ID="Label2" runat="server" />
<hr />

<i>using Text Resources:</i><br />
<asp:Label ID="Label3" runat="server"
Text="<%$ Resources:MyApp, btnRemoveMonitoring %>" />

<p style="margin-top:50px;">
<i>Current UI Culture:</i><br />
<asp:Literal ID="litCulture" runat="server" />
</p>

Label3是页面上唯一调用的,前 2 个设置如下:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = Resources.AdwizaPAR.btnRemoveMonitoring;
Label2.Text = HttpContext.GetGlobalResourceObject("MyApp", "btnRemoveMonitoring").ToString();

litCulture.Text = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
}
}

如果我使用浏览器语言一切正常,但我想覆盖该设置并根据其他输入加载正确的翻译,所以我需要覆盖 UICulture为此,我使用:

protected void Page_Init(object sender, EventArgs e)
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}

女巫与:

protected void Page_Init(object sender, EventArgs e)
{
System.Globalization.CultureInfo cinfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = cinfo;
System.Threading.Thread.CurrentThread.CurrentUICulture = cinfo;
}

有了这一切,我得到的是:

alt text

换句话说 只有使用 code-behind 才能获得正确的本地化信息设置正确的文本,所有inline本地化仅使用浏览器语言。

我错过了什么?

最佳答案

噩梦结束了......

Page_Init 不会改变对全局资源的访问,我们需要覆盖对文化的初始化

protected override void InitializeCulture()
{
//*** make sure to call base class implementation
base.InitializeCulture();

//*** pull language preference from profile
string LanguagePreference = "en-US"; // get from whatever property you want

//*** set the cultures
if (LanguagePreference != null)
{
this.UICulture = LanguagePreference;
this.Culture = LanguagePreference;
}
}

现在一切正常

alt text

关于c# - 全局资源下本土化的噩梦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3812790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com