Exemple #1
0
        /// <summary>
        /// Initializes new value of <see cref="LanguageTag"/>
        /// </summary>
        private LanguageTag(
            Language?language,
            Script?script,
            Region?region,
            VariantCollection variants,
            ExtensionSubtagCollection extensions,
            ExtensionSubtag privateUse)
        {
            if (language != null && script != null)
            {
                var suppressScript = language.SuppressScript;
                if (suppressScript != null && suppressScript == script)
                {
                    script = null;
                }
            }

            Language   = language;
            Script     = script;
            Region     = region;
            Variants   = variants;
            Extensions = extensions;
            PrivateUse = privateUse;

            var fields = Field.None;

            if (language != null)
            {
                fields |= Field.Language;
            }
            if (script != null)
            {
                fields |= Field.Script;
            }
            if (region != null)
            {
                fields |= Field.Region;
            }
            if (!variants.IsEmpty)
            {
                fields |= Field.Variants;
            }
            if (!extensions.IsEmpty)
            {
                fields |= Field.Extensions;
            }
            if (!privateUse.IsEmpty)
            {
                if (!privateUse.PrivateUse)
                {
                    throw new ArgumentOutOfRangeException(nameof(privateUse), "must be a private use subtag");
                }
                fields |= Field.PrivateUse;
            }

            Fields = fields;
        }
Exemple #2
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, IEnumerable <Variant> variants, PrivateUseSubtags privateUseSubtags)
     : this(
         (Language?)lang,
         null,
         null,
         VariantCollection.Create(lang, null, variants),
         default,
         privateUseSubtags)
 {
 }
Exemple #3
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="variants">enumarate of variant subtags</param>
 public LanguageTag(Language lang, IEnumerable <Variant> variants)
     : this(
         (Language?)lang,
         null,
         null,
         VariantCollection.Create(lang, null, variants),
         default,
         default)
 {
 }
Exemple #4
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="region">region subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, Script?script, Region?region, IEnumerable <Variant> variants, IEnumerable <ExtensionSubtag> extSubtags, PrivateUseSubtags privateUseSubtags)
     : this(
         (Language?)lang,
         script,
         region,
         VariantCollection.Create(lang, script, variants),
         new ExtensionSubtagCollection(extSubtags),
         privateUseSubtags)
 {
 }
Exemple #5
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="region">region subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, Script?script, Region?region, IEnumerable <Variant> variants, PrivateUseSubtags privateUseSubtags)
     : this(
         (Language?)lang,
         script,
         region,
         VariantCollection.Create(lang, script, variants),
         default,
         privateUseSubtags)
 {
 }
Exemple #6
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 public LanguageTag(Language lang, Script?script, IEnumerable <Variant> variants, IEnumerable <ExtensionSubtag> extSubtags)
     : this(
         (Language?)lang,
         script,
         null,
         VariantCollection.Create(lang, script, variants),
         new ExtensionSubtagCollection(extSubtags),
         default)
 {
 }
Exemple #7
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="region">region subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 public LanguageTag(Language lang, Region?region, IEnumerable <Variant> variants, IEnumerable <ExtensionSubtag> extSubtags)
     : this(
         (Language?)lang,
         null,
         region,
         VariantCollection.Create(lang, null, variants),
         new ExtensionSubtagCollection(extSubtags),
         default)
 {
 }
Exemple #8
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, IEnumerable <Variant> variants, IEnumerable <ExtensionSubtag> extSubtags, PrivateUseSubtags privateUseSubtags)
     : this(
         (Language?)lang,
         null,
         null,
         VariantCollection.Create(lang, null, variants),
         new ExtensionSubtagCollection(extSubtags),
         privateUseSubtags)
 {
 }
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, IEnumerable <Variant>?variants, ExtensionSubtag privateUseSubtags)
     : this(
         lang,
         null,
         null,
         VariantCollection.Create(lang, null, variants),
         default,
         privateUseSubtags)
 {
 }
Exemple #10
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="region">region subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 public LanguageTag(Language lang, Script?script, Region?region, IEnumerable <Variant>?variants, IEnumerable <ExtensionSubtag>?extSubtags)
     : this(
         lang,
         script,
         region,
         VariantCollection.Create(lang, script, variants),
         new ExtensionSubtagCollection(extSubtags),
         default)
 {
 }
Exemple #11
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="region">region subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 public LanguageTag(Language lang, Script?script, Region?region, IEnumerable <Variant>?variants)
     : this(
         lang,
         script,
         region,
         VariantCollection.Create(lang, script, variants),
         default,
         default)
 {
 }
