3

我有两个资源文件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是 Page 上唯一调用的,前 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我使用的 and :

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;
}

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

替代文字

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

我错过了什么?

4

2 回答 2

3

噩梦结束了……

Page_Init不改变对全局资源的访问,我们需要对overrideculure 进行初始化

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;
    }
}

现在一切正常

替代文字

于 2010-09-28T12:47:06.333 回答
0

如果不想更改每个页面,可以在 Global.asax 中设置文化

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim lang As String = "en-us"
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub
于 2010-09-28T14:10:29.417 回答