/// <summary> /// find the common parent locale, for example /// en-us, en-gb, has the same parent locale: en. /// and en-us, fr, has the no same parent locale. /// </summary> /// <param name="locale1">first locale.</param> /// <param name="locale2">second locale.</param> /// <returns>the most closest common ancestor local.</returns> private static string FindCommonAncestorLocale(string locale1, string locale2) { var policy = new LanguagePolicy(); if (!policy.ContainsKey(locale1) || !policy.ContainsKey(locale2)) { return(string.Empty); } var key1Policy = policy[locale1]; var key2Policy = policy[locale2]; foreach (var key1Language in key1Policy) { foreach (var key2Language in key2Policy) { if (key1Language == key2Language) { return(key1Language); } } } return(string.Empty); }
/// <summary> /// Get the fall back locale from the optional locales. for example /// en-us, is a locale from English. But the option locales has [en, ''], /// So,en would be picked. /// </summary> /// <param name="locale">current locale.</param> /// <param name="optionalLocales">option locales.</param> /// <returns>the final locale.</returns> public static string FallbackLocale(string locale, IList <string> optionalLocales) { if (optionalLocales == null) { throw new ArgumentNullException(nameof(optionalLocales)); } if (optionalLocales.Contains(locale)) { return(locale); } var languagePolicy = new LanguagePolicy(); if (languagePolicy.ContainsKey(locale)) { var fallbackLocals = languagePolicy[locale]; foreach (var fallbackLocal in fallbackLocals) { if (optionalLocales.Contains(fallbackLocal)) { return(fallbackLocal); } } } else if (optionalLocales.Contains(string.Empty)) { return(string.Empty); } throw new Exception($"there is no locale fallback for {locale}"); }