Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Extensions / Globalization / ClientCultureInfo.cs / 1407647 / ClientCultureInfo.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Globalization { using System; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Text; using System.Web.Util; using System.Web.Script.Serialization; internal class ClientCultureInfo { private static Hashtable cultureScriptBlockCache = Hashtable.Synchronized(new Hashtable()); private static readonly CultureInfo enUS = CultureInfo.GetCultureInfo(0x409); private static int eraNumber = 0; private static int eraName = 1; private static int eraStart = 2; private static int eraYearOffset = 3; public string name; public NumberFormatInfo numberFormat; public DateTimeFormatInfo dateTimeFormat; public object[] eras; private string _convertScript; private int _adjustment; private ClientCultureInfo(CultureInfo cultureInfo) { name = cultureInfo.Name; numberFormat = cultureInfo.NumberFormat; dateTimeFormat = cultureInfo.DateTimeFormat; var calendar = dateTimeFormat == null ? null : dateTimeFormat.Calendar; if (calendar != null) { // Dev10 425049: Support Eras for gregorian based calendars // with a simple year offset, and non-gregorian calendars. // Era data is stored in binary resource "culture.nlp" in mscorlib, // hard coded here for simplicity. // era array has the following structure: // [eraNumber1, eraName1, eraStartInTicks1, eraGregorianYearOffset1, eraNumber2, ...] eras = new object[calendar.Eras.Length * 4]; int i = 0; foreach (int era in calendar.Eras) { // era number eras[i + eraNumber] = era; // era name eras[i + eraName] = dateTimeFormat.GetEraName(era); // calendars with only one era will have a null tick count // signifying that the era starts from the lowest datetime // era begining in ticks (null = the oldest era) // eras[i + eraStart] = null; // era year offset from normal gregorian year // some calendars dont have an offset, just a different name // for the A.D. era (B.C. is not supported by normal calendar, // so most calendars only have 1 era) eras[i + eraYearOffset] = 0; i += 4; } var calendarType = calendar.GetType(); if (calendarType != typeof(GregorianCalendar)) { if (calendarType == typeof(TaiwanCalendar)) { // Only the current era is supported, so no tick count is needed //eras[eraStart] = -1830384000000; eras[eraYearOffset] = 1911; } else if (calendarType == typeof(KoreanCalendar)) { // only one era to speak of, so no tick count is needed //eras[eraStart] = -62135596800000; eras[eraYearOffset] = -2333; } else if (calendarType == typeof(ThaiBuddhistCalendar)) { // only one era to speak of, so no tick count is needed //eras[eraStart] = -62135596800000; eras[eraYearOffset] = -543; } else if (calendarType == typeof(JapaneseCalendar)) { // there are multiple eras eras[0 + eraStart] = 60022080000; eras[0 + eraYearOffset] = 1988; eras[4 + eraStart] = -1357603200000; eras[4 + eraYearOffset] = 1925; eras[8 + eraStart] = -1812153600000; eras[8 + eraYearOffset] = 1911; // oldest era is technically from this offset, but for simplicity // it is counted from the lowest date time, so no tick count needed. //eras[12 + eraStart] = -3218832000000; eras[12 + eraYearOffset] = 1867; } else if (calendarType == typeof(HijriCalendar)) { _convertScript = "Date.HijriCalendar.js"; _adjustment = ((HijriCalendar)calendar).HijriAdjustment; } else if (calendarType == typeof(UmAlQuraCalendar)) { _convertScript = "Date.UmAlQuraCalendar.js"; } // else { other calendars arent supported or have no era offsets just different names for A.D. } } } internal PairGetClientCultureScriptBlock() { return GetClientCultureScriptBlock(CultureInfo.CurrentCulture); } internal static Pair 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 Pair (cultureInfo, calendarType); Pair cached = cultureScriptBlockCache[key] as Pair ; 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 Pair (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; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Globalization { using System; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Text; using System.Web.Util; using System.Web.Script.Serialization; internal class ClientCultureInfo { private static Hashtable cultureScriptBlockCache = Hashtable.Synchronized(new Hashtable()); private static readonly CultureInfo enUS = CultureInfo.GetCultureInfo(0x409); private static int eraNumber = 0; private static int eraName = 1; private static int eraStart = 2; private static int eraYearOffset = 3; public string name; public NumberFormatInfo numberFormat; public DateTimeFormatInfo dateTimeFormat; public object[] eras; private string _convertScript; private int _adjustment; private ClientCultureInfo(CultureInfo cultureInfo) { name = cultureInfo.Name; numberFormat = cultureInfo.NumberFormat; dateTimeFormat = cultureInfo.DateTimeFormat; var calendar = dateTimeFormat == null ? null : dateTimeFormat.Calendar; if (calendar != null) { // Dev10 425049: Support Eras for gregorian based calendars // with a simple year offset, and non-gregorian calendars. // Era data is stored in binary resource "culture.nlp" in mscorlib, // hard coded here for simplicity. // era array has the following structure: // [eraNumber1, eraName1, eraStartInTicks1, eraGregorianYearOffset1, eraNumber2, ...] eras = new object[calendar.Eras.Length * 4]; int i = 0; foreach (int era in calendar.Eras) { // era number eras[i + eraNumber] = era; // era name eras[i + eraName] = dateTimeFormat.GetEraName(era); // calendars with only one era will have a null tick count // signifying that the era starts from the lowest datetime // era begining in ticks (null = the oldest era) // eras[i + eraStart] = null; // era year offset from normal gregorian year // some calendars dont have an offset, just a different name // for the A.D. era (B.C. is not supported by normal calendar, // so most calendars only have 1 era) eras[i + eraYearOffset] = 0; i += 4; } var calendarType = calendar.GetType(); if (calendarType != typeof(GregorianCalendar)) { if (calendarType == typeof(TaiwanCalendar)) { // Only the current era is supported, so no tick count is needed //eras[eraStart] = -1830384000000; eras[eraYearOffset] = 1911; } else if (calendarType == typeof(KoreanCalendar)) { // only one era to speak of, so no tick count is needed //eras[eraStart] = -62135596800000; eras[eraYearOffset] = -2333; } else if (calendarType == typeof(ThaiBuddhistCalendar)) { // only one era to speak of, so no tick count is needed //eras[eraStart] = -62135596800000; eras[eraYearOffset] = -543; } else if (calendarType == typeof(JapaneseCalendar)) { // there are multiple eras eras[0 + eraStart] = 60022080000; eras[0 + eraYearOffset] = 1988; eras[4 + eraStart] = -1357603200000; eras[4 + eraYearOffset] = 1925; eras[8 + eraStart] = -1812153600000; eras[8 + eraYearOffset] = 1911; // oldest era is technically from this offset, but for simplicity // it is counted from the lowest date time, so no tick count needed. //eras[12 + eraStart] = -3218832000000; eras[12 + eraYearOffset] = 1867; } else if (calendarType == typeof(HijriCalendar)) { _convertScript = "Date.HijriCalendar.js"; _adjustment = ((HijriCalendar)calendar).HijriAdjustment; } else if (calendarType == typeof(UmAlQuraCalendar)) { _convertScript = "Date.UmAlQuraCalendar.js"; } // else { other calendars arent supported or have no era offsets just different names for A.D. } } } internal PairGetClientCultureScriptBlock() { return GetClientCultureScriptBlock(CultureInfo.CurrentCulture); } internal static Pair 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 Pair (cultureInfo, calendarType); Pair cached = cultureScriptBlockCache[key] as Pair ; 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 Pair (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; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlSchemaImporter.cs
- ContextActivityUtils.cs
- AutomationPropertyChangedEventArgs.cs
- CodeDirectoryCompiler.cs
- Site.cs
- UInt16.cs
- DoubleStorage.cs
- WebPartDescription.cs
- Calendar.cs
- PropertyGeneratedEventArgs.cs
- UpWmlPageAdapter.cs
- EntityType.cs
- FrameDimension.cs
- DllNotFoundException.cs
- Axis.cs
- MobileControlDesigner.cs
- String.cs
- CellParagraph.cs
- nulltextnavigator.cs
- EndpointIdentityConverter.cs
- AtlasWeb.Designer.cs
- CryptoKeySecurity.cs
- OperationInvokerBehavior.cs
- SourceItem.cs
- Span.cs
- COM2IDispatchConverter.cs
- AgileSafeNativeMemoryHandle.cs
- DuplicateWaitObjectException.cs
- SelectionPattern.cs
- WebPartConnectionsDisconnectVerb.cs
- Event.cs
- AssemblyHash.cs
- UserNamePasswordValidator.cs
- ServiceDocumentFormatter.cs
- ResXResourceReader.cs
- Group.cs
- PeerEndPoint.cs
- InheritanceService.cs
- CompilerErrorCollection.cs
- ResourceBinder.cs
- ToolboxDataAttribute.cs
- PageFunction.cs
- HashMembershipCondition.cs
- Psha1DerivedKeyGenerator.cs
- DocumentGrid.cs
- HyperLinkStyle.cs
- XmlSchemaExternal.cs
- CharacterMetricsDictionary.cs
- BoundField.cs
- FontFamily.cs
- Restrictions.cs
- BitmapPalettes.cs
- ValidatingReaderNodeData.cs
- HijriCalendar.cs
- ScriptServiceAttribute.cs
- ReadContentAsBinaryHelper.cs
- GetParentChain.cs
- AllMembershipCondition.cs
- XPathAxisIterator.cs
- Brush.cs
- SchemeSettingElement.cs
- NativeObjectSecurity.cs
- ErasingStroke.cs
- JsonReaderDelegator.cs
- SystemIcmpV6Statistics.cs
- Vector3DIndependentAnimationStorage.cs
- RelationshipEnd.cs
- DecoderFallback.cs
- FileSystemEventArgs.cs
- SimpleRecyclingCache.cs
- TraceEventCache.cs
- PageThemeParser.cs
- CharStorage.cs
- Cell.cs
- XAMLParseException.cs
- ProtocolViolationException.cs
- DetailsViewDeletedEventArgs.cs
- DataContractFormatAttribute.cs
- UnionCodeGroup.cs
- ContainerUtilities.cs
- SoapIncludeAttribute.cs
- Divide.cs
- NativeMethods.cs
- DependencyPropertyConverter.cs
- GlobalEventManager.cs
- TransactionScope.cs
- MenuItemCollection.cs
- CancellationTokenRegistration.cs
- DataBinding.cs
- BindableTemplateBuilder.cs
- StatusBarPanel.cs
- CompilerError.cs
- StyleBamlTreeBuilder.cs
- ResourcesChangeInfo.cs
- HttpProfileBase.cs
- XmlNullResolver.cs
- PlainXmlDeserializer.cs
- DeobfuscatingStream.cs
- Privilege.cs
- SchemaEntity.cs