1

カレンダー、通貨値を保持するラベル、挨拶するラベルを含む Web ページがあります。ドロップダウンから言語を選択すると、通貨ラベル、カレンダーが変更されますが、hello は変更されません。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);  
    }
}
4

2 に答える 2

1

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

ローカライズするテキストを手動で翻訳し、これらの文字列を言語固有のリソース ファイルにコンパイルする必要があります。これは、 System.ResourcesのResourceManagerオブジェクトのGetStringメソッドを介してアクセスできます。

// 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;
于 2009-03-04T12:22:28.517 に答える
1

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

さらなるアドバイス...次のようなコードは避けてください。

string language = Request["ddlLanguages"];

これは良くありません... これは Request オブジェクト機能の副作用としてのみ機能し、このコードをコンテンツ ページなどのネーミング コンテナーに配置するとすぐに壊れます。代わりにこれを行います:

string language = ddlLanguages.SelectedValue;
于 2009-03-03T21:27:51.977 に答える