/// <summary> /// Applies localization to item by specified culture /// </summary> /// <param name="item">Localization item</param> /// <param name="cultureInfo">Culture info</param> private static void SetValue(LocalizationItem item, CultureInfo cultureInfo) { // check if (item == null) { throw new ArgumentNullException("item"); } object value = null; CultureInfo culture = cultureInfo != null ? cultureInfo : CurrentCulture; try { TypeConverter converter = TypeDescriptor.GetConverter(item.Property.PropertyType); // resource is string if (converter.CanConvertFrom(typeof(string))) { string formatedValue = item.Manager.GetString(item.ResourceKey, culture); if (!String.IsNullOrEmpty(item.Format)) { formatedValue = String.Format(culture, item.Format, formatedValue); } if (item.Property.PropertyType != typeof(string)) { value = converter.ConvertFromString(formatedValue); } else { value = formatedValue; } } else { // resource is stream if (item.Property.PropertyType == typeof(Stream)) { value = item.Manager.GetStream(item.ResourceKey, culture); } else { value = item.Manager.GetObject(item.ResourceKey, culture); } } lock (_sync) { if (_items.Contains(item)) { item.Property.SetValue(item.Instance, value); } } } catch (Exception e) { throw new LocalizerException(e.Message, e); } }
public LocalizationData GenerateList() { if (mDictionaryText == null || mDictionaryText.Count == 0) { return(null); } var ret = new LocalizationData { Items = new List <LocalizationItem>() }; foreach (var data in mDictionaryText) { var item = new LocalizationItem(data); ret.Items.Add(item); } return(ret); }
/// <summary> /// Applies localization to item /// </summary> /// <param name="item">Localization item</param> private static void SetValue(LocalizationItem item) { SetValue(item, null); }
/// <summary> /// Adds instance and property for localizing /// </summary> /// <param name="instance">Instance to object</param> /// <param name="propertyName">Name of property</param> /// <param name="resourceKey">Resource key</param> /// <param name="format">Custom string format</param> /// <param name="resourceManager">Resource manager</param> public static void Add(object instance, string propertyName, string resourceKey, string format, ResourceManager resourceManager) { // check if (instance == null) { throw new ArgumentNullException("instance"); } if (String.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } resourceKey = resourceKey ?? String.Empty; if (String.IsNullOrEmpty(resourceKey.Trim())) { throw new ArgumentNullException("resourceKey"); } if (resourceManager == null) { throw new ArgumentNullException("resourceManager"); } PropertyDescriptor descriptor = TypeDescriptor.GetProperties(instance)[propertyName]; if (descriptor == null) { throw new ArgumentException("property does not belongs to instance", "propertyName"); } // trace if (_tracingEnabled) { Trace.WriteLine(String.Format("Added item [Instance:\"{0}\" Property:\"{1}\" ResourceKey:\"{2}\" Format:\"{3}\"]", instance.ToString(), propertyName, resourceKey, format ?? String.Empty, resourceManager)); } // create localization item LocalizationItem item = new LocalizationItem(); item.Instance = instance; item.Manager = resourceManager; item.Property = descriptor; item.ResourceKey = resourceKey; item.Format = format; lock (_sync) { // check whether exists IEnumerable <LocalizationItem> existing = GetItems(instance, propertyName); if (existing.Count() > 0) { if (_exceptionOnDuplicate) { throw new LocalizerException("This instance and property is already defined."); } Remove(instance, propertyName); } // add item _items.Add(item); } // localize item SetValue(item); }