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