Exemplo n.º 1
0
 private Dictionary <string, string> GetCombinePatterns()
 {
     if (this.combinePatterns == null)
     {
         XElement dateTimeFormats = root.XPathSelectElement("dateTimeFormats");
         if (CldrUtility.IsAlias(dateTimeFormats, out string calendar))
         {
             this.combinePatterns = Resolve(locale, calendar).GetCombinePatterns();
         }
         else
         {
             Dictionary <string, string> combinePatterns = new Dictionary <string, string>();
             foreach (XElement child in dateTimeFormats.Elements("dateTimeFormatLength"))
             {
                 combinePatterns[child.Attribute("type").Value] = Regex.Replace(child.XPathSelectElement("dateTimeFormat/pattern").Value, "'([^']*)'", m => {
                     return(m.Length == 2 ? "'" : m.Groups[1].Value);
                 });
             }
             if (combinePatterns.Count < 4)
             {
                 CldrUtility.CopyPatternFromParent(combinePatterns, this.Parent.GetCombinePatterns());
             }
             this.combinePatterns = combinePatterns;
         }
     }
     return(this.combinePatterns);
 }
Exemplo n.º 2
0
        public ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > GetCompactNotationFormats(NumberCompactDisplayFormat style)
        {
            XElement decimalFormats = elements.FirstOrDefault(v => v.Name.LocalName == "decimalFormats");

            if (decimalFormats == null)
            {
                return(this.Parent.GetCompactNotationFormats(style));
            }
            if (CldrUtility.IsAlias(decimalFormats, out string numberSystem))
            {
                return(GetFormat(locale, numberSystem).GetCompactNotationFormats(style));
            }
            Dictionary <PluralCategories, Dictionary <int, NumberFormatPattern> > dict = new Dictionary <PluralCategories, Dictionary <int, NumberFormatPattern> >();

            foreach (PluralCategories kind in pluralRules.EnumerateCategories())
            {
                dict[kind] = new Dictionary <int, NumberFormatPattern>();
            }
            XElement decimalFormat = decimalFormats.XPathSelectElement(String.Format("decimalFormatLength[@type = '{0}']/decimalFormat", IntlProviderOptions.ToStringValue(style)));

            foreach (XElement pattern in decimalFormat.XPathSelectElements("pattern"))
            {
                PluralCategories count = IntlProviderOptions.ParseEnum <PluralCategories>(pattern.Attribute("count").Value);
                dict[count][pattern.Attribute("type").Value.Length - 1] = ParseNumberFormatPattern(pattern.Value);
            }
            if (decimalFormat.Attribute("inherits") != null)
            {
                ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > parent = this.Parent.GetCompactNotationFormats(style);
                foreach (KeyValuePair <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > e in parent)
                {
                    CldrUtility.CopyPatternFromParent(dict[e.Key], e.Value);
                }
            }
            return(new ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> >(dict.ToDictionary(v => v.Key, v => new ReadOnlyDictionary <int, NumberFormatPattern>(v.Value))));
        }
Exemplo n.º 3
0
        private ReadOnlyDictionary <PluralCategories, string> GetUnitSubFormat(string unit, UnitDisplayFormat format, bool perUnit = false)
        {
            XDocument units      = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.units.xml.gz");
            XElement  unitLength = units.XPathSelectElement(String.Format("/root/units[@locale = '{0}']/unitLength[@type = '{1}']", locale, IntlProviderOptions.ToStringValue(format)));

            if (unitLength == null)
            {
                return(this.Parent.GetUnitSubFormat(unit, format));
            }
            if (CldrUtility.IsAlias(unitLength, out string type))
            {
                return(GetUnitSubFormat(unit, IntlProviderOptions.ParseEnum <UnitDisplayFormat>(type), perUnit));
            }
            bool hasInheritedValues = unitLength.Attribute("inherits") != null;

            if (unit == "per")
            {
                XElement perCompountUnit = unitLength.XPathSelectElement("compoundUnit[@type = 'per']/compoundUnitPattern");
                if (perCompountUnit == null)
                {
                    return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
                }
                return(CreateSingleResult(perCompountUnit.Value));
            }
            XElement unitElm = unitLength.XPathSelectElement(String.Format("unit[substring-after(@type, '-') = '{0}']", unit));

            if (unitElm == null)
            {
                return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
            }
            if (perUnit)
            {
                XElement perUnitPattern = unitElm.XPathSelectElement("perUnitPattern");
                if (perUnitPattern == null)
                {
                    return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
                }
                return(CreateSingleResult(perUnitPattern.Value));
            }
            Dictionary <PluralCategories, string> dict = new Dictionary <PluralCategories, string>();

            foreach (XElement unitPattern in unitElm.XPathSelectElements("unitPattern"))
            {
                dict[IntlProviderOptions.ParseEnum <PluralCategories>(unitPattern.Attribute("count").Value)] = unitPattern.Value;
            }
            if (unitElm.Attribute("inherits") != null)
            {
                ReadOnlyDictionary <PluralCategories, string> parent = this.Parent.GetUnitSubFormat(unit, format);
                CldrUtility.CopyPatternFromParent(dict, parent);
            }
            return(new ReadOnlyDictionary <PluralCategories, string>(dict));
        }
