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.GetOrThrow(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 KeyValuePair.Create(m, con)).ToDictionary(); if (memberConflicts.IsEmpty() && typeConflicts == null) { return(null); } return(new LocalizedTypeChanges { Type = targetType, TypeConflict = typeConflicts, MemberConflicts = memberConflicts }); }).NotNull().ToList(); return(types); }
static Dictionary <CultureInfo, TypeNameConflict>?TypeConflicts(LocalizedType target, LocalizedType master, List <LocalizedType> support) { if (!master.IsTypeCompleted()) { return(null); } if (target.IsTypeCompleted()) { return(null); } var sentences = new Dictionary <CultureInfo, TypeNameConflict> { { master.Assembly.Culture, new TypeNameConflict { Original = master } } }; sentences.AddRange(from lt in support where lt.Description != null select KeyValuePair.Create(lt.Assembly.Culture, new TypeNameConflict { Original = lt })); return(sentences); }
public LocalizedAttribute(string resourceId, Type type, LocalizedType ltype) { this._resourceType = type; this._resourceId = resourceId; this.Type = ltype; this.InitProperty(); }
internal void Apply(LocalizedType lt) { switch (Kind) { case TranslationRecordKind.Description: lt.Description = Value; break; case TranslationRecordKind.PluralDescription: lt.PluralDescription = Value; break; case TranslationRecordKind.Gender: lt.Gender = Value != null ? (char?)Value[0] : null; break; case TranslationRecordKind.Member: lt.Members[Member] = Value; break; default: throw new InvalidOperationException("Unexpected kind {0}".FormatWith(Kind)); } }
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?.Elements("Member") .Select(m => KeyValuePair.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?.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 KeyValuePair.Create(m.Name, value)) .ToDictionary() }; return(result); }
private IEnumerable <KeyValuePair <string, string> > GetAllTranslations(LocalizedType from, LocalizedType to) { if (from.Description.HasText() && to.Description.HasText()) { yield return(KeyValuePair.Create(from.Description, to.Description)); } if (from.PluralDescription.HasText() && to.PluralDescription.HasText()) { yield return(KeyValuePair.Create(from.PluralDescription, to.PluralDescription)); } foreach (var item in from.Members !) { var toMember = to.Members !.TryGetC(item.Key); if (toMember.HasText()) { yield return(KeyValuePair.Create(item.Value, toMember)); } } }
public static LocalizedAssembly FromXml(Assembly assembly, CultureInfo cultureInfo, XDocument?doc, Dictionary <string, string>?replacements /*new -> old*/) { Dictionary <string, XElement>?file = doc?.Element("Translations").Elements("Type") .Select(x => KeyValuePair.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 Dictionary <CultureInfo, MemberNameConflict>?MemberConflicts(string member, LocalizedType target, LocalizedType master, List <LocalizedType> support) { if (master.Members !.TryGetC(member) == null) { return(null); } if (target != null && target.Members !.TryGetC(member) != null) { return(null); } var sentences = new Dictionary <CultureInfo, MemberNameConflict> { { master.Assembly.Culture, new MemberNameConflict { Original = master.Members !.TryGetC(member) } } }; sentences.AddRange(from lt in support where lt.Members !.TryGetC(member).HasText() select KeyValuePair.Create(lt.Assembly.Culture, new MemberNameConflict { Original = lt.Members !.TryGetC(member) }));