示例#1
0
        public static EcmaValue SupportedLocalesOf(EcmaValue locales, EcmaValue options)
        {
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);
            List <string>        result           = IntlUtility.GetSupportedLocales(new[] { "en" }, requestedLocales, new ListFormatOptions(options));

            return(new EcmaArray(result.Select(v => (EcmaValue)v).ToArray()));
        }
示例#2
0
        public static EcmaValue SupportedLocalesOf(EcmaValue locales, EcmaValue options)
        {
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);
            List <string>        result           = IntlUtility.GetSupportedLocales(CldrPluralRules.AvailableLocales, requestedLocales, new PluralRulesOptions(options));

            return(new EcmaArray(result.Select(v => (EcmaValue)v).ToArray()));
        }
示例#3
0
        public static EcmaValue DateTimeFormat([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            DateTimeFormat       formatter        = thisValue.GetUnderlyingObject <DateTimeFormat>();
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);

            formatter.Init(requestedLocales, CreateOptions(options, true, true, true, false));
            return(thisValue);
        }
示例#4
0
        public static EcmaValue StringPrototypeToLocaleUpperCase([This] EcmaValue thisValue, EcmaValue locales)
        {
            Guard.RequireObjectCoercible(thisValue);
            string str    = thisValue.ToStringOrThrow();
            string locale = IntlUtility.GetBestAvailableLocale(IntlUtility.SystemLocales, IntlUtility.CanonicalizeLocaleList(locales), LocaleMatcher.BestFit, out _);

            return(CultureInfo.GetCultureInfo(locale).TextInfo.ToUpper(str));
        }
示例#5
0
        public static EcmaValue NumberFormat([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            ListFormat           formatter        = thisValue.GetUnderlyingObject <ListFormat>();
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);

            formatter.Init(requestedLocales, new ListFormatOptions(options));
            return(thisValue);
        }
示例#6
0
        public static EcmaValue PluralRules([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            PluralRules          rules            = thisValue.GetUnderlyingObject <PluralRules>();
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);

            rules.Init(requestedLocales, new PluralRulesOptions(options));
            return(thisValue);
        }
示例#7
0
        public static EcmaValue Collator([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            Collator             collator         = thisValue.GetUnderlyingObject <Collator>();
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);

            collator.Init(requestedLocales, new CollatorOptions(options));
            return(thisValue);
        }
示例#8
0
        public static EcmaValue Locale([This] EcmaValue thisValue, EcmaValue locale, EcmaValue options)
        {
            Locale localeObj = thisValue.GetUnderlyingObject <Locale>();

            localeObj.Init(new List <string> {
                IntlUtility.CanonicalizeLanguageTag(locale)
            }, new LocaleOptions(options));
            return(thisValue);
        }
示例#9
0
        public static EcmaValue DatePrototypeToLocaleTimeString([This] EcmaValue thisValue, EcmaValue locales, EcmaValue options)
        {
            EcmaTimestamp dt = thisValue.GetUnderlyingObject <EcmaDate>().Timestamp;

            if (!dt.IsValid)
            {
                return("Invalid Date");
            }
            DateTimeFormat       formatter        = new DateTimeFormat();
            ICollection <string> requestedLocales = IntlUtility.CanonicalizeLocaleList(locales);

            formatter.Init(requestedLocales, DateTimeFormatConstructor.CreateOptions(options, false, true, false, false));
            return(formatter.Format(thisValue).ToString());
        }
