私の現在の解決策は、グローバルに利用可能な文字列コレクション、つまりシングルトンにローカライズしたいプロパティ (つまり、 、 、ToolTips など) で DataBinding を使用することTextBox
です。私の場合、MVVM Light を使用しているため、.Text
Button
Content
LocalizedStringCollection
ViewModelLocator
このコレクションは、XLIFF ファイル ( https://en.wikipedia.org/wiki/Xliffを参照) から、Dictionary<string, string>
私のコレクションのメンバーである にロードされます。
文字列を公開するためのコレクションの重要な部分は次のとおりです。
/// <summary>
/// A Singleton bindeable collection of localized strings.
/// </summary>
public class LocalizedStringCollection : ObservableObject, INotifyCollectionChanged
{
private Dictionary<string, string> _items;
/// <summary>
/// The content of the collection.
/// </summary>
public Dictionary<string, string> Items
{
get { return _items; }
private set
{
_items = value;
RaisePropertyChanged();
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
/// <summary>
/// Convenience accessor, most likely entry point.
/// </summary>
/// <param name="key">A localized string name</param>
/// <returns>The localized version if found, an error string else (ErrorValue in DEBUG mode, the key in RELEASE)</returns>
public string this[string key]
{
get
{
Contract.Requires(key != null);
Contract.Ensures(Contract.Result<string>() != null);
string value;
if (Items.TryGetValue(key, out value))
return value ?? String.Empty;
#if DEBUG
return ErrorValue;
#else
return key;
#endif
}
}
}
XLIFF の解析は、組み込みの XML サポートを使用すると簡単です。
XAML の使用例を次に示します (使用する構文に応じて、多かれ少なかれ冗長になる可能性があります)。
<TextBlock Text="{Binding LocalizedStrings[login_label], Source={StaticResource Locator}}" />
<Button ToolTipService.ToolTip="{Binding LocalizedStrings[delete_content_button_tip], Source={StaticResource Locator}}" />
お役に立てれば :)
人々が興味を持っているなら、私はこれの記事を(完全なソースとともに)作るかもしれません.