Merge pull request #1678 from esdrubal/tzindst
[mono.git] / mcs / class / corlib / System.Globalization / CultureInfo.cs
1 //
2 // System.Globalization.CultureInfo.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Dick Porter (dick@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
8 //
9 // (C) 2001, 2002, 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections.Generic;
34 using System.Threading;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics.Contracts;
38
39 namespace System.Globalization
40 {
41         [System.Runtime.InteropServices.ComVisible (true)]
42         [Serializable]
43         [StructLayout (LayoutKind.Sequential)]
44         public class CultureInfo : ICloneable, IFormatProvider
45         {
46                 static volatile CultureInfo invariant_culture_info = new CultureInfo (InvariantCultureId, false, true);
47                 static object shared_table_lock = new object ();
48                 static CultureInfo default_current_culture;
49
50 #pragma warning disable 169, 649
51                 bool m_isReadOnly;
52                 int  cultureID;
53                 [NonSerialized]
54                 int parent_lcid;
55                 [NonSerialized]
56                 int datetime_index;
57                 [NonSerialized]
58                 int number_index;
59                 [NonSerialized]
60                 int default_calendar_type;
61                 bool m_useUserOverride;
62                 [NonSerialized]
63                 internal volatile NumberFormatInfo numInfo;
64                 internal volatile DateTimeFormatInfo dateTimeInfo;
65                 volatile TextInfo textInfo;
66                 internal string m_name;
67                 
68                 [NonSerialized]
69                 private string englishname;
70                 [NonSerialized]
71                 private string nativename;
72                 [NonSerialized]
73                 private string iso3lang;
74                 [NonSerialized]
75                 private string iso2lang;
76                 [NonSerialized]
77                 private string win3lang;
78                 [NonSerialized]
79                 private string territory;
80                 [NonSerialized]
81                 string[] native_calendar_names;
82
83                 volatile CompareInfo compareInfo;
84                 [NonSerialized]
85                 private unsafe readonly void *textinfo_data;
86
87                 int m_dataItem;         // MS.NET serializes this.
88 #pragma warning restore 169, 649
89
90                 Calendar calendar;
91
92                 [NonSerialized]
93                 CultureInfo parent_culture;
94
95                 // Deserialized instances will set this to false
96                 [NonSerialized]
97                 bool constructed;
98
99                 [NonSerialized]
100                 // Used by Thread.set_CurrentCulture
101                 internal byte[] cached_serialized_form;
102
103                 [NonSerialized]internal CultureData m_cultureData;
104                 [NonSerialized]internal bool m_isInherited;
105                 
106                 internal const int InvariantCultureId = 0x7F;
107                 const int CalendarTypeBits = 8;
108
109                 const string MSG_READONLY = "This instance is read only";
110                 
111                 public static CultureInfo InvariantCulture {
112                         get {
113                                 return invariant_culture_info;
114                         }
115                 }
116
117                 public static CultureInfo CurrentCulture {
118                         get {
119                                 return Thread.CurrentThread.CurrentCulture;
120                         }
121                 }
122
123                 public static CultureInfo CurrentUICulture { 
124                         get {
125                                 return Thread.CurrentThread.CurrentUICulture;
126                         }
127                 }
128
129                 internal static CultureInfo ConstructCurrentCulture ()
130                 {
131                         if (default_current_culture != null)
132                                 return default_current_culture;
133
134                         var locale_name = get_current_locale_name ();
135                         CultureInfo ci = null;
136                         try {
137                                 ci = CreateSpecificCulture (locale_name);
138                         } catch {
139                         }
140
141                         if (ci == null) {
142                                 ci = InvariantCulture;
143                         } else {
144                                 ci.m_isReadOnly = true;
145                                 ci.m_useUserOverride = true;
146                         }
147
148                         default_current_culture = ci;
149                         return ci;
150                 }
151
152                 internal static CultureInfo ConstructCurrentUICulture ()
153                 {
154                         return ConstructCurrentCulture ();
155                 }
156
157                 // it is used for RegionInfo.
158                 internal string Territory {
159                         get { return territory; }
160                 }
161
162 #if !NET_2_1
163                 // FIXME: It is implemented, but would be hell slow.
164                 [ComVisible (false)]
165                 public CultureTypes CultureTypes {
166                         get {
167                                 CultureTypes ret = (CultureTypes) 0;
168                                 foreach (CultureTypes v in Enum.GetValues (typeof (CultureTypes)))
169                                         if (Array.IndexOf (GetCultures (v), this) >= 0)
170                                                 ret |= v;
171                                 return ret;
172                         }
173                 }
174
175                 [ComVisible (false)]
176                 public CultureInfo GetConsoleFallbackUICulture ()
177                 {
178                         // as documented in MSDN ...
179                         switch (Name) {
180                         case "ar": case "ar-BH": case "ar-EG": case "ar-IQ":
181                         case "ar-JO": case "ar-KW": case "ar-LB": case "ar-LY":
182                         case "ar-QA": case "ar-SA": case "ar-SY": case "ar-AE":
183                         case "ar-YE":
184                         case "dv": case "dv-MV":
185                         case "fa": case "fa-IR":
186                         case "gu": case "gu-IN":
187                         case "he": case "he-IL":
188                         case "hi": case "hi-IN":
189                         case "kn": case "kn-IN":
190                         case "kok": case "kok-IN":
191                         case "mr": case "mr-IN":
192                         case "pa": case "pa-IN":
193                         case "sa": case "sa-IN":
194                         case "syr": case "syr-SY":
195                         case "ta": case "ta-IN":
196                         case "te": case "te-IN":
197                         case "th": case "th-TH":
198                         case "ur": case "ur-PK":
199                         case "vi": case "vi-VN":
200                                 return GetCultureInfo ("en");
201                         case "ar-DZ": case "ar-MA": case "ar-TN":
202                                 return GetCultureInfo ("fr");
203                         }
204                         return (CultureTypes & CultureTypes.WindowsOnlyCultures) != 0 ? CultureInfo.InvariantCulture : this;
205                 }
206
207                 [ComVisible (false)]
208                 public string IetfLanguageTag {
209                         // There could be more consistent way to implement
210                         // it, but right now it works just fine with this...
211                         get {
212                                 switch (Name) {
213                                 case "zh-CHS":
214                                         return "zh-Hans";
215                                 case "zh-CHT":
216                                         return "zh-Hant";
217                                 default:
218                                         return Name;
219                                 }
220                         }
221                 }
222
223                 // For specific cultures it basically returns LCID.
224                 // For neutral cultures it is mapped to the default(?) specific
225                 // culture, where the LCID of the specific culture seems to be
226                 // n + 1024 by default. zh-CHS is the only exception which is 
227                 // mapped to 2052, not 1028 (zh-CHT is mapped to 1028 instead).
228                 // There are very few exceptions, here I simply list them here.
229                 // It is Windows-specific property anyways, so no worthy of
230                 // trying to do some complex things with locale-builder.
231                 [ComVisible (false)]
232                 public virtual int KeyboardLayoutId {
233                         get {
234                                 switch (LCID) {
235                                 case 4: // zh-CHS (neutral)
236                                         return 2052;
237                                 case 1034: // es-ES Spanish 2
238                                         return 3082;
239                                 case 31748: // zh-CHT (neutral)
240                                         return 1028;
241                                 case 31770: // sr (neutral)
242                                         return 2074;
243                                 default:
244                                         return LCID < 1024 ? LCID + 1024 : LCID;
245                                 }
246                         }
247                 }
248 #endif
249
250                 public virtual int LCID {
251                         get {
252                                 return cultureID;
253                         }
254                 }
255
256                 public virtual string Name {
257                         get {
258                                 return(m_name);
259                         }
260                 }
261
262                 public virtual string NativeName {
263                         get {
264                                 if (!constructed) Construct ();
265                                 return nativename;
266                         }
267                 }
268
269                 internal string NativeCalendarName {
270                         get {
271                                 if (!constructed) Construct ();
272                                 return native_calendar_names[(default_calendar_type >> CalendarTypeBits) - 1];
273                         }
274                 }
275                 
276                 public virtual Calendar Calendar {
277                         get {
278                                 if (calendar == null) {
279                                         if (!constructed) Construct ();
280                                         calendar = CreateCalendar (default_calendar_type);
281                                 }
282
283                                 return calendar;
284                         }
285                 }
286
287                 [MonoLimitation ("Optional calendars are not supported only default calendar is returned")]
288                 public virtual Calendar[] OptionalCalendars {
289                         get {
290                                 return new[] { Calendar };
291                         }
292                 }
293
294                 public virtual CultureInfo Parent
295                 {
296                         get {
297                                 if (parent_culture == null) {
298                                         if (!constructed)
299                                                 Construct ();
300                                         if (parent_lcid == cultureID) {
301                                                 //
302                                                 // Parent lcid is same but culture info is not for legacy zh culture
303                                                 //
304                                                 if (parent_lcid == 0x7C04 && EnglishName [EnglishName.Length - 1] == 'y')
305                                                         return parent_culture = new CultureInfo ("zh-Hant");
306                                                 else if (parent_lcid == 0x0004 && EnglishName [EnglishName.Length -1] == 'y')
307                                                         return parent_culture = new CultureInfo ("zh-Hans");
308                                                 return null;
309                                         }
310                                         
311                                         if (parent_lcid == InvariantCultureId)
312                                                 parent_culture = InvariantCulture;
313                                         else if (cultureID == InvariantCultureId)
314                                                 parent_culture = this;
315                                         else if (cultureID == 0x0404) {
316                                                 // zh-tw has parent id 0x7C04 which is in this case zh-cht and not zh-hant
317                                                 parent_culture = new CultureInfo ("zh-CHT");
318                                         } else
319                                                 parent_culture = new CultureInfo (parent_lcid);
320                                 }
321                                 return parent_culture;
322                         }
323                 }
324
325                 public virtual TextInfo TextInfo
326                 {
327                         get {
328                                 if (textInfo == null) {
329                                         if (!constructed) Construct ();
330                                         lock (this) {
331                                                 if(textInfo == null) {
332                                                         textInfo = CreateTextInfo (m_isReadOnly);
333                                                 }
334                                         }
335                                 }
336                                 
337                                 return(textInfo);
338                         }
339                 }
340
341                 public virtual string ThreeLetterISOLanguageName {
342                         get {
343                                 if (!constructed) Construct ();
344                                 return iso3lang;
345                         }
346                 }
347
348                 public virtual string ThreeLetterWindowsLanguageName
349                 {
350                         get {
351                                 if (!constructed) Construct ();
352                                 return(win3lang);
353                         }
354                 }
355
356                 public virtual string TwoLetterISOLanguageName {
357                         get {
358                                 if (!constructed) Construct ();
359                                 return(iso2lang);
360                         }
361                 }
362
363                 public bool UseUserOverride
364                 {
365                         get {
366                                 return m_useUserOverride;
367                         }
368                 }
369
370                 public void ClearCachedData()
371                 {
372                         lock (shared_table_lock) {
373                                 shared_by_number = null;
374                                 shared_by_name = null;
375                         }
376
377                         //
378                         // ClearCachedData method does not refresh the information in
379                         // the Thread.CurrentCulture property for existing threads
380                         //
381                         default_current_culture = null;
382
383                         RegionInfo.ClearCachedData ();
384                         TimeZone.ClearCachedData ();
385                         TimeZoneInfo.ClearCachedData ();
386                 }
387
388                 public virtual object Clone()
389                 {
390                         if (!constructed) Construct ();
391                         CultureInfo ci=(CultureInfo)MemberwiseClone ();
392                         ci.m_isReadOnly=false;
393                         ci.cached_serialized_form=null;
394                         if (!IsNeutralCulture) {
395                                 ci.NumberFormat = (NumberFormatInfo)NumberFormat.Clone ();
396                                 ci.DateTimeFormat = (DateTimeFormatInfo)DateTimeFormat.Clone ();
397                         }
398                         return(ci);
399                 }
400
401                 public override bool Equals (object value)
402                 {
403                         CultureInfo b = value as CultureInfo;
404                         
405                         if (b != null)
406                                 return b.cultureID == cultureID;
407                         return false;
408                 }
409
410                 public static CultureInfo[] GetCultures(CultureTypes types)
411                 {
412                         bool neutral=((types & CultureTypes.NeutralCultures)!=0);
413                         bool specific=((types & CultureTypes.SpecificCultures)!=0);
414                         bool installed=((types & CultureTypes.InstalledWin32Cultures)!=0);  // TODO
415
416                         CultureInfo [] infos = internal_get_cultures (neutral, specific, installed);
417                         // The runtime returns a NULL in the first position of the array when
418                         // 'neutral' is true. We fill it in with a clone of InvariantCulture
419                         // since it must not be read-only
420                         if (neutral && infos.Length > 0 && infos [0] == null) {
421                                 infos [0] = (CultureInfo) InvariantCulture.Clone ();
422                         }
423
424                         for (int i = 1; i < infos.Length; ++i) {
425                                 var ci = infos [i];
426                                 infos [i].m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang);
427                         }
428
429                         return infos;
430                 }
431
432                 public override int GetHashCode ()
433                 {
434                         return cultureID.GetHashCode ();
435                 }
436
437                 public static CultureInfo ReadOnly(CultureInfo ci)
438                 {
439                         if(ci==null) {
440                                 throw new ArgumentNullException("ci");
441                         }
442
443                         if(ci.m_isReadOnly) {
444                                 return(ci);
445                         } else {
446                                 CultureInfo new_ci=(CultureInfo)ci.Clone ();
447                                 new_ci.m_isReadOnly=true;
448                                 if (new_ci.numInfo != null)
449                                         new_ci.numInfo = NumberFormatInfo.ReadOnly (new_ci.numInfo);
450                                 if (new_ci.dateTimeInfo != null)
451                                         new_ci.dateTimeInfo = DateTimeFormatInfo.ReadOnly (new_ci.dateTimeInfo);
452                                 if (new_ci.textInfo != null)
453                                         new_ci.textInfo = TextInfo.ReadOnly (new_ci.textInfo);
454                                 return(new_ci);
455                         }
456                 }
457
458                 public override string ToString()
459                 {
460                         return(m_name);
461                 }
462                 
463                 public virtual CompareInfo CompareInfo
464                 {
465                         get {
466                                 if(compareInfo==null) {
467                                         if (!constructed)
468                                                 Construct ();
469
470                                         lock (this) {
471                                                 if(compareInfo==null) {
472                                                         compareInfo=new CompareInfo (this);
473                                                 }
474                                         }
475                                 }
476                                 
477                                 return(compareInfo);
478                         }
479                 }
480
481                 public virtual bool IsNeutralCulture {
482                         get {
483                                 if (cultureID == InvariantCultureId)
484                                         return false;
485
486                                 if (!constructed) Construct ();
487                                 return territory == null;
488                         }
489                 }
490
491                 internal void CheckNeutral ()
492                 {
493                 }
494
495                 public virtual NumberFormatInfo NumberFormat {
496                         get {
497                                 if (numInfo == null) {
498                                         NumberFormatInfo temp = new NumberFormatInfo(this.m_cultureData);
499                                         temp.isReadOnly = m_isReadOnly;
500                                         numInfo = temp;
501                                 }
502
503                                 return numInfo;
504                         }
505
506                         set {
507                                 if (!constructed) Construct ();
508                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
509
510                                 if (value == null)
511                                         throw new ArgumentNullException ("NumberFormat");
512                                 
513                                 numInfo = value;
514                         }
515                 }
516
517                 public virtual DateTimeFormatInfo DateTimeFormat {
518                         get {
519                                 if (dateTimeInfo != null)
520                                         return dateTimeInfo;
521
522                                 if (!constructed) Construct ();
523                                 CheckNeutral ();
524
525                                 var temp = new DateTimeFormatInfo (m_cultureData, Calendar);
526                                 temp.m_isReadOnly = m_isReadOnly;
527                                 System.Threading.Thread.MemoryBarrier();
528                                 dateTimeInfo = temp;
529                                 return dateTimeInfo;
530                         }
531
532                         set {
533                                 if (!constructed) Construct ();
534                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
535
536                                 if (value == null)
537                                         throw new ArgumentNullException ("DateTimeFormat");
538                                 
539                                 dateTimeInfo = value;
540                         }
541                 }
542
543                 public virtual string DisplayName {
544                         get {
545                                 // Mono is not localized and will always return english name regardless of OS locale
546                                 return EnglishName;
547                         }
548                 }
549
550                 public virtual string EnglishName {
551                         get {
552                                 if (!constructed) Construct ();
553                                 return englishname;
554                         }
555                 }
556
557                 public static CultureInfo InstalledUICulture {
558                         get {
559                                 return ConstructCurrentCulture ();
560                         }
561                 }
562
563                 public bool IsReadOnly {
564                         get {
565                                 return m_isReadOnly;
566                         }
567                 }
568                 
569
570                 // 
571                 // IFormatProvider implementation
572                 //
573                 public virtual object GetFormat( Type formatType )
574                 {
575                         object format = null;
576
577                         if ( formatType == typeof(NumberFormatInfo) )
578                                 format = NumberFormat;
579                         else if ( formatType == typeof(DateTimeFormatInfo) )
580                                 format = DateTimeFormat;
581                         
582                         return format;
583                 }
584                 
585                 void Construct ()
586                 {
587                         construct_internal_locale_from_lcid (cultureID);
588                         constructed = true;
589                 }
590
591                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
592                 private extern bool construct_internal_locale_from_lcid (int lcid);
593
594                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
595                 private extern bool construct_internal_locale_from_name (string name);
596
597                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
598                 private extern static string get_current_locale_name ();
599
600                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
601                 private extern static CultureInfo [] internal_get_cultures (bool neutral, bool specific, bool installed);
602
603                 private void ConstructInvariant (bool read_only)
604                 {
605                         cultureID = InvariantCultureId;
606
607                         /* NumberFormatInfo defaults to the invariant data */
608                         numInfo=NumberFormatInfo.InvariantInfo;
609
610                         if (!read_only) {
611                                 numInfo = (NumberFormatInfo) numInfo.Clone ();
612                         }
613
614                         textInfo = CreateTextInfo (read_only);
615
616                         m_name=String.Empty;
617                         englishname=
618                         nativename="Invariant Language (Invariant Country)";
619                         iso3lang="IVL";
620                         iso2lang="iv";
621                         win3lang="IVL";
622                         default_calendar_type = 1 << CalendarTypeBits | (int) GregorianCalendarTypes.Localized;
623                 }
624
625                 private unsafe TextInfo CreateTextInfo (bool readOnly)
626                 {
627                         return new TextInfo (this, cultureID, this.textinfo_data, readOnly);
628                 }
629
630                 public CultureInfo (int culture) : this (culture, true) {}
631
632                 public CultureInfo (int culture, bool useUserOverride) :
633                         this (culture, useUserOverride, false) {}
634
635                 private CultureInfo (int culture, bool useUserOverride, bool read_only)
636                 {
637                         if (culture < 0)
638                                 throw new ArgumentOutOfRangeException ("culture", "Positive "
639                                         + "number required.");
640
641                         constructed = true;
642                         m_isReadOnly = read_only;
643                         m_useUserOverride = useUserOverride;
644
645                         if (culture == InvariantCultureId) {
646                                 /* Short circuit the invariant culture */
647                                 ConstructInvariant (read_only);
648                                 m_cultureData = CultureData.Invariant;
649                                 return;
650                         }
651
652                         if (!construct_internal_locale_from_lcid (culture)) {
653                                 //
654                                 // Be careful not to cause recursive CultureInfo initialization
655                                 //
656                                 var msg = string.Format (InvariantCulture, "Culture ID {0} (0x{1}) is not a supported culture.", culture.ToString (InvariantCulture), culture.ToString ("X4", InvariantCulture));
657                                 throw new CultureNotFoundException ("culture", msg);
658                         }
659
660                         m_cultureData = CultureData.GetCultureData (m_name, m_useUserOverride, datetime_index, CalendarType, number_index, iso2lang);
661                 }
662
663                 public CultureInfo (string name) : this (name, true) {}
664
665                 public CultureInfo (string name, bool useUserOverride) :
666                         this (name, useUserOverride, false) {}
667
668                 private CultureInfo (string name, bool useUserOverride, bool read_only)
669                 {
670                         if (name == null)
671                                 throw new ArgumentNullException ("name");
672
673                         constructed = true;
674                         m_isReadOnly = read_only;
675                         m_useUserOverride = useUserOverride;
676                         m_isInherited = GetType() != typeof(System.Globalization.CultureInfo);
677
678                         if (name.Length == 0) {
679                                 /* Short circuit the invariant culture */
680                                 ConstructInvariant (read_only);
681                                 m_cultureData = CultureData.Invariant;
682                                 return;
683                         }
684
685                         if (!construct_internal_locale_from_name (name.ToLowerInvariant ())) {
686                                 throw CreateNotFoundException (name);
687                         }
688
689                         m_cultureData = CultureData.GetCultureData (m_name, useUserOverride, datetime_index, CalendarType, number_index, iso2lang);
690                 }
691
692                 // This is used when creating by specific name and creating by
693                 // current locale so we can initialize the object without
694                 // doing any member initialization
695                 private CultureInfo () { constructed = true; }
696                 static Dictionary<int, CultureInfo> shared_by_number;
697                 static Dictionary<string, CultureInfo> shared_by_name;
698                 
699                 static void insert_into_shared_tables (CultureInfo c)
700                 {
701                         if (shared_by_number == null){
702                                 shared_by_number = new Dictionary<int, CultureInfo> ();
703                                 shared_by_name = new Dictionary<string, CultureInfo> ();
704                         }
705                         shared_by_number [c.cultureID] = c;
706                         shared_by_name [c.m_name] = c;
707                 }
708                 
709                 public static CultureInfo GetCultureInfo (int culture)
710                 {
711                         CultureInfo c;
712                         
713                         lock (shared_table_lock){
714                                 if (shared_by_number != null) {
715                                         if (shared_by_number.TryGetValue (culture, out c))
716                                                 return c;
717                                 }
718
719                                 c = new CultureInfo (culture, false, true);
720                                 insert_into_shared_tables (c);
721                                 return c;
722                         }
723                 }
724
725                 public static CultureInfo GetCultureInfo (string name)
726                 {
727                         if (name == null)
728                                 throw new ArgumentNullException ("name");
729
730                         CultureInfo c;
731                         lock (shared_table_lock){
732                                 if (shared_by_name != null){
733                                         if (shared_by_name.TryGetValue (name, out c))
734                                                 return c;
735                                 }
736                                 c = new CultureInfo (name, false, true);
737                                 insert_into_shared_tables (c);
738                                 return c;
739                         }
740                 }
741
742                 [MonoTODO ("Currently it ignores the altName parameter")]
743                 public static CultureInfo GetCultureInfo (string name, string altName) {
744                         if (name == null)
745                                 throw new ArgumentNullException ("null");
746                         if (altName == null)
747                                 throw new ArgumentNullException ("null");
748
749                         return GetCultureInfo (name);
750                 }
751
752                 public static CultureInfo GetCultureInfoByIetfLanguageTag (string name)
753                 {
754                         // There could be more consistent way to implement
755                         // it, but right now it works just fine with this...
756                         switch (name) {
757                         case "zh-Hans":
758                                 return GetCultureInfo ("zh-CHS");
759                         case "zh-Hant":
760                                 return GetCultureInfo ("zh-CHT");
761                         default:
762                                 return GetCultureInfo (name);
763                         }
764                 }
765
766                 // used in runtime (icall.c) to construct CultureInfo for
767                 // AssemblyName of assemblies
768                 internal static CultureInfo CreateCulture (string name, bool reference)
769                 {
770                         bool read_only;
771                         bool use_user_override;
772
773                         bool invariant = name.Length == 0;
774                         if (reference) {
775                                 use_user_override = invariant ? false : true;
776                                 read_only = false;
777                         } else {
778                                 read_only = false;
779                                 use_user_override = invariant ? false : true;
780                         }
781
782                         return new CultureInfo (name, use_user_override, read_only);
783                 }
784
785                 public static CultureInfo CreateSpecificCulture (string name)
786                 {
787                         if (name == null)
788                                 throw new ArgumentNullException ("name");
789
790                         if (name.Length == 0)
791                                 return InvariantCulture;
792
793                         var src_name = name;
794                         name = name.ToLowerInvariant ();
795                         CultureInfo ci = new CultureInfo ();
796
797                         if (!ci.construct_internal_locale_from_name (name)) {
798                                 int idx = name.IndexOf ('-');
799                                 if (idx < 1 || !ci.construct_internal_locale_from_name (name.Substring (0, idx)))
800                                         throw CreateNotFoundException (src_name);
801                         }
802
803                         if (ci.IsNeutralCulture)
804                                 ci = CreateSpecificCultureFromNeutral (ci.Name);
805
806                         ci.m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang);
807                         return ci;
808                 }
809
810                 //
811                 // Creates specific culture from neutral culture. Used by CreateSpecificCulture
812                 // only but using separate method we can delay switch underlying Dictionary
813                 // initialization
814                 //
815                 static CultureInfo CreateSpecificCultureFromNeutral (string name)
816                 {
817                         int id;
818
819                         //
820                         // For neutral cultures find predefined default specific culture
821                         //
822                         // Use managed switch because we need this for only some cultures
823                         // and the method is not used frequently
824                         //
825                         // TODO: We could optimize for cultures with single specific culture 
826                         //
827                         switch (name.ToLowerInvariant ()) {
828                         case "af": id = 1078; break;
829                         case "am": id = 1118; break;
830                         case "ar": id = 1025; break;
831                         case "arn": id = 1146; break;
832                         case "as": id = 1101; break;
833                         case "az": id = 1068; break;
834                         case "az-cyrl": id = 2092; break;
835                         case "az-latn": id = 1068; break;
836                         case "ba": id = 1133; break;
837                         case "be": id = 1059; break;
838                         case "bg": id = 1026; break;
839                         case "bn": id = 1093; break;
840                         case "bo": id = 1105; break;
841                         case "br": id = 1150; break;
842                         case "bs": id = 5146; break;
843                         case "bs-cyrl": id = 8218; break;
844                         case "bs-latn": id = 5146; break;
845                         case "ca": id = 1027; break;
846                         case "co": id = 1155; break;
847                         case "cs": id = 1029; break;
848                         case "cy": id = 1106; break;
849                         case "da": id = 1030; break;
850                         case "de": id = 1031; break;
851                         case "dsb": id = 2094; break;
852                         case "dv": id = 1125; break;
853                         case "el": id = 1032; break;
854                         case "en": id = 1033; break;
855                         case "es": id = 3082; break;
856                         case "et": id = 1061; break;
857                         case "eu": id = 1069; break;
858                         case "fa": id = 1065; break;
859                         case "fi": id = 1035; break;
860                         case "fil": id = 1124; break;
861                         case "fo": id = 1080; break;
862                         case "fr": id = 1036; break;
863                         case "fy": id = 1122; break;
864                         case "ga": id = 2108; break;
865                         case "gd": id = 1169; break;
866                         case "gl": id = 1110; break;
867                         case "gsw": id = 1156; break;
868                         case "gu": id = 1095; break;
869                         case "ha": id = 1128; break;
870                         case "ha-latn": id = 1128; break;
871                         case "he": id = 1037; break;
872                         case "hi": id = 1081; break;
873                         case "hr": id = 1050; break;
874                         case "hsb": id = 1070; break;
875                         case "hu": id = 1038; break;
876                         case "hy": id = 1067; break;
877                         case "id": id = 1057; break;
878                         case "ig": id = 1136; break;
879                         case "ii": id = 1144; break;
880                         case "is": id = 1039; break;
881                         case "it": id = 1040; break;
882                         case "iu": id = 2141; break;
883                         case "iu-cans": id = 1117; break;
884                         case "iu-latn": id = 2141; break;
885                         case "ja": id = 1041; break;
886                         case "ka": id = 1079; break;
887                         case "kk": id = 1087; break;
888                         case "kl": id = 1135; break;
889                         case "km": id = 1107; break;
890                         case "kn": id = 1099; break;
891                         case "ko": id = 1042; break;
892                         case "kok": id = 1111; break;
893                         case "ky": id = 1088; break;
894                         case "lb": id = 1134; break;
895                         case "lo": id = 1108; break;
896                         case "lt": id = 1063; break;
897                         case "lv": id = 1062; break;
898                         case "mi": id = 1153; break;
899                         case "mk": id = 1071; break;
900                         case "ml": id = 1100; break;
901                         case "mn": id = 1104; break;
902                         case "mn-cyrl": id = 1104; break;
903                         case "mn-mong": id = 2128; break;
904                         case "moh": id = 1148; break;
905                         case "mr": id = 1102; break;
906                         case "ms": id = 1086; break;
907                         case "mt": id = 1082; break;
908                         case "nb": id = 1044; break;
909                         case "ne": id = 1121; break;
910                         case "nl": id = 1043; break;
911                         case "nn": id = 2068; break;
912                         case "no": id = 1044; break;
913                         case "nso": id = 1132; break;
914                         case "oc": id = 1154; break;
915                         case "or": id = 1096; break;
916                         case "pa": id = 1094; break;
917                         case "pl": id = 1045; break;
918                         case "prs": id = 1164; break;
919                         case "ps": id = 1123; break;
920                         case "pt": id = 1046; break;
921                         case "qut": id = 1158; break;
922                         case "quz": id = 1131; break;
923                         case "rm": id = 1047; break;
924                         case "ro": id = 1048; break;
925                         case "ru": id = 1049; break;
926                         case "rw": id = 1159; break;
927                         case "sa": id = 1103; break;
928                         case "sah": id = 1157; break;
929                         case "se": id = 1083; break;
930                         case "si": id = 1115; break;
931                         case "sk": id = 1051; break;
932                         case "sl": id = 1060; break;
933                         case "sma": id = 7227; break;
934                         case "smj": id = 5179; break;
935                         case "smn": id = 9275; break;
936                         case "sms": id = 8251; break;
937                         case "sq": id = 1052; break;
938                         case "sr": id = 9242; break;
939                         case "sr-cyrl": id = 10266; break;
940                         case "sr-latn": id = 9242; break;
941                         case "sv": id = 1053; break;
942                         case "sw": id = 1089; break;
943                         case "syr": id = 1114; break;
944                         case "ta": id = 1097; break;
945                         case "te": id = 1098; break;
946                         case "tg": id = 1064; break;
947                         case "tg-cyrl": id = 1064; break;
948                         case "th": id = 1054; break;
949                         case "tk": id = 1090; break;
950                         case "tn": id = 1074; break;
951                         case "tr": id = 1055; break;
952                         case "tt": id = 1092; break;
953                         case "tzm": id = 2143; break;
954                         case "tzm-latn": id = 2143; break;
955                         case "ug": id = 1152; break;
956                         case "uk": id = 1058; break;
957                         case "ur": id = 1056; break;
958                         case "uz": id = 1091; break;
959                         case "uz-cyrl": id = 2115; break;
960                         case "uz-latn": id = 1091; break;
961                         case "vi": id = 1066; break;
962                         case "wo": id = 1160; break;
963                         case "xh": id = 1076; break;
964                         case "yo": id = 1130; break;
965                         case "zh": id = 2052; break;
966                         case "zh-chs":
967                         case "zh-hans":
968                                 id = 2052; break;
969                         case "zh-cht":
970                         case "zh-hant":
971                                 id = 3076; break;
972                         case "zu": id = 1077; break;
973                         default:
974                                 throw new NotImplementedException ("Mapping for neutral culture " + name);
975                         }
976
977                         return new CultureInfo (id);
978                 }
979
980                 internal int CalendarType {
981                         get {
982                                 switch (default_calendar_type >> CalendarTypeBits) {
983                                 case 1:
984                                         return Calendar.CAL_GREGORIAN;
985                                 case 2:
986                                         return Calendar.CAL_THAI;
987                                 case 3:
988                                         return Calendar.CAL_UMALQURA;
989                                 case 4:
990                                         return Calendar.CAL_HIJRI;
991                                 default:
992                                         throw new NotImplementedException ("CalendarType");
993                                 }
994                         }
995                 }
996
997                 static Calendar CreateCalendar (int calendarType)
998                 {
999                         string name = null;
1000                         switch (calendarType >> CalendarTypeBits) {
1001                         case 1:
1002                                 GregorianCalendarTypes greg_type;
1003                                 greg_type = (GregorianCalendarTypes) (calendarType & 0xFF);
1004                                 return new GregorianCalendar (greg_type);
1005                         case 2:
1006                                 name = "System.Globalization.ThaiBuddhistCalendar";
1007                                 break;
1008                         case 3:
1009                                 name = "System.Globalization.UmAlQuraCalendar";
1010                                 break;
1011                         case 4:
1012                                 name = "System.Globalization.HijriCalendar";
1013                                 break;
1014                         default:
1015                                 throw new NotImplementedException ("Unknown calendar type: " + calendarType);
1016                         }
1017
1018                         Type type = Type.GetType (name, false);
1019                         if (type == null)
1020                                 return new GregorianCalendar (GregorianCalendarTypes.Localized); // return invariant calendar if not found
1021                         return (Calendar) Activator.CreateInstance (type);
1022                 }
1023
1024                 static Exception CreateNotFoundException (string name)
1025                 {
1026                         return new CultureNotFoundException ("name", "Culture name " + name + " is not supported.");
1027                 }
1028                 
1029                 public static CultureInfo DefaultThreadCurrentCulture {
1030                         get {
1031                                 return Thread.default_culture;
1032                         }
1033                         set {
1034                                 Thread.default_culture = value;
1035                         }
1036                 }
1037                 
1038                 public static CultureInfo DefaultThreadCurrentUICulture {
1039                         get {
1040                                 return Thread.default_ui_culture;
1041                         }
1042                         set {
1043                                 Thread.default_ui_culture = value;
1044                         }
1045                 }
1046
1047                 internal string SortName {
1048                         get {
1049                                 return m_name;
1050                         }
1051                 }
1052
1053 #region reference sources
1054                 // TODO:
1055                 internal static readonly bool IsTaiwanSku;
1056
1057         //
1058         // CheckDomainSafetyObject throw if the object is customized object which cannot be attached to 
1059         // other object (like CultureInfo or DateTimeFormatInfo).
1060         //
1061
1062         internal static void CheckDomainSafetyObject(Object obj, Object container)
1063         {
1064             if (obj.GetType().Assembly != typeof(System.Globalization.CultureInfo).Assembly) {
1065                 
1066                 throw new InvalidOperationException(
1067                             String.Format(
1068                                 CultureInfo.CurrentCulture, 
1069                                 Environment.GetResourceString("InvalidOperation_SubclassedObject"), 
1070                                 obj.GetType(),
1071                                 container.GetType()));
1072             }
1073             Contract.EndContractBlock();
1074         }
1075
1076         // For resource lookup, we consider a culture the invariant culture by name equality.
1077         // We perform this check frequently during resource lookup, so adding a property for
1078         // improved readability.
1079         internal bool HasInvariantCultureName
1080         {
1081             get { return Name == CultureInfo.InvariantCulture.Name; }
1082         }
1083
1084         internal static bool VerifyCultureName(String cultureName, bool throwException)
1085         {
1086             // This function is used by ResourceManager.GetResourceFileName(). 
1087             // ResourceManager searches for resource using CultureInfo.Name,
1088             // so we should check against CultureInfo.Name.
1089
1090             for (int i=0; i<cultureName.Length; i++) {
1091                 char c = cultureName[i];
1092                 // 
1093
1094                 if (Char.IsLetterOrDigit(c) || c=='-' || c=='_') {
1095                     continue;
1096                 }
1097                 if (throwException) {
1098                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", cultureName));
1099                 }
1100                 return false;
1101             }
1102             return true;
1103         }
1104
1105 #endregion
1106         }
1107 }