internal static unsafe CultureData GetCurrentRegionData()
        {
            Span <char> geoIso2Letters = stackalloc char[10];

            int geoId = Interop.Kernel32.GetUserGeoID(Interop.Kernel32.GEOCLASS_NATION);

            if (geoId != Interop.Kernel32.GEOID_NOT_AVAILABLE)
            {
                int geoIsoIdLength;
                fixed(char *pGeoIsoId = geoIso2Letters)
                {
                    geoIsoIdLength = Interop.Kernel32.GetGeoInfo(geoId, Interop.Kernel32.GEO_ISO2, pGeoIsoId, geoIso2Letters.Length, 0);
                }

                if (geoIsoIdLength != 0)
                {
                    geoIsoIdLength -= geoIso2Letters[geoIsoIdLength - 1] == 0 ? 1 : 0; // handle null termination and exclude it.
                    CultureData?cd = GetCultureDataForRegion(geoIso2Letters.Slice(0, geoIsoIdLength).ToString(), true);
                    if (cd != null)
                    {
                        return(cd);
                    }
                }
            }

            // Fallback to current locale data.
            return(CultureInfo.CurrentCulture._cultureData);
        }
Example #2
0
        internal NumberFormatInfo(CultureData?cultureData)
        {
            if (cultureData != null)
            {
                // We directly use fields here since these data is coming from data table or Win32, so we
                // don't need to verify their values (except for invalid parsing situations).
                cultureData.GetNFIValues(this);

                InitializeInvariantAndNegativeSignFlags();
            }
        }
Example #3
0
        private static CultureInfo?CreateCultureInfoNoThrow(string name, bool useUserOverride)
        {
            Debug.Assert(name != null);
            CultureData?cultureData = CultureData.GetCultureData(name, useUserOverride);

            if (cultureData == null)
            {
                return(null);
            }

            return(new CultureInfo(cultureData));
        }
Example #4
0
        public CultureInfo(string name, bool useUserOverride)
        {
            ArgumentNullException.ThrowIfNull(name);

            // Get our data providing record
            CultureData?cultureData = CultureData.GetCultureData(name, useUserOverride);

            if (cultureData == null)
            {
                throw new CultureNotFoundException(nameof(name), name, GetCultureNotSupportedExceptionMessage());
            }

            _cultureData = cultureData;
            _name        = _cultureData.CultureName;
            _isInherited = GetType() != typeof(CultureInfo);
        }
Example #5
0
        /// <summary>
        /// Constructor called by SQL Server's special munged culture - creates a culture with
        /// a TextInfo and CompareInfo that come from a supplied alternate source. This object
        /// is ALWAYS read-only.
        /// Note that we really cannot use an LCID version of this override as the cached
        /// name we create for it has to include both names, and the logic for this is in
        /// the GetCultureInfo override *only*.
        /// </summary>
        internal CultureInfo(string cultureName, string textAndCompareCultureName)
        {
            if (cultureName == null)
            {
                throw new ArgumentNullException(nameof(cultureName), SR.ArgumentNull_String);
            }

            CultureData?cultureData = CultureData.GetCultureData(cultureName, false);

            if (cultureData == null)
            {
                throw new CultureNotFoundException(nameof(cultureName), cultureName, SR.Argument_CultureNotSupported);
            }

            _cultureData = cultureData;

            _name = _cultureData.CultureName;

            CultureInfo altCulture = GetCultureInfo(textAndCompareCultureName);

            _compareInfo = altCulture.CompareInfo;
            _textInfo    = altCulture.TextInfo;
        }