Exemple #12
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="script">script subtag</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 /// <param name="privateUseSubtags">private use subtag</param>
 public LanguageTag(Language lang, Script?script, IEnumerable <Variant>?variants, IEnumerable <ExtensionSubtag>?extSubtags, ExtensionSubtag privateUseSubtags)
     : this(
         lang,
         script,
         null,
         VariantCollection.Create(lang, script, variants),
         new ExtensionSubtagCollection(extSubtags),
         privateUseSubtags)
 {
 }
Exemple #13
0
 /// <summary>
 /// Initializes new value of <see cref="LanguageTag"/>
 /// </summary>
 /// <param name="lang">primary language</param>
 /// <param name="variants">enumarate of variant subtags</param>
 /// <param name="extSubtags">enumarate of extensions subtags</param>
 public LanguageTag(Language lang, IEnumerable <Variant>?variants, IEnumerable <ExtensionSubtag>?extSubtags)
     : this(
         lang,
         null,
         null,
         VariantCollection.Create(lang, null, variants),
         new ExtensionSubtagCollection(extSubtags),
         default)
 {
 }
        /// <summary>
        /// Initializes new value of <see cref="LanguageTag"/>
        /// </summary>
        private LanguageTag(
            Language?language,
            Script?script,
            Region?region,
            VariantCollection variants,
            ExtensionSubtagCollection extensions,
            PrivateUseSubtags privateUse)
        {
            if (language.HasValue && script.HasValue)
            {
                var suppressScript = language.Value.GetSuppressScript();
                if (suppressScript.HasValue && suppressScript.Value == script.Value)
                {
                    script = null;
                }
            }

            Language   = language;
            Script     = script;
            Region     = region;
            Variants   = variants;
            Extensions = extensions;
            PrivateUse = privateUse;

            var fields = Field.None;

            if (language.HasValue)
            {
                fields |= Field.Language;
            }
            if (script.HasValue)
            {
                fields |= Field.Script;
            }
            if (region.HasValue)
            {
                fields |= Field.Region;
            }
            if (!variants.IsEmpty)
            {
                fields |= Field.Variants;
            }
            if (!extensions.IsEmpty)
            {
                fields |= Field.Extensions;
            }
            if (!privateUse.IsEmpty)
            {
                fields |= Field.PrivateUse;
            }

            Fields = fields;
        }
        /// <summary>
        /// Tries to parse <see cref="LanguageTag"/> value from its text representation
        /// </summary>
        /// <param name="text">text representation of language tag</param>
        /// <param name="result">parsed value</param>
        /// <returns>true if parsing was successful, false otherwise</returns>
        public static bool TryParse(string text, out LanguageTag result)
        {
            var tokens = new TokenEnumerator(Grandfathered.GetPreferredValue(text) ?? text);

            if (tokens.TokenIs(PrivateUseSubtags.Singleton))
            {
                // This tag starts with private use subtags
                var immediatePrivateUseResult = PrivateUseSubtags.Parse(tokens);

                var error = immediatePrivateUseResult.NothingToParse || immediatePrivateUseResult.ErrorOccured ||
                            tokens.CurrentTokenAvailable;

                if (error)
                {
                    // But they are invalid
                    result = default;
                    return(false);
                }

                result = new LanguageTag(immediatePrivateUseResult.Result);
                return(true);
            }

            var language = tokens.TryParseLanguage();

            if (!language.HasValue)
            {
                // If language tag is not entirely private use subtags, it must contain language
                result = default;
                return(false);
            }

            // These two cannot produce error during parsing
            var script = tokens.TryParseScript();
            var region = tokens.TryParseRegion();

            // These three can
            var variantsResult = VariantCollection.Parse(language, script, tokens);

            if (variantsResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            var extensionsResult = ExtensionSubtagCollection.Parse(tokens);

            if (extensionsResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            var privateUseResult = PrivateUseSubtags.Parse(tokens);

            if (privateUseResult.ErrorOccured)
            {
                result = default;
                return(false);
            }

            if (tokens.CurrentTokenAvailable)
            {
                // Something left unparsed
                result = default;
                return(false);
            }

            var variants = variantsResult.NothingToParse ? default : variantsResult.Result;

                           var extensions = extensionsResult.NothingToParse ? default : extensionsResult.Result;

                                            var privateUse = privateUseResult.NothingToParse ? default : privateUseResult.Result;

                                                             result = new LanguageTag(language, script, region, variants, extensions, privateUse);
                                                             return(true);
        }