4

ユーザーが複数の言語から選択できるソフトウェアを作成したいと思います。

まず、国際化の扱い方を学びたいと思っています。これまでに国際化を行ったことがないからです。

IDEとして、私はSharpDevelopまたは#developを使用しますが、それを綴ります。現在XAML/WPFも学習しているので、C#とWPFを使用したいと思います。

そこで、ShardDevelopで新しいWPFプロジェクトを作成します。メインウィンドウで、ComboBoxとTextBlockを作成します。

ComboBoxは、「ドイツ語」と「英語」の2つのエントリを取得します。textBlockには「HalloWelt!」と表示されます。選択した言語に応じて、または「HelloWorld!」。

今、私が立ち往生している部分が来ます。各言語はXML/XAMLスタイルの個別のファイルを取得すると思います(意味があります)。これらのファイルはどこにあり、選択した言語のテキストが読み込まれるように、それらとそのコンテンツはどのように読み込まれますか?

いくつかの例を見つけましたが、すべてResource-DLLを作成し、奇妙なプログラムを使用してそれらをcsvファイルに逆コンパイルすることに関するものです...わかりません。もっと簡単な方法はありませんか?


私は次の一歩を踏み出しました。TextBlockのテキストは、「{StaticResourceStrings.MainForm.hwText}」を介してロードされるようになりました。今は次のようになっています。

<TextBlock Text="{StaticResource Strings.MainForm.hwText}" />

また、ドイツ語用に1つ、英語用に1つのResourceDictionaryを作成しました。どちらも、TextBlockで使用するキーを定義しています。

Application.Resourcesパートでは、デフォルトごとにResourceDictionaryの1つをロードします。

現在の問題は次のとおりです。実行時にこの辞書を「アンロード」して、他の辞書に置き換えるにはどうすればよいですか。

もちろん、ComboBoxのSelectionChange-Eventを使用しますが、そこで何をしますか?


問題が解決しました!!kmatyaszekに感謝します

イベントハンドラーのコードを必要に応じて少し変更しましたが、次のようになります。

Uri baseUri = new Uri(AppDomain.CurrentDomain.BaseDirectory);
Uri uri = new Uri(baseUri,"Languages\\lang."+((sender as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString()+".xaml");
if(File.Exists(uri.LocalPath) || File.Exists((uri = new Uri(baseUri,"Languages\\lang.de-DE.xaml")).LocalPath)){
    ResourceDictionary dict = new ResourceDictionary();
    dict.Source = uri;
    this.Resources.MergedDictionaries.Add(dict);
}
4

1 に答える 1

6

2つのResourceDictionaryファイルを作成した場合は、でバインドできますDynamicResource

例:

最初のリソースファイル(Lang.en-US.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="Username">Username:</system:String>
    <system:String x:Key="Password">Password:</system:String>
    <system:String x:Key="close">Close</system:String>
    <system:String x:Key="login">Login</system:String>        
</ResourceDictionary>

2番目のリソースファイル(Lang.pl-PL.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="Username">Login:</system:String>
    <system:String x:Key="Password">Hasło:</system:String>
    <system:String x:Key="close">Zamknij</system:String>
    <system:String x:Key="login">Zaloguj</system:String>
</ResourceDictionary>

アプリケーションリソースでデフォルト言語を設定します。

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Lang.en-US.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
 </Application.Resources>

以下のようなComboBoxがあるとしましょう。

<ComboBox Name="cbLang" Margin="2" SelectionChanged="cbLang_SelectionChanged" >
                <ComboBoxItem Content="English" Tag="en-US" />
                <ComboBoxItem Content="Polish" Tag="pl-PL" />
  </ComboBox>

コードビハインドSelectionChanged:

 private void cbLang_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            switch (((sender as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString())
            {
                case "en-US":
                    dict.Source = new Uri("Lang.en-US.xaml", UriKind.Relative);
                    break;
                case "pl-PL":
                    dict.Source = new Uri("Lang.pl-PL.xaml", UriKind.Relative);
                    break;
                default:
                    break;
            }
            this.Resources.MergedDictionaries.Add(dict);
        }

そして、あなたはこのようにバインドすることができます:

 <TextBlock Text="{DynamicResource Username}" VerticalAlignment="Center" />
于 2012-12-17T20:51:56.987 に答える