Fix for a localization bug in Json data

This commit is contained in:
Rick Falkvinge
2014-10-08 09:55:04 +02:00
parent c64f8c11c4
commit 7f7b3de610
7 changed files with 74 additions and 119 deletions
+52
View File
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
/// <summary>
@@ -14,4 +16,54 @@ public class CommonV5Base
// TODO: Add constructor logic here
//
}
static public void CulturePreInit (HttpRequest request)
{
// Localization
// Set default culture (English, United States)
string preferredCulture = "en-US";
// ----------- SET CULTURE ------------
// Does the user have a culture preference?
if (request.Cookies["PreferredCulture"] != null)
{
// Yes, set it
preferredCulture = request.Cookies["PreferredCulture"].Value;
}
else
{
// No, determine from browser
string browserPreference = "en-US";
if (request.UserLanguages != null && request.UserLanguages.Length > 0)
{
browserPreference = request.UserLanguages[0];
preferredCulture = browserPreference;
}
/*
string[] languages = (string[])Application["Cultures"];
for (int index = 0; index < languages.Length; index++)
{
if (languages[index].StartsWith(browserPreference))
{
preferredCulture = languages[index];
}
}*/
}
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(preferredCulture);
}
catch (Exception exception)
{
throw new Exception("Could not set culture \"" + preferredCulture + "\"", exception);
// Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
+5 -49
View File
@@ -40,54 +40,7 @@ public class DataV5Base : System.Web.UI.Page
protected override void OnPreInit(EventArgs e)
{
// Localization
// Set default culture (English, United States, but that doesn't work so fake it to GB)
string preferredCulture = "en-GB";
// ----------- SET CULTURE ------------
// Does the user have a culture preference?
if (Request.Cookies["PreferredCulture"] != null)
{
// Yes, set it
preferredCulture = Request.Cookies["PreferredCulture"].Value;
}
else
{
// No, determine from browser
string browserPreference = "en-GB";
if (Request.UserLanguages != null && Request.UserLanguages.Length > 0)
{
browserPreference = Request.UserLanguages[0];
preferredCulture = browserPreference;
}
/*
string[] languages = (string[])Application["Cultures"];
for (int index = 0; index < languages.Length; index++)
{
if (languages[index].StartsWith(browserPreference))
{
preferredCulture = languages[index];
}
}*/
}
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(preferredCulture);
}
catch (Exception exception)
{
throw new Exception("Could not set culture \"" + preferredCulture + "\"", exception);
// Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
CommonV5Base.CulturePreInit(Request);
base.OnPreInit(e);
}
@@ -202,6 +155,9 @@ public class DataV5Base : System.Web.UI.Page
result.CurrentUser = Person.FromIdentity(currentUserId);
result.CurrentOrganization = Organization.FromIdentity(currentOrganizationId);
CommonV5Base.CulturePreInit(HttpContext.Current.Request); // OnPreInit() isn't called in the static methods calling this fn
/*
string userCultureString = result.CurrentUser.PreferredCulture;
if (!string.IsNullOrEmpty(userCultureString))
@@ -209,7 +165,7 @@ public class DataV5Base : System.Web.UI.Page
CultureInfo userCulture = new CultureInfo(userCultureString); // may throw on invalid database data
Thread.CurrentThread.CurrentCulture = userCulture;
Thread.CurrentThread.CurrentUICulture = userCulture;
}
}*/
return result;
}
+1 -48
View File
@@ -65,54 +65,7 @@ public class PageV5Base : System.Web.UI.Page
protected override void OnPreInit(EventArgs e)
{
// Localization
// Set default culture (English, United States)
string preferredCulture = "en-US";
// ----------- SET CULTURE ------------
// Does the user have a culture preference?
if (Request.Cookies["PreferredCulture"] != null)
{
// Yes, set it
preferredCulture = Request.Cookies["PreferredCulture"].Value;
}
else
{
// No, determine from browser
string browserPreference = "en-US";
if (Request.UserLanguages != null && Request.UserLanguages.Length > 0)
{
browserPreference = Request.UserLanguages[0];
preferredCulture = browserPreference;
}
/*
string[] languages = (string[])Application["Cultures"];
for (int index = 0; index < languages.Length; index++)
{
if (languages[index].StartsWith(browserPreference))
{
preferredCulture = languages[index];
}
}*/
}
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(preferredCulture);
}
catch (Exception exception)
{
throw new Exception("Could not set culture \"" + preferredCulture + "\"", exception);
// Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
CommonV5Base.CulturePreInit(Request);
base.OnPreInit(e);
}
+4 -6
View File
@@ -186,10 +186,8 @@ namespace Swarmops
if (cultureStringLower == "en-us")
{
// doesn't work with tooltip styling for some completely inexplicable reason, use en-gb
cultureString = "en-GB";
cultureStringLower = "en-gb";
cultureString = "en-US";
cultureStringLower = "en-us";
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureString);
HttpCookie cookieCulture = new HttpCookie("PreferredCulture");
@@ -200,7 +198,7 @@ namespace Swarmops
string flagName = "uk";
if (cultureStringLower != "en-gb" && cultureString.Length > 3)
if (!cultureStringLower.StartsWith("en") && cultureString.Length > 3)
{
flagName = cultureStringLower.Substring(3);
}
@@ -211,7 +209,7 @@ namespace Swarmops
this.LabelPreferences.Text = Resources.Global.CurrentUserInfo_Preferences;
this.LiteralCurrentlyLoggedIntoSwitch.Text = string.Format(Resources.Global.Master_SwitchOrganizationDialog, _currentOrganization.Name);
if (cultureStringLower != "en-gb" && cultureStringLower != "sv-se" && cultureString.Trim().Length > 0)
if (!cultureStringLower.StartsWith("en") && cultureStringLower != "sv-se" && cultureString.Trim().Length > 0)
{
this.LinkTranslate.Visible = true;
}
@@ -38,7 +38,6 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
}
private int _year = 2012;
private CultureInfo _renderCulture;
private AuthenticationData _authenticationData;
private Dictionary<int, FinancialAccounts> _hashedAccounts;
@@ -200,11 +199,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
{
if (treeValue != 0 && singleValue == 0)
{
return string.Format(_renderCulture,
return string.Format(CultureInfo.CurrentCulture,
"<span class=\\\"accountplandata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"accountplandata-expanded-{0}\\\" style=\\\"display:none\\\">&nbsp;</span>",
accountId, treeValue / 100.00);
}
return string.Format(_renderCulture,
return string.Format(CultureInfo.CurrentCulture,
"<span class=\\\"accountplandata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"accountplandata-expanded-{0}\\\" style=\\\"display:none\\\">{2:N0}</span>",
accountId, treeValue / 100.0, singleValue / 100.0);
}
@@ -68,8 +68,6 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
private int _year = 2012;
private CultureInfo _renderCulture;
private string RecurseReport(List<YearlyReportLine> reportLines)
@@ -104,16 +102,16 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
}
else
{
element += string.Format(_renderCulture, ",\"lastYear\":\"{0:N0}\"",
element += string.Format(CultureInfo.CurrentCulture, ",\"lastYear\":\"{0:N0}\"",
(double) line.AccountValues.PreviousYear/100.0);
for (int quarter = 1; quarter <= 4; quarter++)
{
element += string.Format(_renderCulture, ",\"q{0}\":\"{1:N0}\"", quarter,
element += string.Format(CultureInfo.CurrentCulture, ",\"q{0}\":\"{1:N0}\"", quarter,
line.AccountValues.Quarters[quarter - 1]/100.0);
}
element += string.Format(_renderCulture, ",\"ytd\":\"{0:N0}\"",
element += string.Format(CultureInfo.CurrentCulture, ",\"ytd\":\"{0:N0}\"",
(double) line.AccountValues.ThisYear/100.0);
}
@@ -128,11 +126,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
{
if (treeValue != 0 && singleValue == 0)
{
return string.Format(_renderCulture,
return string.Format(CultureInfo.CurrentCulture,
"\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">&nbsp;</span>\"",
accountId, treeValue/100.00);
}
return string.Format(_renderCulture,
return string.Format(CultureInfo.CurrentCulture,
"\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">{2:N0}</span>\"",
accountId, treeValue/100.0, singleValue/100.0);
}
@@ -80,7 +80,6 @@ public partial class Pages_v5_Ledgers_Json_ProfitLossData : DataV5Base
private int _year = 2012;
private CultureInfo _renderCulture;
private string RecurseReport (List<YearlyReportLine> reportLines)
{
@@ -111,14 +110,14 @@ public partial class Pages_v5_Ledgers_Json_ProfitLossData : DataV5Base
}
else
{
element += string.Format(_renderCulture, ",\"lastYear\":\"{0:N0}\"", (double)line.AccountValues.PreviousYear / -100.0);
element += string.Format(CultureInfo.CurrentCulture, ",\"lastYear\":\"{0:N0}\"", (double)line.AccountValues.PreviousYear / -100.0);
for (int quarter = 1; quarter <= 4; quarter++)
{
element += string.Format(_renderCulture, ",\"q{0}\":\"{1:N0}\"", quarter, line.AccountValues.Quarters[quarter-1] / -100.0);
element += string.Format(CultureInfo.CurrentCulture, ",\"q{0}\":\"{1:N0}\"", quarter, line.AccountValues.Quarters[quarter-1] / -100.0);
}
element += string.Format(_renderCulture, ",\"ytd\":\"{0:N0}\"", (double)line.AccountValues.ThisYear / -100.0);
element += string.Format(CultureInfo.CurrentCulture, ",\"ytd\":\"{0:N0}\"", (double)line.AccountValues.ThisYear / -100.0);
}
elements.Add("{" + element + "}");
@@ -132,9 +131,9 @@ public partial class Pages_v5_Ledgers_Json_ProfitLossData : DataV5Base
{
if (treeValue != 0 && singleValue == 0)
{
return string.Format(_renderCulture, "\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">&nbsp;</span>\"", accountId, treeValue / -100.00);
return string.Format(CultureInfo.CurrentCulture, "\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">&nbsp;</span>\"", accountId, treeValue / -100.00);
}
return string.Format(_renderCulture, "\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">{2:N0}</span>\"", accountId, treeValue / -100.0, singleValue / -100.0);
return string.Format(CultureInfo.CurrentCulture, "\"<span class=\\\"profitlossdata-collapsed-{0}\\\"><strong>&Sigma;</strong> {1:N0}</span><span class=\\\"profitlossdata-expanded-{0}\\\" style=\\\"display:none\\\">{2:N0}</span>\"", accountId, treeValue / -100.0, singleValue / -100.0);
}