Beispiel #1
0
        public static string GetCanonicalRegionName(string value, string lang)
        {
            Guard.ArgumentNotNull(value, "value");
            Guard.ArgumentNotNull(lang, "lang");
            Dictionary <string, string> cached = aliasLookup.GetOrAdd("territory", _ => {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                XDocument supplementalData       = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.supplementalMetadata.xml.gz");
                foreach (XElement elm in supplementalData.XPathSelectElements("/supplementalData/metadata/alias/territoryAlias"))
                {
                    string from = elm.Attribute("type").Value.ToLowerInvariant();
                    string to   = elm.Attribute("replacement").Value;
                    dict[from]  = to;
                }
                return(dict);
            });

            if (cached.TryGetValue(value.ToLowerInvariant(), out string canonical))
            {
                if (canonical.IndexOf(' ') <= 0)
                {
                    return(canonical);
                }
                Dictionary <string, BcpLanguageTag> likelySubtags = EnsureLikelySubtagData();
                string[] candidates = canonical.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string candidate in candidates)
                {
                    if (likelySubtags.TryGetValue(String.Concat("und-", candidate), out BcpLanguageTag tag) && tag.Language == lang)
                    {
                        return(candidate);
                    }
                }
                return(candidates[0]);
            }
            return(value.ToUpperInvariant());
        }
Beispiel #2
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));
        }
Beispiel #3
0
 private static Dictionary <string, BcpLanguageTag> EnsureLikelySubtagData()
 {
     if (likelySubtags == null)
     {
         Dictionary <string, BcpLanguageTag> map = new Dictionary <string, BcpLanguageTag>();
         XDocument xDocument = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.likelySubtags.xml.gz");
         foreach (XElement elm in xDocument.XPathSelectElements("/supplementalData/likelySubtags/likelySubtag"))
         {
             map[elm.Attribute("from").Value] = Parse(elm.Attribute("to").Value);
         }
         likelySubtags = map;
     }
     return(likelySubtags);
 }
Beispiel #4
0
        public static string GetCanonicalVariantName(string value)
        {
            Guard.ArgumentNotNull(value, "value");
            Dictionary <string, string> cached = aliasLookup.GetOrAdd("variant", _ => {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                XDocument supplementalData       = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.supplementalMetadata.xml.gz");
                foreach (XElement elm in supplementalData.XPathSelectElements("/supplementalData/metadata/alias/variantAlias"))
                {
                    string from = elm.Attribute("type").Value.ToLowerInvariant().Replace('_', '-');
                    string to   = elm.Attribute("replacement").Value.Replace('_', '-');
                    dict[from]  = to;
                }
                return(dict);
            });

            return(cached.TryGetValue(value.ToLowerInvariant(), out string canonical) ? canonical : value.ToLowerInvariant());
        }
Beispiel #5
0
 private static DateTime[] GetEraStartDates()
 {
     if (eras == null)
     {
         XDocument       doc    = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.supplementalData.xml.gz");
         List <DateTime> values = new List <DateTime>();
         foreach (XElement era in doc.XPathSelectElements("/supplementalData/calendarData/calendar[@type = 'japanese']/eras/era"))
         {
             try {
                 values.Add(DateTime.Parse(era.Attribute("start").Value));
             } catch {
                 values.Add(new DateTime(Int32.Parse(era.Attribute("start").Value.Substring(0, 4)), 3, 1));
             }
         }
         eras = values.ToArray();
     }
     return(eras);
 }
Beispiel #6
0
        public static CldrPluralRules Resolve(PluralRuleType type, string locale)
        {
            string normalizedLocale = IntlUtility.RemoveUnicodeExtensions(locale);
            ConcurrentDictionary <string, CldrPluralRules> cache = type == PluralRuleType.Cardinal ? cardinalRules : ordinalRules;

            return(cache.GetOrAdd(normalizedLocale, _ => {
                XDocument doc = CldrUtility.LoadXml(type == PluralRuleType.Cardinal ? CardinalFileName : OrdinalFileName);
                foreach (XElement node in doc.XPathSelectElements(Xpath))
                {
                    string[] locales = node.Attribute("locales").Value.Split(' ');
                    if (locales.Contains(normalizedLocale))
                    {
                        CldrPluralRules rule = new CldrPluralRules(node);
                        foreach (string v in locales)
                        {
                            cache.TryAdd(v, rule);
                        }
                        return rule;
                    }
                }
                return Resolve(type, CldrUtility.GetParentLocale(normalizedLocale));
            }));
        }
Beispiel #7
0
        public static string GetCanonicalExtensionValue(string key, string value)
        {
            Guard.ArgumentNotNull(key, "key");
            Guard.ArgumentNotNull(value, "value");
            Dictionary <string, string> cached = aliasLookup.GetOrAdd(key.ToLowerInvariant(), k => {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                if (k == "sd" || k == "rg")
                {
                    XDocument supplementalData = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.supplementalData.xml.gz");
                    foreach (XElement elm in supplementalData.XPathSelectElements("/supplementalData/metadata/alias/subdivisionAlias"))
                    {
                        string from = elm.Attribute("type").Value.ToLowerInvariant().Replace('_', '-');
                        string to   = elm.Attribute("replacement").Value.Replace('_', '-');
                        dict[from]  = to;
                    }
                }
                else
                {
                    XDocument bcp47 = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.bcp47.xml.gz");
                    foreach (XElement elm in bcp47.XPathSelectElements(String.Format("/ldmlBCP47/keyword/key[@name = '{0}']/type[@alias or @deprecated]", k)))
                    {
                        if (elm.Attribute("deprecated") != null)
                        {
                            dict[elm.Attribute("name").Value.ToLowerInvariant()] = elm.Attribute("preferred").Value;
                        }
                        else
                        {
                            dict[elm.Attribute("alias").Value.ToLowerInvariant()] = elm.Attribute("name").Value;
                        }
                    }
                }
                return(dict);
            });

            return(cached.TryGetValue(value.ToLowerInvariant(), out string canonical) ? canonical : value);
        }