Esempio n. 1
0
		static StandardSubtags()
		{
			// 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];
			}

			var languages = new List<LanguageSubtag>();
			var scripts = new List<ScriptSubtag>();
			var regions = new List<RegionSubtag>();
			var variants = new List<VariantSubtag>();
			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 = subtag;
						languages.Add(new LanguageSubtag(subtag, description, false, iso3Code));
						break;
					case "script":
						scripts.Add(new ScriptSubtag(subtag, description, false));
						break;
					case "region":
						regions.Add(new RegionSubtag(subtag, description, false));
						break;
					case "variant":
						variants.Add(new VariantSubtag(subtag, description, false, GetVariantPrefixes(subTagComponents)));
						break;
				}
			}

			IEnumerable<LanguageSubtag> sortedLanguages = languages.OrderBy(l => Regex.Replace(l.Name, @"[^\w]", ""))
				.Concat(new[] {new LanguageSubtag(WellKnownSubtags.UnlistedLanguage, "Language Not Listed", true, string.Empty)});
			RegisteredLanguages = new ReadOnlyKeyedCollection<string, LanguageSubtag>(new KeyedList<string, LanguageSubtag>(sortedLanguages, l => l.Code, StringComparer.InvariantCultureIgnoreCase));
			RegisteredScripts = new ReadOnlyKeyedCollection<string, ScriptSubtag>(new KeyedList<string, ScriptSubtag>(scripts.OrderBy(s => s.Name), s => s.Code, StringComparer.InvariantCultureIgnoreCase));
			RegisteredRegions = new ReadOnlyKeyedCollection<string, RegionSubtag>(new KeyedList<string, RegionSubtag>(regions.OrderBy(r => r.Name), r => r.Code, StringComparer.InvariantCultureIgnoreCase));
			RegisteredVariants = new ReadOnlyKeyedCollection<string, VariantSubtag>(new KeyedList<string, VariantSubtag>(variants.OrderBy(v => v.Name), v => v.Code, StringComparer.InvariantCultureIgnoreCase));
			CommonPrivateUseVariants = new ReadOnlyKeyedCollection<string, VariantSubtag>(new KeyedList<string, VariantSubtag>(new[]
			{
				new VariantSubtag(WellKnownSubtags.IpaPhoneticPrivateUse, "Phonetic"),
				new VariantSubtag(WellKnownSubtags.IpaPhonemicPrivateUse, "Phonemic"),
				new VariantSubtag(WellKnownSubtags.AudioPrivateUse, "Audio")
			}, v => v.Code, StringComparer.InvariantCultureIgnoreCase));

			Iso3Languages = RegisteredLanguages.Where(l => !string.IsNullOrEmpty(l.Iso3Code)).ToDictionary(l => l.Iso3Code, StringComparer.InvariantCultureIgnoreCase);
		}