示例#10
0
        protected override void InitInternal(ICollection <string> locales, DateTimeFormatOptions options)
        {
            Hashtable     ht      = new Hashtable();
            LocaleMatcher matcher = options.LocaleMatcher;

            ht["ca"] = options.Calendar;
            ht["nu"] = options.NumberingSystem;

            bool?     hour12    = options.Hour12;
            HourCycle hourCycle = options.HourCycle;

            if (hour12 != default)
            {
                hourCycle = HourCycle.Unspecified;
            }
            ht["hc"]             = IntlProviderOptions.ToStringValue(hourCycle);
            this.Locale          = ResolveLocale(locales, matcher, relevantExtensionKeys, ht, out ht);
            this.Calendar        = (string)ht["ca"];
            this.NumberingSystem = (string)ht["nu"];

            string tz = options.TimeZone;

            if (tz != null)
            {
                if (!IntlUtility.IsValidTimeZoneName(tz))
                {
                    throw new EcmaRangeErrorException("Invalid time zone specified: {0}", tz);
                }
                this.TimeZone = IntlUtility.CanonicalizeTimeZoneName(tz);
            }
            else
            {
                this.TimeZone = IntlContext.DefaultTimeZone;
            }
            this.FormatMatcher = options.FormatMatcher;

            DateTimePartStyles styles         = new DateTimePartStyles(options);
            HourCycle          finalHourCycle = IntlProviderOptions.ParseEnum <HourCycle>((string)ht["hc"]);

            this.HourCycle = hour12 == default ? finalHourCycle : NormalizeHourCycle(finalHourCycle, hour12.Value);
            this.Hour12    = this.HourCycle == HourCycle.Hour11 || this.HourCycle == HourCycle.Hour12;

            this.format             = GetBestFormat(styles);
            this.calendar           = IntlUtility.SupportedCalendars[this.Calendar];
            this.DateTimePartStyles = format.Styles;
            this.BoundFormat        = Literal.FunctionLiteral(this.FormatInternal);
        }
示例#11
0
        private CldrDateTimeFormat GetBestFormat(DateTimePartStyles options)
        {
            string locale = IntlUtility.RemoveUnicodeExtensions(this.Locale);
            ReadOnlyCollection <CldrDateTimeFormat> formats;

            if (options.IsDateOnly)
            {
                formats = CldrCalendarInfo.Resolve(locale, this.Calendar).GetAvailableDateFormats();
            }
            else if (options.IsTimeOnly)
            {
                formats = CldrCalendarInfo.Resolve(locale, "generic").GetAvailableTimeFormats();
            }
            else
            {
                formats = CldrCalendarInfo.Resolve(locale, this.Calendar).GetAvailableDateTimeFormats();
            }
            bool isDateOnly = options.IsDateOnly;
            bool isHour12   = this.Hour12;
            int  bestScore  = Int32.MinValue;
            CldrDateTimeFormat bestFormat = null;

            foreach (CldrDateTimeFormat format in formats)
            {
                if (isDateOnly || isHour12 == format.Styles.IsHour12)
                {
                    int score = format.Styles.Match(options);
                    if (score > bestScore)
                    {
                        bestScore  = score;
                        bestFormat = format;
                    }
                }
            }
            return(bestFormat);
        }
示例#12
0
        public static EcmaValue BaseName([This] EcmaValue thisValue)
        {
            Locale locale = thisValue.GetUnderlyingObject <Locale>();

            return(IntlUtility.RemoveUnicodeExtensions(locale.LocaleString));
        }
