3

下に 2 つの Resources ファイルがありますApp_GlobalResources

MyApp.resx
MyApp.sv.resx

知らない人のために:スウェーデンの UICultureをMyApp.resx 除くすべての言語がフォールバックします。MyApp.sv.resx

そして、私はプロパティが次のように異なる方法で呼び出される<asp:Label>魔女の3を示す簡単なページを持っています: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ページで呼び出されるのは 1 つだけで、最初の 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";
}

witch は次と同じです。

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グローバルリソースへのアクセスを変更しないため、文化へoverrideの初期化が必要です

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 に答える