CultureInfo.Windows.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. string? strDefault = CultureData.GetLocaleInfoEx(Interop.Kernel32.LOCALE_NAME_USER_DEFAULT, Interop.Kernel32.LOCALE_SNAME);
  23. if (strDefault == null)
  24. {
  25. strDefault = CultureData.GetLocaleInfoEx(Interop.Kernel32.LOCALE_NAME_SYSTEM_DEFAULT, Interop.Kernel32.LOCALE_SNAME);
  26. if (strDefault == null)
  27. {
  28. // If system default doesn't work, use invariant
  29. return CultureInfo.InvariantCulture;
  30. }
  31. }
  32. return GetCultureByName(strDefault);
  33. }
  34. private static unsafe CultureInfo GetUserDefaultUICulture()
  35. {
  36. #if !ENABLE_WINRT
  37. if (GlobalizationMode.Invariant)
  38. return CultureInfo.InvariantCulture;
  39. const uint MUI_LANGUAGE_NAME = 0x8; // Use ISO language (culture) name convention
  40. uint langCount = 0;
  41. uint bufLen = 0;
  42. if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &langCount, null, &bufLen) != Interop.BOOL.FALSE)
  43. {
  44. char[] languages = new char[bufLen];
  45. fixed (char* pLanguages = languages)
  46. {
  47. if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &langCount, pLanguages, &bufLen) != Interop.BOOL.FALSE)
  48. {
  49. int index = 0;
  50. while (languages[index] != (char)0 && index < languages.Length)
  51. {
  52. index++;
  53. }
  54. return GetCultureByName(new string(languages, 0, index));
  55. }
  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;
  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. }