示例#13
0
 private static void Initialize()
 {
     lock (typeof(IntlContext)) {
         bool isTimeZoneAutoDetected = defaultTimeZone == null;
         if (defaultTimeZone == null)
         {
             TimeZoneInfo localTimeZone = TimeZoneInfo.Local;
             if (IntlUtility.IsValidTimeZoneName(localTimeZone.Id))
             {
                 defaultTimeZone = IntlUtility.CanonicalizeTimeZoneName(localTimeZone.Id);
             }
             else
             {
                 if (Environment.OSVersion.Platform != PlatformID.Unix && TZConvert.TryWindowsToIana(localTimeZone.StandardName, out string result))
                 {
                     defaultTimeZone = IntlUtility.CanonicalizeTimeZoneName(result);
                 }
                 if (defaultTimeZone == null)
                 {
                     TimeZoneInfo[] systemIanaTimezones = TimeZoneInfo.GetSystemTimeZones().Where(v => IntlUtility.IsValidTimeZoneName(v.Id)).ToArray();
                     TimeZoneInfo   matched             = systemIanaTimezones.FirstOrDefault(v => v.HasSameRules(localTimeZone)) ?? systemIanaTimezones.FirstOrDefault(v => v.BaseUtcOffset == localTimeZone.BaseUtcOffset);
                     if (matched != null)
                     {
                         defaultTimeZone = IntlUtility.CanonicalizeTimeZoneName(matched.Id);
                     }
                 }
             }
             if (defaultTimeZone == null)
             {
                 defaultTimeZone = "UTC";
             }
         }
         if (regionCode == null)
         {
             regionCode = RegionInfo.CurrentRegion.TwoLetterISORegionName;
             if (regionCode == "IV")
             {
                 regionCode = IntlUtility.GetRegionCodeFromTimeZone(defaultTimeZone);
             }
         }
         if (defaultLocale == null)
         {
             string systemLocale = CultureInfo.CurrentUICulture.Name;
             if (String.IsNullOrEmpty(systemLocale))
             {
                 defaultLocale = CldrUtility.GetDefaultLanguage(regionCode);
             }
             else
             {
                 defaultLocale = IntlUtility.CanonicalizeLanguageTag(systemLocale);
             }
         }
         if (isTimeZoneAutoDetected && IntlUtility.GetRegionCodeFromTimeZone(defaultTimeZone) == "001" && regionCode != "001")
         {
             string timeZone = IntlUtility.GetTimeZoneFromRegionCode(regionCode);
             if (timeZone != null && TZConvert.IanaToWindows(timeZone) == TZConvert.IanaToWindows(defaultTimeZone))
             {
                 defaultTimeZone = timeZone;
             }
         }
     }
 }
示例#14
0
        protected override void InitInternal(ICollection <string> locales, NumberFormatOptions options)
        {
            Hashtable     ht            = new Hashtable();
            LocaleMatcher localeMatcher = options.LocaleMatcher;

            ht["nu"]             = options.NumberingSystem;
            this.Locale          = ResolveLocale(locales, localeMatcher, relevantExtensionKeys, ht, out ht);
            this.NumberingSystem = (string)ht["nu"];
            this.Style           = options.Style;

            string currency = options.Currency;

            if (currency != null)
            {
                if (!IntlUtility.IsWellFormedCurrencyCode(currency))
                {
                    throw new EcmaRangeErrorException("Invalid currency codes: {0}", currency);
                }
                this.Currency = currency;
            }
            this.CurrencyDisplay = options.CurrencyDisplay;
            this.CurrencySign    = options.CurrencySign;

            this.Unit = options.Unit;
            if (this.Unit != null)
            {
                if (!IntlUtility.IsWellFormedUnitIdentifier(this.Unit))
                {
                    throw new EcmaRangeErrorException("Invalid unit identifier: {0}", this.Unit);
                }
            }
            this.UnitDisplay = options.UnitDisplay;

            NumberFormatInfo numberFormat = CultureInfo.GetCultureInfo(this.Locale).NumberFormat;
            int defaultMinFractionDigits, defaultMaxFractionDigits;

            if (this.Style == NumberStyle.Currency)
            {
                if (currency == null)
                {
                    throw new EcmaTypeErrorException("Currency code is required with currency style");
                }
                defaultMinFractionDigits = numberFormat.CurrencyDecimalDigits;
                defaultMaxFractionDigits = numberFormat.CurrencyDecimalDigits;
            }
            else
            {
                if (this.Style == NumberStyle.Unit && this.Unit == null)
                {
                    throw new EcmaTypeErrorException("Unit is required with unit style");
                }
                defaultMinFractionDigits = 0;
                defaultMaxFractionDigits = this.Style == NumberStyle.Percent ? 0 : 3;
            }
            this.Notation = options.Notation;
            this.Digits   = options.GetNumberFormatDigitOptions(defaultMinFractionDigits, defaultMaxFractionDigits, this.Notation);

            NumberCompactDisplayFormat compactDisplay = options.CompactDisplay;

            if (this.Notation == NumberNotation.Compact)
            {
                this.CompactDisplay = compactDisplay;
            }
            this.UseGrouping    = options.UseGrouping;
            this.SignDisplay    = options.SignDisplay;
            this.BoundFormat    = Literal.FunctionLiteral(this.FormatInternal);
            this.formatProvider = CldrNumberFormat.GetFormat(this.Locale, this.NumberingSystem);
            this.pluralRules    = CldrPluralRules.Resolve(PluralRuleType.Cardinal, this.Locale);

            NumberFormatInfo formatter = formatProvider.FormatProvider;

            this.symbolType = new Dictionary <string, FormattedPartType> {
                [formatter.PositiveSign]           = FormattedPartType.PlusSign,
                [formatter.NegativeSign]           = FormattedPartType.MinusSign,
                [formatter.NumberGroupSeparator]   = FormattedPartType.Group,
                [formatter.NumberDecimalSeparator] = FormattedPartType.Decimal,
                [formatter.PercentSymbol]          = FormattedPartType.PercentSign,
                ["E"] = FormattedPartType.ExponentSeparator
            };
            this.styleFormats = GetStyleFormat().ToDictionary(v => v.Key, v => v.Value);
            if (this.Notation == NumberNotation.Compact)
            {
                this.compactNotations = formatProvider.GetCompactNotationFormats(this.CompactDisplay);
            }
            else if (this.Notation == NumberNotation.Scientific)
            {
                this.notationFormats = formatProvider.GetScientificNotationFormat().ToDisplayFormat(this);
            }
            else
            {
                this.notationFormats = formatProvider.GetDecimalNotationFormat().ToDisplayFormat(this);
            }
        }
