Esempio n. 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);
         }
     }
 }
Esempio n. 2
0
        private static bool Validate(Match match, BcpLanguageTagOptions options)
        {
            if (!match.Success)
            {
                return(false);
            }
            if ((options & BcpLanguageTagOptions.AllowLegacy) == 0 && match.Groups["legacy"].Success)
            {
                return(false);
            }
            if (match.Groups["transformed_extensions"].Captures.Count > 1 ||
                match.Groups["unicode_locale_extensions"].Captures.Count > 1)
            {
                return(false);
            }
            HashSet <string> visitedKeys = new HashSet <string>();

            foreach (Capture capture in match.Groups["other_extensions"].Captures)
            {
                string key = capture.Value.Substring(1, 1).ToLowerInvariant();
                if (!visitedKeys.Add(key))
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 3
0
 private BcpLanguageTag(string locale, BcpLanguageTagOptions options, Match match)
 {
     Guard.ArgumentNotNull(match, "match");
     if (!Validate(match, options))
     {
         throw new FormatException(String.Format("'{0}' is not a value BCP-47 language tag", locale));
     }
     PopulateFromMatchResult(match, options);
 }
Esempio n. 4
0
        public static BcpLanguageTag Parse(string locale, BcpLanguageTagOptions options)
        {
            Guard.ArgumentNotNull(locale, "locale");
            string key = locale.ToLowerInvariant().Replace('_', '-');

            if (options == 0 && canonicalCache.TryGetValue(key, out BcpLanguageTag cached))
            {
                return(cached);
            }
            return(new BcpLanguageTag(locale, options));
        }
Esempio n. 5
0
        public static bool TryParse(string locale, BcpLanguageTagOptions options, out BcpLanguageTag tag)
        {
            Guard.ArgumentNotNull(locale, "locale");
            string key = locale.ToLowerInvariant().Replace('_', '-');

            if (options == 0 && canonicalCache.TryGetValue(key, out BcpLanguageTag cached))
            {
                tag = cached;
                return(true);
            }
            Match match = bcp47re.Match(locale);

            if (!Validate(match, options))
            {
                tag = null;
                return(false);
            }
            tag = new BcpLanguageTag(locale, options, match);
            if (tag.IsCanonical)
            {
                canonicalCache.TryAdd(key, tag);
            }
            return(true);
        }
Esempio n. 6
0
 public BcpLanguageTagBuilder(string baseName, BcpLanguageTagOptions options)
     : this(BcpLanguageTag.Parse(baseName, options), options)
 {
 }
Esempio n. 7
0
 private BcpLanguageTag(string locale, BcpLanguageTagOptions options)
     : this(locale, options, bcp47re.Match(locale))
 {
 }
Esempio n. 8
0
        private void PopulateFromMatchResult(Match match, BcpLanguageTagOptions options)
        {
            HashSet <string>            variants   = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            HashSet <string>            attributes = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            Dictionary <string, string> ukeys      = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            Dictionary <string, string> tkeys      = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            Dictionary <string, string> singletons = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            Group languageId = match.Groups["unicode_language_id"];

            if (languageId.Value == "root")
            {
                this.Language = "root";
                this.Script   = "";
                this.Region   = "";
            }
            else
            {
                Group languageSubtag = match.Groups["unicode_language_subtag"];
                Group scriptSubtag   = match.Groups["unicode_script_subtag"];
                Group regionSubtag   = match.Groups["unicode_region_subtag"];
                Group variantSubtag  = match.Groups["unicode_variant_subtag"];
                this.Language = languageSubtag.Success ? languageSubtag.Value.ToLowerInvariant() : "";
                this.Script   = scriptSubtag.Success ? ToTitleCase(scriptSubtag.Value) : "";
                this.Region   = regionSubtag.Success ? regionSubtag.Value.ToUpperInvariant() : "";
                if (variantSubtag.Success)
                {
                    foreach (Capture item in variantSubtag.Captures)
                    {
                        variants.Add(item.Value.ToLowerInvariant());
                    }
                }
            }
            Group tExtensions = match.Groups["transformed_extensions"];
            Group uExtensions = match.Groups["unicode_locale_extensions"];

            if (tExtensions.Success)
            {
                Group tlang = match.Groups["tlang"];
                if (tlang.Success)
                {
                    this.TLang = tlang.Value;
                }
                foreach (Capture field in match.Groups["tfield"].Captures)
                {
                    string key = field.Value.Substring(0, 2).ToLowerInvariant();
                    if (!tkeys.ContainsKey(key))
                    {
                        tkeys.Add(key, ToLowerCaseHyphenated(field.Value.Substring(3)));
                    }
                }
            }
            if (this.TLang == null)
            {
                this.TLang = "";
            }
            if (uExtensions.Success)
            {
                Group uattr = match.Groups["uattr"];
                if (uattr.Success)
                {
                    foreach (Capture item in uattr.Captures)
                    {
                        attributes.Add(item.Value.ToLowerInvariant());
                    }
                }
                foreach (Capture field in match.Groups["ufield"].Captures)
                {
                    string key = field.Value.Substring(0, 2).ToLowerInvariant();
                    if (!ukeys.ContainsKey(key))
                    {
                        ukeys.Add(key, ToLowerCaseHyphenated(field.Value.Substring(3)));
                    }
                }
            }
            foreach (Capture ext in match.Groups["extensions"].Captures)
            {
                singletons.Add(ext.Value.Substring(1, 1).ToLowerInvariant(), ToLowerCaseHyphenated(ext.Value.Substring(3)));
            }
            if ((options & BcpLanguageTagOptions.IgnorePrivateExtensions) == 0)
            {
                Group privateExtensions = match.Groups["pu_extensions"];
                if (privateExtensions.Success)
                {
                    singletons.Add("x", ToLowerCaseHyphenated(privateExtensions.Value.Substring(3)));
                }
            }
            this.Variants         = new BcpSubtagCollection(variants, true);
            this.Attributes       = new BcpSubtagCollection(attributes, true);
            this.UExtensions      = new BcpSubtagDictionary(ukeys, true);
            this.TExtensions      = new BcpSubtagDictionary(tkeys, true);
            this.SingletonSubtags = new BcpSubtagDictionary(singletons, true);
        }