PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/corlib/System.Globalization/CultureInfo.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 292 lines | 245 code | 41 blank | 6 comment | 21 complexity | 0fa702f98b5cd20f08f2ae298b2d5ce6 MD5 | raw file
  1. #if !LOCALTEST
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace System.Globalization {
  6. public class CultureInfo:IFormatProvider {
  7. #region Static methods
  8. private static Dictionary<string, CultureInfo> shareByName = new Dictionary<string,CultureInfo>();
  9. private static CultureInfo invariantCulture = null;
  10. public static CultureInfo GetCultureInfo(string name) {
  11. // Always use lower-case version of name
  12. lock (shareByName) {
  13. CultureInfo ci;
  14. if (!shareByName.TryGetValue(name.ToLowerInvariant(), out ci)) {
  15. ci = new CultureInfo(name);
  16. // Don't put in cache, as the constructor already does this
  17. }
  18. return ci;
  19. }
  20. }
  21. public static CultureInfo CurrentCulture {
  22. get {
  23. return Thread.CurrentThread.CurrentCulture;
  24. }
  25. }
  26. public static CultureInfo InvariantCulture {
  27. get {
  28. if (invariantCulture == null) {
  29. invariantCulture = new CultureInfo(string.Empty);
  30. }
  31. return invariantCulture;
  32. }
  33. }
  34. public static CultureInfo[] GetCultures(CultureTypes types) {
  35. DirectoryInfo cultureDir = new DirectoryInfo(Environment.CultureDirectory);
  36. List<CultureInfo> ret = new List<CultureInfo>();
  37. foreach (FileInfo fi in cultureDir.GetFiles()) {
  38. CultureInfo ci = CultureInfo.GetCultureInfo(fi.Name);
  39. if ((ci.cultureTypes & types) > 0) {
  40. ret.Add(ci);
  41. }
  42. }
  43. return ret.ToArray();
  44. }
  45. #endregion
  46. private string name;
  47. private int lcid;
  48. private string parentName;
  49. private CultureInfo parent = null;
  50. private string displayName;
  51. private string englishName;
  52. private string nativeName;
  53. private string twoLetterISOLanguageName;
  54. private string threeLetterISOLanguageName;
  55. private string threeLetterWindowsLanguageName;
  56. private CultureTypes cultureTypes;
  57. private string ietfLanguageTag;
  58. private bool isNeutralCulture;
  59. private NumberFormatInfo numberFormatInfo;
  60. private TextInfo textInfo;
  61. private DateTimeFormatInfo dateTimeFormat;
  62. public CultureInfo(string name) {
  63. if (name == null) {
  64. throw new ArgumentNullException();
  65. }
  66. if (name.Length == 0) {
  67. ConstructInvariant();
  68. return;
  69. }
  70. // Always use lower-case version of name
  71. string nameLower = name.ToLowerInvariant();
  72. // If this culture is already loaded and cached, then just copy all of its settings
  73. lock (shareByName) {
  74. CultureInfo cached;
  75. if (shareByName.TryGetValue(nameLower, out cached)) {
  76. CopyFrom(cached);
  77. return;
  78. }
  79. }
  80. // Not cached, so create from new and place in cache
  81. ConstructFromFile(name);
  82. }
  83. private void ConstructInvariant() {
  84. this.name = string.Empty;
  85. this.displayName =
  86. this.englishName =
  87. this.nativeName = "Invariant Language (Invariant Country)";
  88. this.lcid = 0x7f;
  89. this.numberFormatInfo = NumberFormatInfo.InvariantInfo;
  90. this.dateTimeFormat = DateTimeFormatInfo.InvariantInfo;
  91. }
  92. private void ConstructFromFile(string name) {
  93. string fileName = Environment.CultureDirectory + Path.DirectorySeparatorStr + name;
  94. try {
  95. using (StreamReader s = File.OpenText(fileName)) {
  96. this.name = s.ReadLine();
  97. this.lcid = int.Parse(s.ReadLine().Substring(2), NumberStyles.HexNumber);
  98. this.parentName = s.ReadLine();
  99. this.englishName = s.ReadLine();
  100. this.nativeName = s.ReadLine();
  101. this.displayName = s.ReadLine();
  102. this.twoLetterISOLanguageName = s.ReadLine();
  103. this.threeLetterISOLanguageName = s.ReadLine();
  104. this.threeLetterWindowsLanguageName = s.ReadLine();
  105. string calendarName = s.ReadLine(); // Calendar
  106. s.ReadLine(); // Optional calendars
  107. this.cultureTypes = (CultureTypes)int.Parse(s.ReadLine());
  108. this.ietfLanguageTag = s.ReadLine();
  109. this.isNeutralCulture = bool.Parse(s.ReadLine());
  110. this.textInfo = new TextInfo(this, s);
  111. if (!this.isNeutralCulture) {
  112. this.numberFormatInfo = new NumberFormatInfo(s);
  113. this.dateTimeFormat = new DateTimeFormatInfo(s, calendarName);
  114. } else {
  115. this.numberFormatInfo = null;
  116. this.dateTimeFormat = null;
  117. }
  118. }
  119. } catch (FileNotFoundException) {
  120. throw new ArgumentException(string.Format("{0} is not a valid culture", name));
  121. }
  122. lock (shareByName) {
  123. shareByName.Add(name.ToLowerInvariant(), this);
  124. }
  125. }
  126. private void CopyFrom(CultureInfo ci) {
  127. this.name = ci.name;
  128. this.lcid = ci.lcid;
  129. this.parent = ci.parent;
  130. this.englishName = ci.englishName;
  131. this.nativeName = ci.nativeName;
  132. this.displayName = ci.displayName;
  133. this.twoLetterISOLanguageName = ci.twoLetterISOLanguageName;
  134. this.threeLetterISOLanguageName = ci.threeLetterISOLanguageName;
  135. this.threeLetterWindowsLanguageName = ci.threeLetterWindowsLanguageName;
  136. this.cultureTypes = ci.cultureTypes;
  137. this.ietfLanguageTag = ci.ietfLanguageTag;
  138. this.isNeutralCulture = ci.isNeutralCulture;
  139. this.textInfo = ci.textInfo;
  140. this.numberFormatInfo = ci.numberFormatInfo;
  141. this.dateTimeFormat = ci.dateTimeFormat;
  142. }
  143. public bool IsReadOnly {
  144. get{
  145. // At the moment, all CultureInfo's are read-only
  146. return true;
  147. }
  148. }
  149. public virtual NumberFormatInfo NumberFormat {
  150. get {
  151. if (this.numberFormatInfo == null) {
  152. throw new NotSupportedException("Not supported for neutral cultures");
  153. }
  154. return this.numberFormatInfo;
  155. }
  156. }
  157. public virtual DateTimeFormatInfo DateTimeFormat {
  158. get {
  159. if (this.dateTimeFormat == null) {
  160. throw new NotSupportedException("Not supported for neutral cultures");
  161. }
  162. return this.dateTimeFormat;
  163. }
  164. }
  165. public virtual int LCID {
  166. get {
  167. return this.lcid;
  168. }
  169. }
  170. public virtual string Name {
  171. get {
  172. return this.name;
  173. }
  174. }
  175. public virtual CultureInfo Parent {
  176. get {
  177. if (this.parent == null) {
  178. this.parent = CultureInfo.GetCultureInfo(this.parentName);
  179. }
  180. return this.parent;
  181. }
  182. }
  183. public virtual string DisplayName {
  184. get {
  185. return this.displayName;
  186. }
  187. }
  188. public virtual string EnglishName {
  189. get {
  190. return this.englishName;
  191. }
  192. }
  193. public virtual string NativeName {
  194. get {
  195. return this.nativeName;
  196. }
  197. }
  198. public virtual string TwoLetterISOLanguageName {
  199. get {
  200. return this.twoLetterISOLanguageName;
  201. }
  202. }
  203. public virtual string ThreeLetterISOLanguageName {
  204. get {
  205. return this.threeLetterISOLanguageName;
  206. }
  207. }
  208. public virtual string ThreeLetterWindowsLanguageName {
  209. get {
  210. return this.threeLetterWindowsLanguageName;
  211. }
  212. }
  213. public virtual CultureTypes CultureTypes {
  214. get {
  215. return this.cultureTypes;
  216. }
  217. }
  218. public virtual string IetfLanguageTag {
  219. get {
  220. return this.ietfLanguageTag;
  221. }
  222. }
  223. public virtual bool IsNeutralCulture {
  224. get {
  225. return this.isNeutralCulture;
  226. }
  227. }
  228. public virtual TextInfo TextInfo {
  229. get {
  230. return this.textInfo;
  231. }
  232. }
  233. public override string ToString() {
  234. return this.name;
  235. }
  236. public virtual object GetFormat(Type formatType)
  237. {
  238. object dateTimeFormat = null;
  239. if (formatType == typeof(NumberFormatInfo))
  240. {
  241. return this.NumberFormat;
  242. }
  243. if (formatType == typeof(DateTimeFormatInfo))
  244. {
  245. dateTimeFormat = this.DateTimeFormat;
  246. }
  247. return dateTimeFormat;
  248. }
  249. }
  250. }
  251. #endif