CultureInfo.Windows.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #if FEATURE_APPX
  5. using System.Resources;
  6. using Internal.Resources;
  7. #endif
  8. namespace System.Globalization
  9. {
  10. public partial class CultureInfo : IFormatProvider
  11. {
  12. #if FEATURE_APPX
  13. // When running under AppX, we use this to get some information about the language list
  14. private static volatile WindowsRuntimeResourceManagerBase s_WindowsRuntimeResourceManager;
  15. [ThreadStatic]
  16. private static bool ts_IsDoingAppXCultureInfoLookup;
  17. #endif
  18. internal static CultureInfo GetUserDefaultCulture()
  19. {
  20. if (GlobalizationMode.Invariant)
  21. return CultureInfo.InvariantCulture;
  22. const uint LOCALE_SNAME = 0x0000005c;
  23. const string LOCALE_NAME_USER_DEFAULT = null;
  24. const string LOCALE_NAME_SYSTEM_DEFAULT = "!x-sys-default-locale";
  25. string strDefault = CultureData.GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME);
  26. if (strDefault == null)
  27. {
  28. strDefault = CultureData.GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME);
  29. if (strDefault == null)
  30. {
  31. // If system default doesn't work, use invariant
  32. return CultureInfo.InvariantCulture;
  33. }
  34. }
  35. return GetCultureByName(strDefault);
  36. }
  37. private static CultureInfo GetUserDefaultUICulture()
  38. {
  39. #if !ENABLE_WINRT
  40. if (GlobalizationMode.Invariant)
  41. return CultureInfo.InvariantCulture;
  42. const uint MUI_LANGUAGE_NAME = 0x8; // Use ISO language (culture) name convention
  43. uint langCount = 0;
  44. uint bufLen = 0;
  45. if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, out langCount, null, ref bufLen))
  46. {
  47. char[] languages = new char[bufLen];
  48. if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, out langCount, languages, ref bufLen))
  49. {
  50. int index = 0;
  51. while (languages[index] != (char)0 && index < languages.Length)
  52. {
  53. index++;
  54. }
  55. return GetCultureByName(new string(languages, 0, index));
  56. }
  57. }
  58. #endif
  59. return InitializeUserDefaultCulture();
  60. }
  61. #if FEATURE_APPX
  62. internal static CultureInfo GetCultureInfoForUserPreferredLanguageInAppX()
  63. {
  64. // If a call to GetCultureInfoForUserPreferredLanguageInAppX() generated a recursive
  65. // call to itself, return null, since we don't want to stack overflow. For example,
  66. // this can happen if some code in this method ends up calling CultureInfo.CurrentCulture.
  67. // In this case, returning null will mean CultureInfo.CurrentCulture gets the default Win32
  68. // value, which should be fine.
  69. if (ts_IsDoingAppXCultureInfoLookup)
  70. {
  71. return null;
  72. }
  73. CultureInfo toReturn = null;
  74. try
  75. {
  76. ts_IsDoingAppXCultureInfoLookup = true;
  77. if (s_WindowsRuntimeResourceManager == null)
  78. {
  79. s_WindowsRuntimeResourceManager = ResourceManager.GetWinRTResourceManager();
  80. }
  81. toReturn = s_WindowsRuntimeResourceManager.GlobalResourceContextBestFitCultureInfo;
  82. }
  83. finally
  84. {
  85. ts_IsDoingAppXCultureInfoLookup = false;
  86. }
  87. return toReturn;
  88. }
  89. internal static bool SetCultureInfoForUserPreferredLanguageInAppX(CultureInfo ci)
  90. {
  91. if (s_WindowsRuntimeResourceManager == null)
  92. {
  93. s_WindowsRuntimeResourceManager = ResourceManager.GetWinRTResourceManager();
  94. }
  95. return s_WindowsRuntimeResourceManager.SetGlobalResourceContextDefaultCulture(ci);
  96. }
  97. #endif
  98. }
  99. }