/// <summary> /// Return the extension value, implemented by all providers /// </summary> /// <param name="ext"></param> /// <param name="culture"></param> /// <returns></returns> public override object GetObject(LocalizationExtension ext, CultureInfo culture) { List <LocalizationItem> listLoc = GetLocalizationItem(culture.IetfLanguageTag, ext.ResModul); if (listLoc != null) { LocalizationItem item = listLoc.Single(p => p.Key == ext.Key); item.IsUsed = true; return(item.Translated); } else { listLoc = GetLocalizationItem(culture.IetfLanguageTag, "UNDEFINED"); if (listLoc != null) { LocalizationItem item = listLoc.Single(p => p.Key == ext.Key); item.IsUsed = true; return(item.Translated); } else { return(null); } } }
public override object GetObject(LocalizationExtension ext, CultureInfo culture) { ResourceManager resourceManager = GetResourceManager(ext.ResModul); if (resourceManager != null) { return(resourceManager.GetObject(ext.Key, CultureManager.Instance.UICulture)); } else { return(null); } }
/// <summary> /// Return the default value for the property /// </summary> /// <param name="ext"></param> /// <param name="culture"></param> /// <returns></returns> public virtual object GetDefaultValue(LocalizationExtension ext, CultureInfo culture) { if (LogHelper.CanDebug()) { LogHelper.Begin("ProviderBase.GetDefaultValue", "extension:{0}, culture:{1}", ext.ToString(), culture.DisplayName); } try { object result = ext.DefaultValue; Type targetType = ext.TargetPropertyType; if (ext.DefaultValue == null) { if (targetType == typeof(String) || targetType == typeof(object)) { result = "No default on #" + ext.Key; } } else if (targetType != null) { // convert the default value if necessary to the required type if (targetType != typeof(String) && targetType != typeof(object)) { try { TypeConverter tc = TypeDescriptor.GetConverter(targetType); result = tc.ConvertFromInvariantString(ext.DefaultValue); } catch { } } } return(result); } catch (Exception err) { LogHelper.Manage("ProviderBase.GetDefaultValue", err); return(null); } finally { LogHelper.End("ProviderBase.GetDefaultValue"); } }
/// <summary> /// Extra method to get translation from code /// </summary> /// <param name="ietfCode"></param> /// <param name="modul"></param> /// <param name="key"></param> /// <param name="defaultValue"></param> /// <returns></returns> public override string GetLocalizationResource(string ietfCode, string modul, string key, string defaultValue) { List <LocalizationExtension> ext = MarkupExtensionManager.Instance.Extensions. Cast <LocalizationExtension>().Where(p => p.ResModul == modul && p.Key == key).ToList(); LocalizationExtension locExt; if (ext.Count == 1) { locExt = ext[0]; } else { locExt = new LocalizationExtension(modul, key, defaultValue); } LocalizationItem item = GetCorrespondingItem(locExt, CultureInfo.GetCultureInfo(ietfCode)); return(item.Translated); }
protected void CreateForOtherlanguage(LocalizationItem item, string ietfCodeOther, string modul) { try { foreach (LocalizationFile fileOther in _localizationFileList.Where(p => p.IetfLanguageTag != ietfCodeOther)) { List <LocalizationExtension> ext = MarkupExtensionManager.Instance.Extensions. Cast <LocalizationExtension>().Where(p => p.ResModul == modul && p.Key == item.Key).ToList(); LocalizationExtension locExt; if (ext.Count == 1) { locExt = ext[0]; } else { locExt = new LocalizationExtension(modul, item.Key, item.Default); } GetCorrespondingItem(locExt, CultureInfo.GetCultureInfo(fileOther.IetfLanguageTag)); } } catch { } }
internal LocalizationItem GetCorrespondingItem(LocalizationExtension ext, CultureInfo culture) { if (string.IsNullOrEmpty(ext.DefaultValue)) { ext.DefaultValue = (string)base.GetDefaultValue(ext, culture); } //add default value to the resource LocalizationItem itemResult = new LocalizationItem() { Key = ext.Key, Default = ext.DefaultValue, Translated = ext.DefaultValue }; List <LocalizationItem> listLoc = null; if (string.IsNullOrEmpty(ext.ResModul)) //case modul is missing, invent a undefined.2letter.xml dictionnary { listLoc = GetLocalizationItem(culture.IetfLanguageTag, "UNDEFINED"); //no resource dictionnary for code and modul if (listLoc == null) { //create it LocalizationDictionary dict = GetUndefined(culture.IetfLanguageTag); dict.LocalizationItems.Add(itemResult); } else //list exist, what about the resource { if (listLoc.Exists(p => p.Key == ext.Key)) { return(listLoc.Single(p => p.Key == ext.Key)); } else { listLoc.Add(itemResult); } } itemResult.IsUsed = true; return(itemResult); } LocalizationFile loc_file = GetLocalizationFileFromCode(culture.IetfLanguageTag); //file is missing if (loc_file == null) { //create the file loc_file = new LocalizationFile(culture.IetfLanguageTag); _localizationFileList.Add(loc_file); //create the dico LocalizationDictionary dict = new LocalizationDictionary() { Module = ext.ResModul, IetfLanguageTag = culture.IetfLanguageTag, FileName = string.Format("{0}.{1}.{2}", ext.ResModul, culture.IetfLanguageTag, FileExtension), LocalizationItems = new List <LocalizationItem>() }; loc_file.Dictionnaries.Add(dict); dict.LocalizationItems.Add(itemResult); itemResult.IsUsed = true; return(itemResult); } LocalizationDictionary dico = GetLocalizationDictionary(culture.IetfLanguageTag, ext.ResModul); // no dictionnary if (dico == null) { //create the dico dico = new LocalizationDictionary() { Module = ext.ResModul, IetfLanguageTag = culture.IetfLanguageTag, FileName = string.Format("{0}.{1}.{2}", ext.ResModul, culture.IetfLanguageTag, FileExtension), LocalizationItems = new List <LocalizationItem>() }; loc_file.Dictionnaries.Add(dico); dico.LocalizationItems.Add(itemResult); itemResult.IsUsed = true; return(itemResult); } else { List <LocalizationItem> temp = dico.LocalizationItems.Where(p => p.Key == itemResult.Key).ToList(); if (temp.Count == 1) { itemResult = temp[0]; } else { dico.LocalizationItems.Add(itemResult); CreateForOtherlanguage(itemResult, culture.IetfLanguageTag, ext.ResModul); } } itemResult.IsUsed = true; return(itemResult); }
/// <summary> /// Return the default value for the property /// </summary> /// <param name="ext"></param> /// <param name="culture"></param> /// <returns></returns> public override object GetDefaultValue(LocalizationExtension ext, CultureInfo culture) { return(GetCorrespondingItem(ext, culture).Default); }
/// <summary> /// Convert a resource object to the type required by the WPF element /// </summary> /// <param name="value">The resource value to convert</param> /// <returns>The WPF element value</returns> public object ConvertValue(LocalizationExtension ext, object value) { object result = null; if (LogHelper.CanDebug()) { LogHelper.Begin("ProviderBase.ConvertValue"); } try { //BitmapSource bitmapSource = null; //// convert icons and bitmaps to BitmapSource objects that WPF uses //if (value is Icon) //{ // Icon icon = value as Icon; // // For icons we must create a new BitmapFrame from the icon data stream // // The approach we use for bitmaps (below) doesn't work when setting the // // Icon property of a window (although it will work for other Icons) // // // using (MemoryStream iconStream = new MemoryStream()) // { // icon.Save(iconStream); // iconStream.Seek(0, SeekOrigin.Begin); // bitmapSource = BitmapFrame.Create(iconStream); // } //} //else if (value is Bitmap) //{ // Bitmap bitmap = value as Bitmap; // IntPtr bitmapHandle = bitmap.GetHbitmap(); // bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapHandle, IntPtr.Zero, Int32Rect.Empty, // BitmapSizeOptions.FromEmptyOptions()); // bitmapSource.Freeze(); // DeleteObject(bitmapHandle); //} //if (bitmapSource != null) //{ // // if the target property is expecting the Icon to be content then we // // create an ImageControl and set its Source property to image // // // if (ext.TargetPropertyType == typeof(object)) // { // System.Windows.Controls.Image imageControl = new System.Windows.Controls.Image(); // imageControl.Source = bitmapSource; // imageControl.Width = bitmapSource.Width; // imageControl.Height = bitmapSource.Height; // result = imageControl; // } // else // { // result = bitmapSource; // } //} //else { result = value; // allow for resources to either contain simple strings or typed data // Type targetType = ext.TargetPropertyType; if (value is String && targetType != typeof(String) && targetType != typeof(object)) { TypeConverter tc = TypeDescriptor.GetConverter(targetType); result = tc.ConvertFromInvariantString(value as string); } } return(result); } catch (Exception err) { LogHelper.Manage("ProviderBase.ConvertValue", err); return(null); } finally { LogHelper.End("ProviderBase.ConvertValue"); } }
/// <summary> /// Return the extension value, implemented by all providers /// </summary> /// <param name="ext"></param> /// <param name="culture"></param> /// <returns></returns> public virtual object GetObject(LocalizationExtension ext, CultureInfo culture) { throw new NotImplementedException(); }