162 lines
6.0 KiB
C#
162 lines
6.0 KiB
C#
#region Copyright information
|
|
// <copyright file="InheritingResxLocalizationProvider.cs">
|
|
// Licensed under Microsoft Public License (Ms-PL)
|
|
// https://github.com/XAMLMarkupExtensions/WPFLocalizationExtension/blob/master/LICENSE
|
|
// </copyright>
|
|
// <author>Uwe Mayer</author>
|
|
#endregion
|
|
|
|
namespace WPFLocalizeExtension.Providers
|
|
{
|
|
#region Usings
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.Resources;
|
|
using System.Windows;
|
|
using XAMLMarkupExtensions.Base;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// A singleton RESX provider that uses inheriting attached properties.
|
|
/// </summary>
|
|
public class InheritingResxLocalizationProvider : ResxLocalizationProviderBase, IInheritingLocalizationProvider
|
|
{
|
|
#region Dependency Properties
|
|
/// <summary>
|
|
/// <see cref="DependencyProperty"/> DefaultDictionary to set the fallback resource dictionary.
|
|
/// </summary>
|
|
public static readonly DependencyProperty DefaultDictionaryProperty =
|
|
DependencyProperty.RegisterAttached(
|
|
"DefaultDictionary",
|
|
typeof(string),
|
|
typeof(InheritingResxLocalizationProvider),
|
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, AttachedPropertyChanged));
|
|
|
|
/// <summary>
|
|
/// <see cref="DependencyProperty"/> DefaultAssembly to set the fallback assembly.
|
|
/// </summary>
|
|
public static readonly DependencyProperty DefaultAssemblyProperty =
|
|
DependencyProperty.RegisterAttached(
|
|
"DefaultAssembly",
|
|
typeof(string),
|
|
typeof(InheritingResxLocalizationProvider),
|
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, AttachedPropertyChanged));
|
|
#endregion
|
|
|
|
#region Dependency Property Callback
|
|
/// <summary>
|
|
/// Indicates, that one of the attached properties changed.
|
|
/// </summary>
|
|
/// <param name="obj">The dependency object.</param>
|
|
/// <param name="args">The event argument.</param>
|
|
private static void AttachedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
|
|
{
|
|
Instance.OnProviderChanged(obj);
|
|
}
|
|
#endregion
|
|
|
|
#region Dependency Property Management
|
|
#region Get
|
|
/// <summary>
|
|
/// Getter of <see cref="DependencyProperty"/> default dictionary.
|
|
/// </summary>
|
|
/// <param name="obj">The dependency object to get the default dictionary from.</param>
|
|
/// <returns>The default dictionary.</returns>
|
|
public static string GetDefaultDictionary(DependencyObject obj)
|
|
{
|
|
return obj.GetValueSync<string>(DefaultDictionaryProperty);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Getter of <see cref="DependencyProperty"/> default assembly.
|
|
/// </summary>
|
|
/// <param name="obj">The dependency object to get the default assembly from.</param>
|
|
/// <returns>The default assembly.</returns>
|
|
public static string GetDefaultAssembly(DependencyObject obj)
|
|
{
|
|
return obj.GetValueSync<string>(DefaultAssemblyProperty);
|
|
}
|
|
#endregion
|
|
|
|
#region Set
|
|
/// <summary>
|
|
/// Setter of <see cref="DependencyProperty"/> default dictionary.
|
|
/// </summary>
|
|
/// <param name="obj">The dependency object to set the default dictionary to.</param>
|
|
/// <param name="value">The dictionary.</param>
|
|
public static void SetDefaultDictionary(DependencyObject obj, string value)
|
|
{
|
|
obj.SetValueSync(DefaultDictionaryProperty, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setter of <see cref="DependencyProperty"/> default assembly.
|
|
/// </summary>
|
|
/// <param name="obj">The dependency object to set the default assembly to.</param>
|
|
/// <param name="value">The assembly.</param>
|
|
public static void SetDefaultAssembly(DependencyObject obj, string value)
|
|
{
|
|
obj.SetValueSync(DefaultAssemblyProperty, value);
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
#region Singleton Variables, Properties & Constructor
|
|
/// <summary>
|
|
/// The instance of the singleton.
|
|
/// </summary>
|
|
private static InheritingResxLocalizationProvider _instance;
|
|
|
|
/// <summary>
|
|
/// Lock object for the creation of the singleton instance.
|
|
/// </summary>
|
|
private static readonly object InstanceLock = new object();
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="ResxLocalizationProvider"/> singleton.
|
|
/// </summary>
|
|
public static InheritingResxLocalizationProvider Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
lock (InstanceLock)
|
|
{
|
|
if (_instance == null)
|
|
_instance = new InheritingResxLocalizationProvider();
|
|
}
|
|
}
|
|
|
|
// return the existing/new instance
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The singleton constructor.
|
|
/// </summary>
|
|
private InheritingResxLocalizationProvider()
|
|
{
|
|
ResourceManagerList = new Dictionary<string, ResourceManager>();
|
|
AvailableCultures = new ObservableCollection<CultureInfo> { CultureInfo.InvariantCulture };
|
|
}
|
|
#endregion
|
|
|
|
#region Abstract assembly & dictionary lookup
|
|
/// <inheritdoc/>
|
|
protected override string GetAssembly(DependencyObject target)
|
|
{
|
|
return target?.GetValue(DefaultAssemblyProperty) as string;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
protected override string GetDictionary(DependencyObject target)
|
|
{
|
|
return target?.GetValue(DefaultDictionaryProperty) as string;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|