Exemplo n.º 4
0
        public static CldrRelativeTimeFormat Resolve(string locale, string type)
        {
            string key = locale + "/" + type;

            if (resolvedPatterns.TryGetValue(key, out CldrRelativeTimeFormat cached))
            {
                return(cached);
            }
            CldrRelativeTimeFormat formatter = new CldrRelativeTimeFormat(locale, type);
            XElement field = xDocument.XPathSelectElement(String.Format("/root/fields[@locale = '{0}']/field[@type = '{1}']", locale, type));

            if (field == null)
            {
                throw new InvalidOperationException("Unknown locale or type");
            }
            if (CldrUtility.IsAlias(field, out string use))
            {
                return(resolvedPatterns.GetOrAdd(key, Resolve(locale, use)));
            }
            bool hasInheritedValues = field.Attribute("inherits") != null;

            foreach (XElement relative in field.Elements("relative"))
            {
                int amount = Int32.Parse(relative.Attribute("type").Value);
                formatter.relative[amount] = FormattedString.Parse(relative.Value);
            }
            foreach (XElement relativeTime in field.Elements("relativeTime"))
            {
                Dictionary <PluralCategories, FormattedString> dict = relativeTime.Attribute("type").Value == "future" ? formatter.future : formatter.past;
                foreach (XElement child in relativeTime.Elements())
                {
                    PluralCategories category = IntlProviderOptions.ParseEnum <PluralCategories>(child.Attribute("count").Value);
                    dict[category] = FormattedString.Parse(child.Value);
                }
                hasInheritedValues |= relativeTime.Attribute("inherits") != null;
            }
            if (hasInheritedValues)
            {
                CldrRelativeTimeFormat parent = CldrUtility.GetParentPatterns(locale, type, Resolve);
                CldrUtility.CopyPatternFromParent(formatter.relative, parent.relative);
                CldrUtility.CopyPatternFromParent(formatter.future, parent.future);
                CldrUtility.CopyPatternFromParent(formatter.past, parent.past);
            }
            return(resolvedPatterns.GetOrAdd(key, formatter));
        }
Exemplo n.º 5
0
 private Dictionary <string, string[]> GetLabels(int type, string sectionName, string path, Dictionary <string, string> keyMapping, string[] types = null)
 {
     if (this.labels[type] == null)
     {
         XElement section = root.XPathSelectElement(sectionName);
         if (section == null)
         {
             this.labels[type] = this.Parent.GetLabels(type, sectionName, path, keyMapping, types);
         }
         else if (CldrUtility.IsAlias(section, out string calendar))
         {
             this.labels[type] = Resolve(locale, calendar).GetLabels(type, sectionName, path, keyMapping, types);
         }
         else
         {
             bool hasInheritedValues            = false;
             Dictionary <string, string>   copy = new Dictionary <string, string>();
             Dictionary <string, string[]> dict = new Dictionary <string, string[]>();
             foreach (XElement child in section.XPathSelectElements(path))
             {
                 string labelType = keyMapping[child.Attribute("type")?.Value ?? child.Name.LocalName];
                 if (CldrUtility.IsAlias(child, out string use))
                 {
                     copy[labelType] = keyMapping[use];
                 }
                 else
                 {
                     IEnumerable <XElement> elements = child.XPathSelectElements("*[not(@alt)]");
                     string[] labels = (types == null ? elements : types.Select(v => elements.FirstOrDefault(w => w.Attribute("type").Value == v))).Select(v => v?.Value).ToArray();
                     hasInheritedValues |= labels.Contains(null);
                     dict[labelType]     = labels;
                 }
             }
             foreach (KeyValuePair <string, string> e in copy)
             {
                 dict[e.Key] = dict[e.Value];
             }
             if (dict.Count < 3 || hasInheritedValues)
             {
                 Dictionary <string, string[]> parent = this.Parent.GetLabels(type, sectionName, path, keyMapping, types);
                 if (locale != "root")
                 {
                     if (dict.Count == 0)
                     {
                         dict = parent;
                     }
                     else
                     {
                         if (dict.Count < 3)
                         {
                             CldrUtility.CopyPatternFromParent(dict, parent);
                         }
                     }
                 }
                 if (!dict.ContainsKey("narrow"))
                 {
                     dict["narrow"] = dict.ContainsKey("short") ? dict["short"] : dict["long"];
                 }
                 if (!dict.ContainsKey("short"))
                 {
                     dict["short"] = dict.ContainsKey("narrow") ? dict["narrow"] : dict["long"];
                 }
                 if (!dict.ContainsKey("long"))
                 {
                     dict["long"] = dict.ContainsKey("short") ? dict["short"] : dict["narrow"];
                 }
                 if (hasInheritedValues)
                 {
                     foreach (KeyValuePair <string, string[]> e in dict)
                     {
                         CldrUtility.CopyPatternFromParent(e.Value, parent[e.Key]);
                     }
                 }
             }
             this.labels[type] = dict;
         }
     }
     return(this.labels[type]);
 }