Exemple #1
0
        public static string GetParentLocale(string locale)
        {
            Guard.ArgumentNotNull(locale, "locale");
            locale = BcpLanguageTag.Parse(locale).Canonicalize().ToString();
            if (CldrUtility.parentLocales == null)
            {
                Dictionary <string, string> parentLocales = new Dictionary <string, string>();
                XDocument doc = LoadXml("Codeless.Ecma.Intl.Data.supplementalData.xml.gz");
                foreach (XElement elm in doc.XPathSelectElements("/supplementalData/parentLocales/parentLocale"))
                {
                    string parent = elm.Attribute("parent").Value;
                    foreach (string child in elm.Attribute("locales").Value.Split(' '))
                    {
                        parentLocales[child] = parent;
                    }
                }
                CldrUtility.parentLocales = parentLocales;
            }
            if (CldrUtility.parentLocales.TryGetValue(locale, out string p))
            {
                return(p);
            }
            int pos = locale.LastIndexOf('-');

            if (pos > 0)
            {
                return(locale.Substring(0, pos));
            }
            return(locale == "root" ? null : "root");
        }
Exemple #2
0
 private void OnChange(IDictionary <string, string> collection, string[] changedKeys)
 {
     if (!suppressChange)
     {
         try {
             suppressChange = true;
             if (collection == UExtensions)
             {
                 SingletonSubtags["u"] = FormatUExtension();
             }
             else if (collection == TExtensions)
             {
                 SingletonSubtags["t"] = FormatTExtension();
             }
             else
             {
                 if (Array.IndexOf(changedKeys, "t") >= 0 || Array.IndexOf(changedKeys, "T") >= 0)
                 {
                     TExtensions.Clear();
                     if (collection.TryGetValue("t", out string value) && !String.IsNullOrEmpty(value))
                     {
                         BcpLanguageTag t = BcpLanguageTag.Parse("root-t-" + value);
                         foreach (KeyValuePair <string, string> a in t.TExtensions)
                         {
                             TExtensions.Add(a);
                         }
                         TLang = t.TLang;
                     }
                     else
                     {
                         TLang = "";
                     }
                 }
                 if (Array.IndexOf(changedKeys, "u") >= 0 || Array.IndexOf(changedKeys, "U") >= 0)
                 {
                     UExtensions.Clear();
                     Attributes.Clear();
                     if (collection.TryGetValue("u", out string value) && !String.IsNullOrEmpty(value))
                     {
                         BcpLanguageTag t = BcpLanguageTag.Parse("root-u-" + value);
                         foreach (KeyValuePair <string, string> a in t.UExtensions)
                         {
                             UExtensions.Add(a);
                         }
                         foreach (string a in t.Attributes)
                         {
                             Attributes.Add(a);
                         }
                     }
                 }
             }
         } finally {
             suppressChange = false;
         }
     }
 }
Exemple #3
0
        public static string CanonicalizeLanguageTag(string locale)
        {
            BcpLanguageTag tag = BcpLanguageTag.Parse(locale);

            if (tag.SingletonSubtags.Count > 0)
            {
                if (tag.SingletonSubtags.Count > 1 || !tag.SingletonSubtags.ContainsKey("u"))
                {
                    BcpLanguageTagBuilder builder = new BcpLanguageTagBuilder(tag);
                    builder.SingletonSubtags.Clear();
                    builder.SingletonSubtags["u"] = tag.SingletonSubtags["u"];
                    return(builder.Canonicalize().ToString());
                }
            }
            return(tag.Canonicalize().ToString());
        }
Exemple #4
0
        public static string GetBestAvailableLocale(ICollection <string> availableLocales, ICollection <string> requestedLocales, LocaleMatcher lookupMatcher, out string extension)
        {
            Guard.ArgumentNotNull(availableLocales, "availableLocales");
            Guard.ArgumentNotNull(requestedLocales, "requestedLocales");
            ICollection <string> candidates = requestedLocales.Count == 0 ? new[] { IntlContext.DefaultLocale } : requestedLocales;

            foreach (string locale in candidates)
            {
                string normalized = RemoveUnicodeExtensions(locale);
                string matched    = GetBestAvailableLocale(availableLocales, normalized);
                extension = locale.Substring(normalized.Length);
                if (matched != null)
                {
                    return(matched);
                }
                if (lookupMatcher == LocaleMatcher.BestFit)
                {
                    BcpLanguageTagBuilder candidate = new BcpLanguageTagBuilder(BcpLanguageTag.Parse(normalized).Maximize());
                    matched = GetBestAvailableLocale(availableLocales, candidate.ToString());
                    if (matched == null && candidate.Script != "")
                    {
                        candidate.Script = "";
                        matched          = GetBestAvailableLocale(availableLocales, candidate.ToString());
                    }
                    if (matched != null)
                    {
                        return(matched);
                    }
                }
            }
            if (requestedLocales.Count > 0)
            {
                return(GetBestAvailableLocale(availableLocales, new string[0], lookupMatcher, out extension));
            }
            extension = "";
            return(availableLocales.First());
        }
Exemple #5
0
 public BcpLanguageTagBuilder(string baseName, BcpLanguageTagOptions options)
     : this(BcpLanguageTag.Parse(baseName, options), options)
 {
 }
Exemple #6
0
 public BcpLanguageTagBuilder(string baseName)
     : this(BcpLanguageTag.Parse(baseName), 0)
 {
 }