public static string ToString(object value) { if (value == null) { return(null); } switch (System.Type.GetTypeCode(value.GetType())) { case System.TypeCode.Boolean: return(System.Xml.XmlConvert.ToString((bool)value)); case System.TypeCode.Int16: return(System.Xml.XmlConvert.ToString((short)value)); case System.TypeCode.Int32: return(System.Xml.XmlConvert.ToString((int)value)); case System.TypeCode.Int64: return(System.Xml.XmlConvert.ToString((long)value)); case System.TypeCode.Single: return(System.Xml.XmlConvert.ToString((float)value)); case System.TypeCode.Double: return(System.Xml.XmlConvert.ToString((double)value)); case System.TypeCode.Decimal: return(System.Xml.XmlConvert.ToString((decimal)value)); case System.TypeCode.DateTime: return(System.Xml.XmlConvert.ToString((System.DateTime)value, System.Xml.XmlDateTimeSerializationMode.RoundtripKind)); case System.TypeCode.String: return((string)value); } if (value is byte[]) { byte[] array = (byte[])value; if (array.Length == 0) { return(string.Empty); } return(System.Convert.ToBase64String(array)); } else { System.IFormattable formattable = value as System.IFormattable; if (formattable != null) { return(formattable.ToString(null, System.Globalization.CultureInfo.InvariantCulture)); } return(value.ToString()); } }
private IFormattable FindContextFromAssemblyFactory(object targetObject) { IFormattable result = null; LocalizationContextFactoryBase localizationContextFactory = _contextFactoryProvider; if (localizationContextFactory == null) { Type factoryType = _contextFactoryType; if (factoryType == null && targetObject != null) { localizationContextFactory = FindContextFactoryFromAssembly(targetObject.GetType().Assembly); } if (factoryType != null && localizationContextFactory == null) { try { localizationContextFactory = Activator.CreateInstance(factoryType) as LocalizationContextFactoryBase; _contextFactoryProvider = localizationContextFactory ?? throw new InvalidOperationException( $"Type '{factoryType.FullName}' does not implement abstract class '{TypeLocalizationContextFactory.FullName}' !"); } catch (Exception e) { throw new InvalidOperationException( $"Error creating instance of type '{factoryType.FullName}' !", e); } } } if (localizationContextFactory != null) { result = localizationContextFactory.GetSingleton(); } return(result); }
/// <inheritdoc /> public override object ProvideValue(IServiceProvider serviceProvider) { IFormattable localizationContext = GetLocalizationContext(serviceProvider, out var _); if (localizationContext == null) { return($"@@{Key}@@"); } var binding = new Binding(PropertyNameCulture) { Source = localizationContext, Mode = BindingMode.TwoWay }; if (_bindings == null || _bindings.Count == 0) { binding.Converter = new LocalizationConverter(localizationContext, Key); object value = binding.ProvideValue(serviceProvider); return(value); } var multiBinding = new MultiBinding { Mode = BindingMode.TwoWay, Converter = new LocalizationConverter(localizationContext, Key, _bindings) }; multiBinding.Bindings.Add(binding); foreach (var item in _bindings) { multiBinding.Bindings.Add(item); } return(multiBinding.ProvideValue(serviceProvider)); }
/// <summary> /// Creates new instance. /// </summary> /// <param name="key">resource key</param> /// <param name="source">The source.</param> public LocalizationExtension(string key, IFormattable source) { Key = key; Source = source; }
private IFormattable GetLocalizationContext(IServiceProvider serviceProvider, out DependencyObject targetObjectOut) { targetObjectOut = null; if (_localizationContext == null) { try { var context = Source; if (context == null) { context = FindContextFromAssemblyFactory(null); if (context == null && serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget target) { var targetObject = target.TargetObject; if (targetObject != null) { if (targetObject is DependencyObject currentDependencyObject) { targetObjectOut = currentDependencyObject; DependencyObject lastCurrent = null; try { context = currentDependencyObject.TryFindParentDependencyProperty(item => { lastCurrent = item; var localizationSource = LocalizationSourceBehavior.GetLocalizationSource(item); if (localizationSource == null && item is FrameworkElement frameworkElement) { localizationSource = FindContextSourceInResources(frameworkElement); } return(localizationSource); }); } catch (Exception e) { Debug.WriteLine($"[LHQ] {nameof(LocalizationExtension)}.{nameof(GetLocalizationContext)}() try parent DP failed, " + $"message: {e.Message}, stackTrace: {e.StackTrace}"); } if (context == null) { try { if (lastCurrent != null && lastCurrent.GetParentObject() == null) { context = FindContextFromAssemblyFactory(lastCurrent); } } catch (Exception e) { Console.WriteLine(e); } if (context == null && Application.Current != null && !IsInDesignMode()) { context = FindContextSourceInResources(Application.Current.Resources.MergedDictionaries); } } } } } } // try to find context factory on entry assembly level if (context == null) { try { var contextFactoryBase = FindContextFactoryFromAssembly(Assembly.GetEntryAssembly()); if (contextFactoryBase != null) { context = contextFactoryBase.GetSingleton(); } } catch (Exception e) { Console.WriteLine(e); } } if (context != null) { Type localizationContextType = context.GetType(); PropertyInfo propertyCulture = localizationContextType.GetProperties().SingleOrDefault(x => x.Name == PropertyNameCulture); if (propertyCulture == null || propertyCulture.PropertyType != TypeCultureInfo) { throw new InvalidOperationException( $"Invalid localization context type '{localizationContextType.FullName}' ! " + $"Missing property name '{PropertyNameCulture}' of type '{TypeCultureInfo.FullName}' !"); } if (!TypeINotifyPropertyChanged.IsAssignableFrom(localizationContextType)) { throw new InvalidOperationException( $"Invalid localization context type '{localizationContextType.FullName}' ! " + $"Type must implement interface '{TypeINotifyPropertyChanged.FullName}' !"); } _localizationContext = context; } } catch (Exception e) { Debug.WriteLine($"[LHQ] {nameof(LocalizationExtension)}.{nameof(GetLocalizationContext)}() failed, " + $"message: {e.Message}, stackTrace: {e.StackTrace}"); } } return(_localizationContext); }