internal static Tuple <String, String> GetClientCultureScriptBlock(CultureInfo cultureInfo) { if (cultureInfo == null) { return(null); } // note: DateTimeFormat could be null since it is a virtual property, but DateTimeFormat.Calendar cannot be Type calendarType = cultureInfo.DateTimeFormat == null ? null : cultureInfo.DateTimeFormat.Calendar.GetType(); if (cultureInfo.Equals(enUS) && (calendarType == typeof(GregorianCalendar))) { return(null); } var key = new Tuple <CultureInfo, Type>(cultureInfo, calendarType); Tuple <String, String> cached = cultureScriptBlockCache[key] as Tuple <String, String>; if (cached == null) { ClientCultureInfo clientCultureInfo = new ClientCultureInfo(cultureInfo); string json = JavaScriptSerializer.SerializeInternal(BuildSerializeableCultureInfo(clientCultureInfo)); if (json.Length > 0) { string script = "var __cultureInfo = " + json + ";"; if (clientCultureInfo._adjustment != 0) { script += "\r\n__cultureInfo.dateTimeFormat.Calendar._adjustment = " + clientCultureInfo._adjustment.ToString(CultureInfo.InvariantCulture) + ";"; } cached = new Tuple <String, String>(script, clientCultureInfo._convertScript); } cultureScriptBlockCache[key] = cached; } return(cached); }
private static OrderedDictionary BuildSerializeableCultureInfo(ClientCultureInfo clientCultureInfo) { // It's safe to serialize the values set in the dictionary // name is a string // numberFormat is NumberFormatInfo which is a public type // dateTimeFormat is a DateFormatInfo which is a public type // eras is an object[] array that only contains strings or numbers var dictionary = new OrderedDictionary(); dictionary["name"] = clientCultureInfo.name; dictionary["numberFormat"] = clientCultureInfo.numberFormat; dictionary["dateTimeFormat"] = clientCultureInfo.dateTimeFormat; dictionary["eras"] = clientCultureInfo.eras; return(dictionary); }
private static OrderedDictionary BuildSerializeableCultureInfo(ClientCultureInfo clientCultureInfo) { // It's safe to serialize the values set in the dictionary // name is a string // numberFormat is NumberFormatInfo which is a public type // dateTimeFormat is a DateFormatInfo which is a public type // eras is an object[] array that only contains strings or numbers var dictionary = new OrderedDictionary(); dictionary["name"] = clientCultureInfo.name; dictionary["numberFormat"] = clientCultureInfo.numberFormat; dictionary["dateTimeFormat"] = clientCultureInfo.dateTimeFormat; dictionary["eras"] = clientCultureInfo.eras; return dictionary; }
internal static Tuple<String, String> GetClientCultureScriptBlock(CultureInfo cultureInfo) { if (cultureInfo == null) { return null; } // note: DateTimeFormat could be null since it is a virtual property, but DateTimeFormat.Calendar cannot be Type calendarType = cultureInfo.DateTimeFormat == null ? null : cultureInfo.DateTimeFormat.Calendar.GetType(); if (cultureInfo.Equals(enUS) && (calendarType == typeof(GregorianCalendar))) { return null; } var key = new Tuple<CultureInfo, Type>(cultureInfo, calendarType); Tuple<String, String> cached = cultureScriptBlockCache[key] as Tuple<String, String>; if (cached == null) { ClientCultureInfo clientCultureInfo = new ClientCultureInfo(cultureInfo); string json = JavaScriptSerializer.SerializeInternal(BuildSerializeableCultureInfo(clientCultureInfo)); if (json.Length > 0) { string script = "var __cultureInfo = " + json + ";"; if (clientCultureInfo._adjustment != 0) { script += "\r\n__cultureInfo.dateTimeFormat.Calendar._adjustment = " + clientCultureInfo._adjustment.ToString(CultureInfo.InvariantCulture) + ";"; } cached = new Tuple<String, String>(script, clientCultureInfo._convertScript); } cultureScriptBlockCache[key] = cached; } return cached; }