public static char?GetGender(this Type type) { type = CleanType(type); var cc = CultureInfo.CurrentUICulture; if (LocalizedAssembly.GetDefaultAssemblyCulture(type.Assembly) == null) { return(type.GetCustomAttribute <GenderAttribute>()?.Gender ?? NaturalLanguageTools.GetGender(type.NiceName())); } var lt = GetLocalizedType(type, cc); if (lt != null && lt.Gender != null) { return(lt.Gender); } if (cc.Parent.Name.HasText()) { lt = GetLocalizedType(type, cc.Parent); if (lt != null) { return(lt.Gender); } } return(null); }
static string GetMemberNiceName(MemberInfo memberInfo) { //var cc = CultureInfo.CurrentUICulture; var type = memberInfo.DeclaringType; if (!LocalizedAssembly.HasDefaultAssemblyCulture(type.Assembly)) { var f = ExternalEnums.TryGetC(type); if (f != null) { return(f(memberInfo)); } return(memberInfo.GetCustomAttribute <DescriptionAttribute>()?.Description ?? memberInfo.Name.NiceName()); } var result = Fallback(type, lt => lt.Members.TryGetC(memberInfo.Name), lt => OnNotLocalizedMemeber(null, memberInfo)); if (result != null) { return(result); } return(result); }
public static List<LocalizedTypeChanges> GetMergeChanges(LocalizedAssembly target, LocalizedAssembly master, List<LocalizedAssembly> support) { var types = master.Types.Select(kvp => { Type type = kvp.Key; LocalizedType targetType = target.Types.TryGetC(type); LocalizedType masterType = master.Types[type]; List<LocalizedType> supportTypes = support.Select(la => la.Types.TryGetC(type)).NotNull().ToList(); Dictionary<CultureInfo, TypeNameConflict> typeConflicts = TypeConflicts(targetType, masterType, supportTypes); var memberConflicts = (from m in masterType.Members.Keys let con = MemberConflicts(m, targetType, masterType, supportTypes) where con != null select KVP.Create(m, con)).ToDictionary(); if (memberConflicts.IsEmpty() && typeConflicts == null) return null; return new LocalizedTypeChanges { Type = targetType, TypeConflict = typeConflicts, MemberConflicts = memberConflicts }; }).NotNull().ToList(); return types; }
private static LocalizedAssemblyChanges Translate(ITranslator translator, LocalizedAssembly target, List<LocalizedTypeChanges> types) { List<IGrouping<CultureInfo, TypeNameConflict>> typeGroups = (from t in types where t.TypeConflict != null from tc in t.TypeConflict select tc).GroupBy(a => a.Key, a => a.Value).ToList(); foreach (IGrouping<CultureInfo, TypeNameConflict> gr in typeGroups) { List<string> result = translator.TranslateBatch(gr.Select(a => a.Original.Description).ToList(), gr.Key.Name, target.Culture.Name); gr.ZipForeach(result, (sp, translated) => sp.Translated = translated); } List<IGrouping<CultureInfo, MemberNameConflict>> memberGroups = (from t in types where t.MemberConflicts != null from mcKVP in t.MemberConflicts from mc in mcKVP.Value select mc).GroupBy(a => a.Key, a => a.Value).ToList(); foreach (IGrouping<CultureInfo, MemberNameConflict> gr in memberGroups) { var result = translator.TranslateBatch(gr.Select(a => a.Original).ToList(), gr.Key.Name, target.Culture.Name); gr.ZipForeach(result, (sp, translated) => sp.Translated = translated); } return new LocalizedAssemblyChanges { LocalizedAssembly = target, Types = types, }; }
static string Fallback(Type type, Func <LocalizedType, string> typeValue, Action <LocalizedType> notLocalized) { var cc = CultureInfo.CurrentUICulture; { var loc = GetLocalizedType(type, cc); if (loc != null) { string result = typeValue(loc); if (result != null) { return(result); } } } if (cc.Parent.Name.HasText()) { var loc = GetLocalizedType(type, cc.Parent); if (loc != null) { string result = typeValue(loc); if (result != null) { return(result); } } } var defaultCulture = LocalizedAssembly.GetDefaultAssemblyCulture(type.Assembly); //if (defaultCulture != null) { var loc = GetLocalizedType(type, CultureInfo.GetCultureInfo(defaultCulture)); if (loc == null) { throw new InvalidOperationException("Type {0} is not localizable".FormatWith(type.TypeName())); } if (notLocalized != null) { notLocalized.Invoke(loc); } return(typeValue(loc)); } //return null; }
public static string NiceName(this Type type) { type = CleanType(type); if (!LocalizedAssembly.HasDefaultAssemblyCulture(type.Assembly)) { return(type.GetCustomAttribute <DescriptionAttribute>()?.Description ?? type.Name.NiceName()); } var result = Fallback(type, lt => lt.Description, lt => OnNotLocalizedMemeber(type, null)); if (result != null) { return(result); } return(DefaultTypeDescription(type)); }
public static LocalizedAssemblyChanges GetAssemblyChanges(ITranslator translator, LocalizedAssembly target, LocalizedAssembly master, List<LocalizedAssembly> support, bool translatedOnly, out int totalTypes) { var types = GetMergeChanges(target, master, support); totalTypes = types.Count; if (!translatedOnly && types.Sum(a => a.TotalOriginalLength()) > MaxTotalSyncCharacters) types = types.GroupsOf(a => a.TotalOriginalLength(), MaxTotalSyncCharacters).First().ToList(); var result = Translate(translator, target, types); if (translatedOnly) result.Types = result.Types.Where(t => t.TypeConflict != null && t.TypeConflict.Values.Any(tc => tc.Translated.HasText()) || t.MemberConflicts != null && t.MemberConflicts.Values.Any(m => m.Values.Any(mc => mc.Translated.HasText()))) .ToList(); return result; }
internal static LocalizedType ImportXml(Type type, DescriptionOptions opts, LocalizedAssembly assembly, XElement x) { string description = !opts.IsSetAssert(DescriptionOptions.Description, type) ? null : (x == null || x.Attribute("Name").Value != type.Name ? null : x.Attribute("Description")?.Value) ?? (!assembly.IsDefault ? null : DescriptionManager.DefaultTypeDescription(type)); var xMembers = x == null ? null : x.Elements("Member") .Select(m => KVP.Create(m.Attribute("Name").Value, m.Attribute("Description").Value)) .Distinct(m => m.Key) .ToDictionary(); LocalizedType result = new LocalizedType { Type = type, Options = opts, Assembly = assembly, Description = description, PluralDescription = !opts.IsSetAssert(DescriptionOptions.PluralDescription, type) ? null : ((x == null || x.Attribute("Name").Value != type.Name ? null : x.Attribute("PluralDescription")?.Value) ?? (!assembly.IsDefault ? null : type.GetCustomAttribute <PluralDescriptionAttribute>()?.PluralDescription) ?? (description == null ? null : NaturalLanguageTools.Pluralize(description, assembly.Culture))), Gender = !opts.IsSetAssert(DescriptionOptions.Gender, type) ? null : ((x == null ? null : x.Attribute("Gender")?.Value.Single()) ?? (!assembly.IsDefault ? null : type.GetCustomAttribute <GenderAttribute>()?.Gender) ?? (description == null ? null : NaturalLanguageTools.GetGender(description, assembly.Culture))), Members = !opts.IsSetAssert(DescriptionOptions.Members, type) ? null : (from m in GetMembers(type) where DescriptionManager.OnShouldLocalizeMember(m) let value = xMembers?.TryGetC(m.Name) ?? (!assembly.IsDefault ? null : DescriptionManager.DefaultMemberDescription(m)) where value != null select KVP.Create(m.Name, value)) .ToDictionary() }; return(result); }
public static LocalizedAssembly FromXml(Assembly assembly, CultureInfo cultureInfo, XDocument doc, Dictionary <string, string> replacements /*new -> old*/) { Dictionary <string, XElement> file = doc == null ? null : doc.Element("Translations").Elements("Type") .Select(x => KVP.Create(x.Attribute("Name").Value, x)) .Distinct(x => x.Key) .ToDictionary(); var result = new LocalizedAssembly { Assembly = assembly, Culture = cultureInfo, IsDefault = GetDefaultAssemblyCulture(assembly) == cultureInfo.Name }; result.Types = (from t in assembly.GetTypes() let opts = GetDescriptionOptions(t) where opts != DescriptionOptions.None let x = file?.TryGetC(replacements?.TryGetC(t.Name) ?? t.Name) select LocalizedType.ImportXml(t, opts, result, x)) .ToDictionary(lt => lt.Type); return(result); }
static string GetMemberNiceName(MemberInfo memberInfo) { var cc = CultureInfo.CurrentUICulture; var type = memberInfo.DeclaringType; if (LocalizedAssembly.GetDefaultAssemblyCulture(type.Assembly) == null) { if (type == typeof(DayOfWeek)) { return(CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)((FieldInfo)memberInfo).GetValue(null)]); } return(memberInfo.GetCustomAttribute <DescriptionAttribute>()?.Description ?? memberInfo.Name.NiceName()); } var result = Fallback(type, lt => lt.Members.TryGetC(memberInfo.Name)); if (result != null) { return(result); } return(result); }
static Dictionary<string, LocalizedType> DictionaryByTypeName(LocalizedAssembly locAssembly) { return locAssembly.Types.Values.ToDictionary(a => a.Type.Name, "LocalizedTypes"); }
public static LocalizedAssembly GetLocalizedAssembly(Assembly assembly, CultureInfo cultureInfo) { return(localizations .GetOrAdd(cultureInfo, ci => new ConcurrentDictionary <Assembly, LocalizedAssembly>()) .GetOrAdd(assembly, (Assembly a) => LocalizedAssembly.ImportXml(assembly, cultureInfo, forceCreate: false))); }
internal static LocalizedType ImportXml(Type type, DescriptionOptions opts, LocalizedAssembly assembly, XElement x) { string name = !opts.IsSetAssert(DescriptionOptions.Description, type) ? null : (x == null ? null : x.Attribute("Description").Try(xa => xa.Value)) ?? (!assembly.IsDefault ? null : DescriptionManager.DefaultTypeDescription(type)); var xMembers = x == null ? null : x.Elements("Member") .Select(m => KVP.Create(m.Attribute("Name").Value, m.Attribute("Description").Value)) .Distinct(m => m.Key) .ToDictionary(); LocalizedType result = new LocalizedType { Type = type, Options = opts, Assembly = assembly, Description = name, PluralDescription = !opts.IsSetAssert(DescriptionOptions.PluralDescription, type) ? null : ((x == null ? null : x.Attribute("PluralDescription").Try(xa => xa.Value)) ?? (!assembly.IsDefault ? null : type.SingleAttribute<PluralDescriptionAttribute>().Try(t => t.PluralDescription)) ?? (name == null ? null : NaturalLanguageTools.Pluralize(name, assembly.Culture))), Gender = !opts.IsSetAssert(DescriptionOptions.Gender, type) ? null : ((x == null ? null : x.Attribute("Gender").Try(xa => xa.Value.Single())) ?? (!assembly.IsDefault ? null : type.SingleAttribute<GenderAttribute>().Try(t => t.Gender)) ?? (name == null ? null : NaturalLanguageTools.GetGender(name, assembly.Culture))), Members = !opts.IsSetAssert(DescriptionOptions.Members, type) ? null : (from m in GetMembers(type) where DescriptionManager.OnShouldLocalizeMember(m) let value = xMembers.TryGetC(m.Name) ?? (!assembly.IsDefault ? null : DescriptionManager.DefaultMemberDescription(m)) where value != null select KVP.Create(m.Name, value)) .ToDictionary() }; return result; }
public static LocalizedAssembly ImportXml(Assembly assembly, CultureInfo cultureInfo, bool forceCreate) { var defaultCulture = GetDefaultAssemblyCulture(assembly); if(defaultCulture == null) return null; bool isDefault = cultureInfo.Name == defaultCulture; string fileName = TranslationFileName(assembly, cultureInfo); Dictionary<string, XElement> file = !File.Exists(fileName) ? null : XDocument.Load(fileName).Element("Translations").Elements("Type") .Select(x => KVP.Create(x.Attribute("Name").Value, x)) .Distinct(x => x.Key) .ToDictionary(); if (!isDefault && !forceCreate && file == null) return null; var result = new LocalizedAssembly { Assembly = assembly, Culture = cultureInfo, IsDefault = isDefault }; result.Types = (from t in assembly.GetTypes() let opts = GetDescriptionOptions(t) where opts != DescriptionOptions.None let x = file.TryGetC(t.Name) select LocalizedType.ImportXml(t, opts, result, x)) .ToDictionary(lt => lt.Type); return result; }