質問

WPFアプリに多言語システムを実装する良い方法をお勧めしますか?現在使用しているメソッドには、XML、クラス、およびxaml拡張が含まれます。ほとんどの場合問題なく動作しますが、動的ラベルまたは動的テキストを一般的に処理する必要がある場合は、さらに手間がかかります。プログラマーが主な問題でのみ作業し、言語の問題を忘れてしまうようにしたいと思います。

役に立ちましたか?

解決

WPFローカリゼーション拡張機能を使用しています。これは、 DependencyObject sのあらゆるタイプの DependencyProperty をローカライズする非常に簡単な方法です。

  • 実際の安定状態
  • Text = {LocText ResAssembly:ResFile:ResKey} などのバインディングのような書き込みスタイルをサポート
  • .resx-fallbackメカニズムで動作します(たとえば、en-us-> en->独立カルチャ)
  • 文化の強制をサポートします(例:「これは常に英語である必要があります」)
  • 通常の依存関係プロパティで動作します
  • コントロールテンプレートで動作します
  • 追加の名前空間なしでXAMLで使用できます(実際には:P)
  • コードビハインドで使用して、ローカライズされた値を動的に生成されたコントロールにバインドすることができます
  • 高度な使用のための INotifyPropertyChanged の実装
  • 文字列フォーマットをサポートします。 "これは「{0}」値です
  • プレフィックスとサフィックスの値をサポート(現在は LocText 拡張機能を使用)
  • 生産システムで使用されています(私の広報製品など)
  • 言語をランタイムに切り替えると、 NO タイムスライスに影響します
  • すべてのアセンブリ(実行時に動的にロードされるものも含む)でリソースファイル( .resx )とともに使用できます
  • 初期化プロセスは必要ありません(「xyzを呼び出して特別なローカライズ辞書を登録する」など)
  • デザインタイムで利用可能(MS Expression Blend、MS Visual Studio 2008(NormalおよびSP1)
  • 設計時に選択した言語の変更が可能です
  • コンバーター( TypeConverter )が存在する限り、任意のタイプのデータ型をローカライズできます( LocalizeExtension を拡張)
  • Text 、上の Text 、下の Text Image s、のサポートが組み込まれていますブラシ es、 Double および Thickness
  • メモリリークには影響しません
  • UID プロパティをそのままにします
  • IFormatProvider として使用する SpecificCulture を提供します(例:(123.20).ToString(LocalizeDictionary.SpecificCulture)=" 123.20" または" 123,20"
  • コードビハインドでリソース値を確認および取得する機能を提供します
  • Thread.CurrentCulture または Thread.CurrentUICulture のカルチャは変更しません(簡単に変更できます)

他のヒント

次の手順に従ってください:

1)すべての String フラグメントを個別のリソースファイルに配置します。

例: StringResources.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">

    <!-- String resource that can be localized -->
    <system:String x:Key="All_Vehicles">All Vehicles</system:String>

</ResourceDictionary>

2)各言語のコピーを作成し、それらをマージされた辞書に追加(翻訳)します。物事を簡単にするために国のISOコードを追加することを忘れないでください。

App.xaml

<Application x:Class="WpfStringTables.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="StringResources.de-DE.xaml" />
                <ResourceDictionary Source="StringResources.nl-NL.xaml" />
                <ResourceDictionary Source="StringResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

文字列を含む最後のリソースファイルは、コード内のテキスト部分を置き換えるために使用されます。

3a) String テーブルのテキスト部分を使用します:

Window1.xaml

<Window x:Class="WpfStringTables.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Margin="51,82,108,129" Name="AllVehiclesButton" 
                Content="{StaticResource All_Vehicles}"/>
    </Grid>
</Window>

3b)コードからリソースをロードします( XAML で設定したくない場合にのみこのコードを使用します):

void PageLoad()
{
  string str = FindResource("All_Vehicles").ToString();
}

4)アプリケーションの開始時に新しいカルチャに切り替えます:

App.xaml.cs のコードスニペット:

public static void SelectCulture(string culture)    
{      
    if (String.IsNullOrEmpty(culture))
        return;

    //Copy all MergedDictionarys into a auxiliar list.
    var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();

    //Search for the specified culture.     
    string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
    var resourceDictionary = dictionaryList.
        FirstOrDefault(d => d.Source.OriginalString == requestedCulture);

    if (resourceDictionary == null)
    {
        //If not found, select our default language.             
        requestedCulture = "StringResources.xaml";
        resourceDictionary = dictionaryList.
            FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
    }

    //If we have the requested resource, remove it from the list and place at the end.     
    //Then this language will be our string table to use.      
    if (resourceDictionary != null)
    {
        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
    }

    //Inform the threads of the new culture.     
    Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

}

ジョシュ・スミスは、このための彼の好ましい方法について詳細なチュートリアルを書きました: WPFの国際化ウィザード

大幅な再設計に向けられる可能性があります( MVVMソリューション)、しかしMVVMを使用することは他の理由でも価値があるようです。

この記事を使用して、リソースファイルを簡単に使用して多言語WPFウィンドウを処理できました。 http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx 本当に簡単で効果的だから、チェックしてください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top