Improved culture handling

This commit is contained in:
Rick Falkvinge
2019-01-27 15:59:57 +01:00
parent b5cd7c93d6
commit 395b4a3eb1
3 changed files with 72 additions and 19 deletions
+1 -1
View File
@@ -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
}
+71 -15
View File
@@ -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<string, bool> _supportedFullCulturesLookup;
private static Dictionary<string, bool> _supportedPartialCulturesLookup;
private static void InitializeLookups()
{
if (_supportedFullCulturesLookup != null)
{
return;
}
_supportedFullCulturesLookup = new Dictionary<string, bool>();
_supportedPartialCulturesLookup = new Dictionary<string, bool>();
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
@@ -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 = "<img src='/Images/Icons/iconshock-green-tick-128x96px.png' height='20' width='26' />";
string halfImage = "<img src='/Images/Icons/iconshock-gold-tick-128x96px.png' height='18' width='24' />";
string noImage = "<img src='/Images/Icons/iconshock-red-cross-128x96px.png' height='10' width='13' />";