示例#1
0
 public BcpLanguageTagBuilder(BcpLanguageTag parent, BcpLanguageTagOptions options)
     : this()
 {
     Guard.ArgumentNotNull(parent, "parent");
     this.Language = parent.Language;
     this.Script   = parent.Script;
     this.Region   = parent.Region;
     foreach (string variant in parent.Variants)
     {
         this.Variants.Add(variant);
     }
     this.TLang = parent.TLang;
     foreach (KeyValuePair <string, string> e in parent.TExtensions)
     {
         this.TExtensions.Add(e);
     }
     foreach (string attribute in parent.Attributes)
     {
         this.Attributes.Add(attribute);
     }
     foreach (KeyValuePair <string, string> e in parent.UExtensions)
     {
         this.UExtensions.Add(e);
     }
     if ((options & BcpLanguageTagOptions.IgnorePrivateExtensions) == 0)
     {
         foreach (KeyValuePair <string, string> e in parent.SingletonSubtags)
         {
             this.SingletonSubtags.Add(e);
         }
     }
 }
示例#2
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");
        }
示例#3
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;
         }
     }
 }
示例#4
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());
        }
示例#5
0
            protected override bool Validate(string key, string value)
            {
                Guard.ArgumentNotNull(key, "key");
                Guard.ArgumentNotNull(value, "value");
                if (key.Length != 1 && !Char.IsLetterOrDigit(key[0]))
                {
                    throw new FormatException("Singleton must be a single letter or digit");
                }
                if (!String.IsNullOrEmpty(value))
                {
                    bool result;
                    switch (key[0])
                    {
                    case 'u':
                    case 'U':
                        result = BcpLanguageTag.IsValid("root-u-" + value);
                        break;

                    case 't':
                    case 'T':
                        result = BcpLanguageTag.IsValid("root-t-" + value);
                        break;

                    case 'x':
                    case 'X':
                        result = Regex.IsMatch(value, reXValue, RegexOptions.IgnoreCase);
                        break;

                    default:
                        result = Regex.IsMatch(value, reSValue, RegexOptions.IgnoreCase);
                        break;
                    }
                    if (!result)
                    {
                        throw new FormatException(String.Format("'{0}' is not a valid sequence for {1} singleton", value, key));
                    }
                }
                return(true);
            }
示例#6
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());
        }
示例#7
0
 public BcpLanguageTagBuilder(BcpLanguageTag parent)
     : this(parent, 0)
 {
 }
示例#8
0
 public BcpLanguageTagBuilder(string baseName, BcpLanguageTagOptions options)
     : this(BcpLanguageTag.Parse(baseName, options), options)
 {
 }
示例#9
0
 public BcpLanguageTagBuilder(string baseName)
     : this(BcpLanguageTag.Parse(baseName), 0)
 {
 }
示例#10
0
 public static bool IsStructurallyValidLanguageTag(string locale)
 {
     return(BcpLanguageTag.IsValid(locale));
 }