| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- #if FEATURE_APPX
- using System.Resources;
- using Internal.Resources;
- #endif
- namespace System.Globalization
- {
- public partial class CultureInfo : IFormatProvider
- {
- #if FEATURE_APPX
- // When running under AppX, we use this to get some information about the language list
- private static volatile WindowsRuntimeResourceManagerBase s_WindowsRuntimeResourceManager;
- [ThreadStatic]
- private static bool ts_IsDoingAppXCultureInfoLookup;
- #endif
- internal static CultureInfo GetUserDefaultCulture()
- {
- if (GlobalizationMode.Invariant)
- return CultureInfo.InvariantCulture;
- const uint LOCALE_SNAME = 0x0000005c;
- const string LOCALE_NAME_USER_DEFAULT = null;
- const string LOCALE_NAME_SYSTEM_DEFAULT = "!x-sys-default-locale";
- string strDefault = CultureData.GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME);
- if (strDefault == null)
- {
- strDefault = CultureData.GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME);
- if (strDefault == null)
- {
- // If system default doesn't work, use invariant
- return CultureInfo.InvariantCulture;
- }
- }
- return GetCultureByName(strDefault);
- }
- private static CultureInfo GetUserDefaultUICulture()
- {
- #if !ENABLE_WINRT
- if (GlobalizationMode.Invariant)
- return CultureInfo.InvariantCulture;
- const uint MUI_LANGUAGE_NAME = 0x8; // Use ISO language (culture) name convention
- uint langCount = 0;
- uint bufLen = 0;
- if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, out langCount, null, ref bufLen))
- {
- char[] languages = new char[bufLen];
- if (Interop.Kernel32.GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, out langCount, languages, ref bufLen))
- {
- int index = 0;
- while (languages[index] != (char)0 && index < languages.Length)
- {
- index++;
- }
- return GetCultureByName(new string(languages, 0, index));
- }
- }
- #endif
- return InitializeUserDefaultCulture();
- }
- #if FEATURE_APPX
- internal static CultureInfo GetCultureInfoForUserPreferredLanguageInAppX()
- {
- // If a call to GetCultureInfoForUserPreferredLanguageInAppX() generated a recursive
- // call to itself, return null, since we don't want to stack overflow. For example,
- // this can happen if some code in this method ends up calling CultureInfo.CurrentCulture.
- // In this case, returning null will mean CultureInfo.CurrentCulture gets the default Win32
- // value, which should be fine.
- if (ts_IsDoingAppXCultureInfoLookup)
- {
- return null;
- }
- CultureInfo toReturn = null;
- try
- {
- ts_IsDoingAppXCultureInfoLookup = true;
- if (s_WindowsRuntimeResourceManager == null)
- {
- s_WindowsRuntimeResourceManager = ResourceManager.GetWinRTResourceManager();
- }
- toReturn = s_WindowsRuntimeResourceManager.GlobalResourceContextBestFitCultureInfo;
- }
- finally
- {
- ts_IsDoingAppXCultureInfoLookup = false;
- }
- return toReturn;
- }
- internal static bool SetCultureInfoForUserPreferredLanguageInAppX(CultureInfo ci)
- {
- if (s_WindowsRuntimeResourceManager == null)
- {
- s_WindowsRuntimeResourceManager = ResourceManager.GetWinRTResourceManager();
- }
- return s_WindowsRuntimeResourceManager.SetGlobalResourceContextDefaultCulture(ci);
- }
- #endif
- }
- }
|