private static IKeyedCollection <string, SldrLanguageTagInfo> DeriveTagsFromJsonEntries(List <AllTagEntry> rootObject) { var tags = new KeyedList <string, SldrLanguageTagInfo>(info => info.LanguageTag, StringComparer.InvariantCultureIgnoreCase); foreach (AllTagEntry entry in rootObject) { // tags starting with x- have undefined structure so ignoring them // tags starting with _ showed up in buggy data so we'll drop them also if (!entry.tag.StartsWith("x-") && !entry.tag.StartsWith("_")) { LanguageSubtag languageTag; if (!entry.deprecated && !StandardSubtags.RegisteredLanguages.TryGet(entry.tag.Split('-')[0], out languageTag)) { if (entry.iso639_3 == null) { StandardSubtags.AddLanguage(entry.tag.Split('-')[0], entry.name, false, entry.tag.Split('-')[0]); } else if (!StandardSubtags.RegisteredLanguages.TryGet(entry.iso639_3, out languageTag)) { StandardSubtags.AddLanguage(entry.iso639_3, entry.name, false, entry.iso639_3); } } string implicitStringCode = null; // the script is always in the full tag string scriptCode = entry.full.Split('-')[1]; if (scriptCode.Length == 4) { var tagComponents = entry.tag.Split('-'); ScriptSubtag scriptTag; if (!StandardSubtags.RegisteredScripts.TryGet(scriptCode, out scriptTag)) { StandardSubtags.AddScript(scriptCode, scriptCode); } // if the script is also in the tag then it is explicit not implicit if (tagComponents.Length == 1 || tagComponents[1] != scriptCode) { implicitStringCode = scriptCode; } } tags.Add(new SldrLanguageTagInfo(entry.tag, implicitStringCode, entry.tag, entry.sldr)); } } return(tags); }
internal static IKeyedCollection <string, SldrLanguageTagInfo> ParseAllTagsJson(string allTagsContent) { // read in the json file /* { * "full": "aa-Latn-ET", * "name": "Afar", * "names": [ * "Adal", * ... * ], * "sldr": true, * "tag": "aa", * "tags": [ * "aa-ET", * "aa-Latn" * ] * },*/ // for each entry // tag -> langtag which is same as sldrtag // sldr -> isAvailable // tags -> process to find implicitScript var tags = new KeyedList <string, SldrLanguageTagInfo>(info => info.LanguageTag, StringComparer.InvariantCultureIgnoreCase); List <AllTagEntry> rootObject = JsonConvert.DeserializeObject <List <AllTagEntry> >(allTagsContent); foreach (AllTagEntry entry in rootObject) { if (!entry.tag.StartsWith("x-")) // tags starting with x- have undefined structure so ignoring them { LanguageSubtag languageTag; if (!entry.deprecated && !StandardSubtags.RegisteredLanguages.TryGet(entry.tag.Split('-')[0], out languageTag)) { if (entry.iso639_3 == null) { StandardSubtags.AddLanguage(entry.tag.Split('-')[0], entry.name, false, entry.tag.Split('-')[0]); } else if (!StandardSubtags.RegisteredLanguages.TryGet(entry.iso639_3, out languageTag)) { StandardSubtags.AddLanguage(entry.iso639_3, entry.name, false, entry.iso639_3); } } string implicitStringCode = null; // the script is always in the full tag string scriptCode = entry.full.Split('-')[1]; if (scriptCode.Length == 4) { var tagComponents = entry.tag.Split('-'); ScriptSubtag scriptTag; if (!StandardSubtags.RegisteredScripts.TryGet(scriptCode, out scriptTag)) { StandardSubtags.AddScript(scriptCode, scriptCode); } // if the script is also in the tag then it is explicit not implicit if (tagComponents.Length == 1 || tagComponents[1] != scriptCode) { implicitStringCode = scriptCode; } } tags.Add(new SldrLanguageTagInfo(entry.tag, implicitStringCode, entry.tag, entry.sldr)); } } return(tags); }
private bool AddLanguage(string code, string threelettercode, string full = null, string name = null, string localName = null, string region = null, List <string> names = null, string regions = null, List <string> tags = null) { string primarycountry; if (region == null) { primarycountry = ""; } else if (StandardSubtags.IsValidIso3166RegionCode(region)) { if (StandardSubtags.IsPrivateUseRegionCode(region)) { if (region == "XK") { primarycountry = "Kosovo"; } else { primarycountry = "Unknown private use"; } } else { primarycountry = StandardSubtags.RegisteredRegions[region].Name; // convert to full region name } } else { primarycountry = "Invalid region"; } LanguageInfo language = new LanguageInfo { LanguageTag = code, ThreeLetterTag = threelettercode, // DesiredName defaults to Names[0], which is set below. PrimaryCountry = primarycountry }; language.Countries.Add(primarycountry); if (regions != null) { string[] countries = regions.Split(); foreach (string country in countries) { if (!country.Contains('?') && country != "") { language.Countries.Add(StandardSubtags.RegisteredRegions[country].Name); } } } // For sorting, it is better to store name first instead of localName, which may be in a local script. // Names[0] is used in several ways in sorting languages in the list of possible matches: 1) bring // to the top of the list languages where Names[0] matches what the user typed, 2) order by the // "typing distance" of Names[0] from what the user typed, and 3) order by comparing the Names[0] // value of the two languages if neither matches the search string and their typing distances from // the search string are the same. // Names[1] (if it exists) is used to move the language toward the top of the list if it exactly // matches the search string. It is not used otherwise in the sorting heuristics. No other // values in the Names list are involved in the sorting process. if (name != null) { language.Names.Add(name.Trim()); } if (localName != null && localName != name) { language.Names.Add(localName.Trim()); } if (names != null) { foreach (string langname in names) { if (!language.Names.Contains(langname)) { language.Names.Add(langname.Trim()); } } } // If we end up needing to add the language code, that reflects a deficiency in the data. But // having a bogus name value probably hurts less that not having any name at all. The sort // process mentioned above using the language tag as well as the first two items in the Names list. Debug.Assert(language.Names.Count > 0); if (language.Names.Count == 0) { language.Names.Add(code); } // add language to _codeToLanguageIndex and _nameToLanguageIndex // if 2 letter code then add both 2 and 3 letter codes to _codeToLanguageIndex _codeToLanguageIndex[code] = language; if (full != null && !string.Equals(full, code)) { _codeToLanguageIndex[full] = language; // add the full expanded tag } if (threelettercode != null && !string.Equals(code, threelettercode)) { _codeToLanguageIndex[threelettercode] = language; } if (tags != null) { foreach (string langtag in tags) { _codeToLanguageIndex[langtag] = language; } } foreach (string langname in language.Names) { GetOrCreateListFromName(langname).Add(language); } // add to _countryToLanguageIndex foreach (var country in language.Countries) { if (!string.IsNullOrEmpty(country)) { List <LanguageInfo> list; if (!_countryToLanguageIndex.TryGetValue(country, out list)) { list = new List <LanguageInfo>(); _countryToLanguageIndex[country] = list; } list.Add(language); } } return(true); }