public static Language FindLanguage(string languageIdentifier, Database db) { Language toLanguage; if (languageIdentifier.Length == 2) { toLanguage = Language.OneWhere(c => c.ISO6391 == languageIdentifier, db); } else if (languageIdentifier.Length == 3) { toLanguage = Language.OneWhere(c => c.ISO6392 == languageIdentifier, db); } else { toLanguage = Language.OneWhere(c => c.EnglishName == languageIdentifier, db); if (toLanguage == null) { OtherName otherName = OtherName.FirstOneWhere(c => c.Value == languageIdentifier, db); if (otherName != null) { toLanguage = otherName.LanguageOfLanguageId; } } } return(toLanguage ?? Language.Default); }
/// <summary> /// Retrieves language information from https://www.loc.gov/standards/iso639-2/php/code_list.php /// and stores it in the specified languageDb /// </summary> /// <param name="languageDb"></param> /// <param name="onSaved"></param> /// <param name="onExists"></param> /// <param name="onOtherNameSaved"></param> /// <param name="onOtherNameExists"></param> public static bool?EnsureLanguages(Database languageDb, Action <Language> onSaved = null, Action <Language> onExists = null, Action <OtherName> onOtherNameSaved = null, Action <OtherName> onOtherNameExists = null) { return(_retrieveLock.DoubleCheckLock(ref _languagesRetrieved, () => { CQ cq = CQ.Create(Http.Get("https://www.loc.gov/standards/iso639-2/php/code_list.php")); cq = cq.Remove("script"); bool first = true; cq["tr", cq["table[width='100%'][cellspacing='0'][cellpadding='4'][border='1']"]].Each((Action <IDomObject>)(row => { if (!first) { string isoCode = cq["td", row][0].InnerText.DelimitSplit(" ")[0]; Language language = Language.OneWhere(c => c.ISO6392 == isoCode, languageDb); if (language == null) { language = new Language(); language.Uuid = Guid.NewGuid().ToString(); language.ISO6392 = isoCode; language.ISO6391 = cq["td", row][1].InnerText; string[] englishNames = cq["td", row][2].InnerText.DelimitSplit(";"); string[] frenchNames = cq["td", row][3].InnerText.DelimitSplit(";"); string[] germanNames = cq["td", row][4].InnerText.DelimitSplit(";"); language.EnglishName = englishNames.Length > 0 ? englishNames[0] : ""; language.FrenchName = frenchNames.Length > 0 ? frenchNames[0].Or(" ") : ""; language.GermanName = germanNames.Length > 0 ? germanNames[0].Or(" ") : ""; language.Save(languageDb); if (onSaved != null) { onSaved(language); } if (englishNames.Length > 1) { SetOtherNames(languageDb, language, englishNames, "English"); } if (frenchNames.Length > 1) { SetOtherNames(languageDb, language, frenchNames, "French"); } if (germanNames.Length > 1) { SetOtherNames(languageDb, language, germanNames, "German"); } } else { if (onExists != null) { onExists(language); } } } first = false; })); return true; })); }