private string GetLanguageCountry(Icu.Locale locale) { if (string.IsNullOrEmpty(locale.Country) && string.IsNullOrEmpty(locale.Language)) { return(string.Empty); } return(locale.Language + "_" + locale.Country); }
public IcuBreakIterator(Icu.BreakIterator.UBreakIteratorType type, CultureInfo locale) { if (locale == null) { throw new ArgumentNullException("locale"); } this.locale = new Icu.Locale(locale.Name); this.type = type; }
/// <summary> /// Get the language name in the indicated language if possible. [n.b., it isn't without ICU or something similar] /// Otherwise, get the name in the language itself if possible. /// It that doesn't work, return the English name. /// If we don't know even that, return the code as the name. /// </summary> /// <remarks> /// This method is called by the CollectionSettings dialog to get the language names displayed /// on two on its tabs for the vernacular, national, and regional languages. Returning the /// English name (from the Ethnologue data via SIL.Windows.Forms.WritingSystems.LanguageLookupModel) /// is the best we can do for most minor languages and what was done for all languages before /// this method was written. Unfortunately, the name and inputs of this method remain more of /// an aspiration than a reality. But returning the names of languages that the system (or /// our code) does know about in the languages themselves is less objectional (as in less /// blatantly ethnocentric) than always returning the English name for every language. /// This method is also called by the WorkspaceView class to get the English names of /// languages. /// </remarks> public static string GetLocalizedLanguageName(this LanguageLookupModel isoModel, string code, string uiCode) { #if USING_ICU // ICU is cheaply available on Linux, but very expensive on Windows (adds ~28MB to the Bloom installer). // But it's the only solution that doesn't seem to require coding up our own generalized fix. // I'm leaving the code here in case a workable Windows solution to providing ICU is found. // But we want identical behavior as much as possible on both platforms, so this code is not // used on Linux either. string icuCode = code.Replace("-", "_"); string icuDisplayCode = uiCode.Replace("-", "_"); Icu.Locale locale; if (!_mapCodeToIcuLocale.TryGetValue(icuCode, out locale)) { locale = new Icu.Locale(icuCode); _mapCodeToIcuLocale.Add(icuCode, locale); } Icu.Locale displayLocale; if (!_mapCodeToIcuLocale.TryGetValue(icuDisplayCode, out displayLocale)) { displayLocale = new Icu.Locale(icuDisplayCode); _mapCodeToIcuLocale.Add(icuDisplayCode, displayLocale); } return(locale.GetDisplayName(displayLocale)); #endif var key = new Tuple <string, string>(code, uiCode); string langName; if (_mapIsoCodesToLanguageName.TryGetValue(key, out langName)) { return(langName); } var generalUiCode = GetGeneralCode(uiCode); try { var generalCode = GetGeneralCode(code); var ci = CultureInfo.GetCultureInfo(generalCode); // We are depending on CultureInfo.DisplayName returning the language name in the current // UI language implicitly. (The current UI language should match uiCode.) // CultureInfo.DisplayName is known to be broken in Mono as it always returns EnglishName. // I can't tell that Windows behaves any differently, but maybe it does if the system is // installed as a Spanish language, French language, or whatever language, system. // If DisplayName does not do what we want (but returns the same as EnglishName), then // we return just NativeName if it exists. This seems less objectionable (ethnocentric) // than returning the EnglishName value. langName = ci.DisplayName; if (langName == ci.EnglishName && generalUiCode != "en") { langName = FixBotchedNativeName(ci.NativeName); } if (String.IsNullOrWhiteSpace(langName)) { langName = ci.EnglishName; } if (!ci.EnglishName.StartsWith("Unknown Language")) // Windows .Net behavior { _mapIsoCodesToLanguageName.Add(key, langName); return(langName); } } catch (Exception e) { // ignore exception, but log on terminal. Console.WriteLine(@"GetLocalizedLanguageName ignoring exception: {0}", e.Message); } // We get here after either an exception was thrown or the returned CultureInfo // helpfully told us it is for an unknown language (instead of throwing). // Handle a few languages that we do know the English and native names for, // and that are being localized for Bloom. langName = GetNativeNameIfKnown(code); if (generalUiCode == "en" || String.IsNullOrWhiteSpace(langName)) { langName = GetEnglishNameIfKnown(isoModel, code); } if (String.IsNullOrWhiteSpace(langName)) { langName = code; } _mapIsoCodesToLanguageName.Add(key, langName); return(langName); }