diff --git a/Logic/Support/Formatting.cs b/Logic/Support/Formatting.cs index 39e35d3e..54100f39 100644 --- a/Logic/Support/Formatting.cs +++ b/Logic/Support/Formatting.cs @@ -374,7 +374,7 @@ namespace Swarmops.Logic.Support { get { - return new string[] { "ar-AE", "de-DE", "yo-NG" /* Yoruba writes as Èbè Yoruba */, "el-GR" /* Greek writes as Ellenika */ , "es-ES", "es-VE", "fr-FR", "fil-PH", "it-IT", "nl-NL", "pl-PL", "pt-PT", "ru-RU", "sr-Cyrl-RS", "sr-Latn-RS", "sv-SE", "tr-TR", "zh-CN" }; + return new string[] { "ar-AE", "de-DE", "yo-NG" /* Yoruba writes as Èbè Yoruba */, "el-GR" /* Greek writes as Ellenika */ , "en-US", "es-ES", "es-VE", "fr-FR", "fil-PH", "it-IT", "nl-NL", "pl-PL", "pt-PT", "ru-RU", "sr-Cyrl-RS", "sr-Latn-RS", "sv-SE", "tr-TR", "zh-CN" }; // the above locales are sorted by the language NATIVE name, to make the list maximally useful } diff --git a/Site/Bases/CommonV5.cs b/Site/Bases/CommonV5.cs index ee4558bd..84d82860 100644 --- a/Site/Bases/CommonV5.cs +++ b/Site/Bases/CommonV5.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.Threading; @@ -19,9 +20,8 @@ namespace Swarmops.Frontend public static void CulturePreInit (HttpRequest request) { // Localization - // Set default culture (English, United States) - string preferredCulture = "en-US"; + string preferredCulture = "en-US"; // default // ----------- SET CULTURE ------------ @@ -30,27 +30,61 @@ namespace Swarmops.Frontend if (request.Cookies["PreferredCulture"] != null) { // Yes, set it - preferredCulture = request.Cookies["PreferredCulture"].Value; + preferredCulture = request.Cookies["PreferredCulture"].Value; // Ignores whether we actually support this culture...? } else { // No, determine from browser - string browserPreference = "en-US"; + + InitializeLookups(); + + string browserPreference = string.Empty; if (request.UserLanguages != null && request.UserLanguages.Length > 0) { - browserPreference = request.UserLanguages[0]; - preferredCulture = browserPreference; - } + // Iterate over user-requested cultures, return best match - /* - string[] languages = (string[])Application["Cultures"]; - for (int index = 0; index < languages.Length; index++) - { - if (languages[index].StartsWith(browserPreference)) - { - preferredCulture = languages[index]; + string partialMatch = string.Empty; + + foreach (string userLanguage in request.UserLanguages) + { + string partialLanguage = userLanguage; + int indexOfDivider = partialLanguage.IndexOf('-'); + if (indexOfDivider > 0) + { + partialLanguage = partialLanguage.Substring(0, indexOfDivider); + } + else + { + partialLanguage = string.Empty; + } + + // If we have a full match, e.g. en-US to en-US... + + if (_supportedFullCulturesLookup.ContainsKey(userLanguage)) + { + browserPreference = userLanguage; + break; + } + + // ...otherwise, a close match, like en-US to a requested en-AU.... + + if (_supportedPartialCulturesLookup.ContainsKey(partialLanguage)) + { + partialMatch = partialLanguage; + } + } + + if (!string.IsNullOrEmpty(browserPreference)) // prefer full match to partial match + { + preferredCulture = browserPreference; + } + else if (!string.IsNullOrEmpty(partialMatch)) + { + preferredCulture = partialMatch; + } + + // if none of these work, use the default setting, en-US, from initialization } - }*/ } GregorianCalendar normalizedCalendar = new GregorianCalendar(); @@ -85,6 +119,28 @@ namespace Swarmops.Frontend Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } + private static Dictionary _supportedFullCulturesLookup; + private static Dictionary _supportedPartialCulturesLookup; + + private static void InitializeLookups() + { + if (_supportedFullCulturesLookup != null) + { + return; + } + + _supportedFullCulturesLookup = new Dictionary(); + _supportedPartialCulturesLookup = new Dictionary(); + + string[] supportedCultures = Swarmops.Logic.Support.Formatting.SupportedCultures; + foreach (string culture in supportedCultures) + { + _supportedFullCulturesLookup[culture] = true; + _supportedPartialCulturesLookup[culture.Substring(0, culture.IndexOf('-'))] = true; + } + } + + public static AuthenticationData GetAuthenticationDataAndCulture (HttpContext suppliedContext) { // This function is called from all the V5 base classes to have a common diff --git a/Site/Pages/v5/Admin/Json-CultureInfo.aspx.cs b/Site/Pages/v5/Admin/Json-CultureInfo.aspx.cs index 6f63e798..5fb177fd 100644 --- a/Site/Pages/v5/Admin/Json-CultureInfo.aspx.cs +++ b/Site/Pages/v5/Admin/Json-CultureInfo.aspx.cs @@ -37,9 +37,6 @@ namespace Swarmops.Frontend.Pages.v5.Admin culturePartialLookup[culture.Substring(0, culture.IndexOf('-'))] = true; } - cultureFullLookup["en-US"] = true; - culturePartialLookup["en"] = true; - string yesImage = ""; string halfImage = ""; string noImage = "";