通貨、カレンダーは選択した言語に変更されますが、asp.netのラベルは変更されませんか?

StackOverflow https://stackoverflow.com/questions/608226

  •  03-07-2019
  •  | 
  •  

質問

カレンダー、通貨の値を保持するラベル、および挨拶のラベルが付いたWebページがあります。ドロップダウンから言語を選択すると、通貨ラベル、カレンダーが変更されますが、こんにちはは変更されません。 aspxページとcsファイルのコードを削除しました:

ASPX:

<asp:Label ID="lblLanguageSelection" runat="server" 
           Text="Select a language: "></asp:Label>
    <asp:DropDownList ID="ddlLanguages" runat="server" AutoPostBack="true">
    <asp:ListItem Value="auto">Auto</asp:ListItem>
    <asp:ListItem Value="en-US">English (US)</asp:ListItem>
    <asp:ListItem Value="en-GB">English (GB)</asp:ListItem>
    <asp:ListItem Value="de">German</asp:ListItem>
    <asp:ListItem Value="fr">French</asp:ListItem>
    <asp:ListItem Value="fr-CA">French (Canada)</asp:ListItem>
    <asp:ListItem Value="hi">Hindi</asp:ListItem>
    <asp:ListItem Value="th">Thai</asp:ListItem>
    </asp:DropDownList>
    <br /><br />
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    <br /><br />
    <asp:Label ID="lblCurrency" runat="server"></asp:Label>
    <br /><br />
    <asp:Label ID="lblHello" runat="server"></asp:Label>

CS:

protected void Page_Load(object sender, EventArgs e)
{
    decimal currency = 65542.43M;
    string hello = "Hello";

    lblCurrency.Text = string.Format("{0:c}", currency);
    lblHello.Text = string.Format("{0}",hello);
}

protected override void InitializeCulture()
{
    string language = Request["ddlLanguages"];

    if (language != null)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        Thread.CurrentThread.CurrentCulture = 
                             CultureInfo.CreateSpecificCulture(language);  
    }
}
役に立ちましたか?

解決

ラベルをローカライズする場合は、ローカライズされたリソースファイルを使用して文字列を調べる必要があります(これは、「文字列リテラルを使用しない」全体のベストプラクティスの由来です。

ローカライズするテキストを手動で翻訳し、これらの文字列を言語固有のリソースファイルにコンパイルし、 メソッドus / library / system.resources.resourcemanager.aspx "rel =" nofollow noreferrer "> オブジェクト.aspx "rel =" nofollow noreferrer "> System.Resources

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", 
        Assembly.GetExecutingAssembly());

// Retrieve the value of the string resource named "hello".
// The resource manager will retrieve the value of the  
// localized resource using the caller's current culture setting.
String hello = rm.GetString("hello");
lblHello.Text = hello;

他のヒント

えー...正確に何が起こると思っていますか?通貨と日付には、ロケールに基づいた組み込みの形式があります。 ASP.NETで言語翻訳を行いたいですか?!?申し訳ありませんが、あなたはその1つに運がありません。 :)私はあなたの意図を逃していますか?

その他のアドバイス...このようなコードは避けてください:

string language = Request["ddlLanguages"];

これは良くありません...これはリクエストオブジェクト機能の副作用としてのみ機能し、このコードをコンテンツページなどの名前付けコンテナに入れるとすぐに壊れます。代わりにこれを行います:

string language = ddlLanguages.SelectedValue;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top