示例#15
0
        protected string ResolveLocale(ICollection <string> locales, LocaleMatcher matcher, IList <string> relevantExtensionKeys, Hashtable options, out Hashtable properties)
        {
            Guard.ArgumentNotNull(locales, "locales");
            ICollection <string> availableLocales = this.AvailableLocales;

            if (availableLocales == null)
            {
                throw new InvalidOperationException("IntlProvider.AvailableLocales must not return null");
            }
            string extension;
            string matched            = IntlUtility.GetBestAvailableLocale(availableLocales, locales, matcher, out extension);
            string supportedExtension = "-u";

            properties = new Hashtable();
            if (relevantExtensionKeys != null)
            {
                foreach (string key in relevantExtensionKeys)
                {
                    IList <string> supportedValue             = IntlUtility.SupportedValues[key];
                    string         value                      = IntlUtility.GetDefaultExtensionValue(key);
                    string         supportedExtensionAddition = "";
                    if (extension != "")
                    {
                        string requestedValue = IntlUtility.GetUnicodeExtensionValue(extension, key);
                        if (requestedValue != null && supportedValue.Contains(requestedValue))
                        {
                            value = requestedValue;
                            if (value != "")
                            {
                                supportedExtensionAddition += "-" + key + "-" + value;
                            }
                            else
                            {
                                supportedExtensionAddition += "-" + key;
                            }
                        }
                    }
                    if (options[key] != null)
                    {
                        string optionValue = options[key].ToString();
                        if (supportedValue.Contains(optionValue) && optionValue != value)
                        {
                            value = optionValue;
                            supportedExtensionAddition = "";
                        }
                    }
                    properties[key]     = value;
                    supportedExtension += supportedExtensionAddition;
                }
            }
            if (supportedExtension.Length <= 2)
            {
                return(IntlUtility.CanonicalizeLanguageTag(matched));
            }
            int pos = matched.IndexOf("-x-");

            if (pos < 0)
            {
                return(IntlUtility.CanonicalizeLanguageTag(matched + supportedExtension));
            }
            return(IntlUtility.CanonicalizeLanguageTag(matched.Substring(0, pos) + supportedExtension + matched.Substring(pos)));
        }
示例#16
0
 public IntlProvider(SharedObjectHandle proto, ICollection <string> locale, T options)
     : base(proto)
 {
     Init(IntlUtility.CanonicalizeLocaleList(locale), options);
 }
示例#17
0
 public IntlProvider(SharedObjectHandle proto, string locale, T options)
     : base(proto)
 {
     Init(IntlUtility.CanonicalizeLocaleList(new[] { locale }), options);
 }
示例#18
0
        public static EcmaValue GetCanonicalLocales(EcmaValue locales)
        {
            ICollection <string> result = IntlUtility.CanonicalizeLocaleList(locales);

            return(new EcmaArray(result.Select(v => (EcmaValue)v).ToArray()));
        }