More robust language caching

This commit is contained in:
Thomas Hounsell 2016-08-21 16:15:33 +01:00
parent df5dd2fd4b
commit 7901300c30
5 changed files with 39 additions and 26 deletions

View File

@ -1,4 +1,6 @@
using System.Globalization;
using System.Linq;
using System.Web;
namespace BuildFeed.Code.Options
{
@ -39,6 +41,8 @@ public class Locale
new Locale("zh-hant")
};
private const string LANG_COOKIE_NAME = "bf_lang";
public string DisplayName => Info.NativeName;
public CultureInfo Info { get; set; }
@ -49,5 +53,31 @@ public Locale(string localeId)
LocaleId = localeId;
Info = CultureInfo.GetCultureInfo(localeId);
}
public static CultureInfo DetectCulture(HttpContextBase context)
{
string langCookie = context.Request.Cookies[LANG_COOKIE_NAME]?.Value;
if (!string.IsNullOrEmpty(langCookie))
{
try
{
CultureInfo ci = (CultureInfo)CultureInfo.GetCultureInfo(langCookie).Clone();
// Get Gregorian Calendar in locale if available
Calendar gc = ci.OptionalCalendars.FirstOrDefault(c => c is GregorianCalendar && ((GregorianCalendar)c).CalendarType == GregorianCalendarTypes.Localized);
if (gc != null)
{
ci.DateTimeFormat.Calendar = gc;
}
return ci;
}
catch (CultureNotFoundException) { }
}
return CultureInfo.CurrentCulture;
}
}
}

View File

@ -9,31 +9,11 @@ namespace BuildFeed.Controllers
{
public class BaseController : Controller
{
private const string LANG_COOKIE_NAME = "bf_lang";
protected override void Initialize(RequestContext requestContext)
{
string langCookie = requestContext.HttpContext.Request.Cookies[LANG_COOKIE_NAME]?.Value;
if (!string.IsNullOrEmpty(langCookie))
{
try
{
CultureInfo ci = (CultureInfo)CultureInfo.GetCultureInfo(langCookie).Clone();
// Get Gregorian Calendar in locale if available
Calendar gc = ci.OptionalCalendars.FirstOrDefault(c => c is GregorianCalendar && ((GregorianCalendar)c).CalendarType == GregorianCalendarTypes.Localized);
if (gc != null)
{
ci.DateTimeFormat.Calendar = gc;
}
CultureInfo ci = Locale.DetectCulture(requestContext.HttpContext);
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
}
catch (CultureNotFoundException) { }
}
ViewBag.Theme = new Theme(Theme.DetectTheme(requestContext.HttpContext));

View File

@ -37,6 +37,7 @@ public override string GetVaryByCustomString(HttpContext context, string custom)
{
string[] parts = custom.Split(';');
List<string> varyParts = new List<string>();
HttpContextWrapper contextWrapper = new HttpContextWrapper(context);
foreach (string part in parts)
{
@ -46,10 +47,10 @@ public override string GetVaryByCustomString(HttpContext context, string custom)
varyParts.Add($"user:{context.User.Identity.Name}");
break;
case "lang":
varyParts.Add($"lang:{CultureInfo.CurrentUICulture.LCID}");
varyParts.Add($"lang:{Locale.DetectCulture(contextWrapper).LCID}");
break;
case "theme":
varyParts.Add($"theme:{Theme.DetectTheme(new HttpContextWrapper(context))}");
varyParts.Add($"theme:{Theme.DetectTheme(contextWrapper)}");
break;
}
}

View File

@ -1,3 +1,5 @@
/// <reference path="../../scripts/typings/google.analytics/ga.d.ts" />
/// <reference path="../../scripts/typings/jsrender/jsrender.d.ts" />
var BuildFeed;
(function (BuildFeed) {
var ajax;

File diff suppressed because one or more lines are too long