Beispiel #1
0
        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);
        }
Beispiel #2
0
        public XElement ExportXml()
        {
            return(new XElement("Type",
                                new XAttribute("Name", Type.Name),

                                !Options.IsSetAssert(DescriptionOptions.Description, Type) ||
                                Description == null ||
                                (Assembly.IsDefault && Description == DescriptionManager.DefaultTypeDescription(Type)) ? null :
                                new XAttribute("Description", Description),

                                !Options.IsSetAssert(DescriptionOptions.PluralDescription, Type) ||
                                PluralDescription == null ||
                                (PluralDescription == NaturalLanguageTools.Pluralize(Description, Assembly.Culture)) ? null :
                                new XAttribute("PluralDescription", PluralDescription),

                                !Options.IsSetAssert(DescriptionOptions.Gender, Type) ||
                                Gender == null ||
                                (Gender == NaturalLanguageTools.GetGender(Description, Assembly.Culture)) ? null :
                                new XAttribute("Gender", Gender.ToString()),

                                !Options.IsSetAssert(DescriptionOptions.Members, Type) ? null :
                                (from m in GetMembers(Type)
                                 where DescriptionManager.OnShouldLocalizeMember(m)
                                 orderby m.Name
                                 let value = Members.TryGetC(m.Name)
                                             where value != null && !(Assembly.IsDefault && (DescriptionManager.DefaultMemberDescription(m) == value))
                                             select new XElement("Member", new XAttribute("Name", m.Name), new XAttribute("Description", value)))
                                ));
        }
Beispiel #3
0
        public static string NicePluralName(this Type type)
        {
            type = CleanType(type);

            var result = Fallback(type, lt => lt.PluralDescription);

            if (result != null)
            {
                return(result);
            }

            return(type.GetCustomAttribute <PluralDescriptionAttribute>()?.PluralDescription ??
                   NaturalLanguageTools.Pluralize(DefaultTypeDescription(type)));
        }
Beispiel #4
0
        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);
        }