Esempio n. 1
0
 public static bool IsStandardIso3166Region(string regionCodeToCheck)
 {
     return(ValidIso3166Regions.Any(
                code => regionCodeToCheck.Equals(code.Subtag, StringComparison.OrdinalIgnoreCase)
                ));
 }
Esempio n. 2
0
        private static void LoadIanaSubtags()
        {
            // JohnT: can't find anywhere else to document this, so here goes: TwoToThreeMap is a file adapted from
            // FieldWorks Ethnologue\Data\iso-639-3_20080804.tab, by discarding all but the first column (3-letter
            // ethnologue codes) and the fourth (two-letter IANA codes), and all the rows where the fourth column is empty.
            // I then swapped the columns. So, in this resource, the string before the tab in each line is a 2-letter
            // Iana code, and the string after it is the one we want to return as the corresponding ISO3Code.
            // The following block of code assembles these lines into a map we can use to fill this slot properly
            // when building the main table.
            var TwoToThreeMap = new Dictionary <string, string>();

            string[] encodingPairs = LanguageRegistryResources.TwoToThreeCodes.Replace("\r\n", "\n").Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string pair in encodingPairs)
            {
                var items = pair.Split('\t');
                if (items.Length != 2)
                {
                    continue;
                }
                TwoToThreeMap[items[0]] = items[1];
            }

            string[] ianaSubtagsAsStrings = LanguageRegistryResources.ianaSubtagRegistry.Split(new[] { "%%" }, StringSplitOptions.None);
            foreach (string ianaSubtagAsString in ianaSubtagsAsStrings)
            {
                string[] subTagComponents = ianaSubtagAsString.Replace("\r\n", "\n").Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                if (subTagComponents[0].Contains("File-Date"))
                {
                    continue;                       //This is the first line of the file.
                }

                CheckIfIanaSubtagFromFileHasExpectedForm(subTagComponents);

                string type        = subTagComponents[0].Split(' ')[1];
                string subtag      = subTagComponents[1].Split(' ')[1];
                string description = SubTagComponentDescription(subTagComponents[2]);

                if (subtag.Contains(".."))                 // do not add private use subtags to the list
                {
                    continue;
                }

                /* Note: currently we are only using the first "Description:" line in each entry.
                 * A few script entries contain multiple Description: lines, as in the example below:
                 *
                 * Type: script
                 * Subtag: Deva
                 * Description: Devanagari
                 * Description: Nagari
                 * Added: 2005-10-16
                 *
                 * In the future it may be necessary to build a separate iana script entry collection
                 * that contains duplicate script codes, for the purposes of including all possible
                 * script Descriptions.
                 */
                switch (type)
                {
                case "language":

                    string iso3Code;
                    if (!TwoToThreeMap.TryGetValue(subtag, out iso3Code))
                    {
                        iso3Code = String.Empty;
                    }
                    ValidIso639LanguageCodes.Add(
                        new Iso639LanguageCode(subtag, description, iso3Code)
                        );
                    break;

                case "script":
                    ValidIso15924Scripts.Add(
                        new Iso15924Script(description, subtag)
                        );
                    break;

                case "region":
                    ValidIso3166Regions.Add(
                        new IanaSubtag(type, subtag, description)
                        );
                    break;

                case "variant":
                    ValidRegisteredVariants.Add(
                        new IanaVariantSubtag(type, subtag, description, GetVariantPrefixes(subTagComponents))
                        );
                    break;
                }
            }

            ValidIso639LanguageCodes.Sort(Iso639LanguageCode.CompareByName);
            ValidIso15924Scripts.Sort(Iso15924Script.CompareScriptOptions);
            ValidIso3166Regions.Sort(IanaSubtag.CompareByDescription);
            ValidRegisteredVariants.Sort(IanaSubtag.CompareByDescription);

            // Add Unlisted Language at the end
            ValidIso639LanguageCodes.Insert(ValidIso639LanguageCodes.Count, new Iso639LanguageCode("qaa", "Language Not Listed", String.Empty));

            // To help people find Latin as a script tag
            ValidIso15924Scripts.Insert(0, new Iso15924Script("Roman (Latin)", "Latn"));
        }