Exemplo n.º 1
0
        protected override void InitInternal(ICollection <string> locales, CollatorOptions options)
        {
            this.Usage = options.Usage;
            LocaleMatcher matcher = options.LocaleMatcher;
            Hashtable     ht      = new Hashtable();

            ht["kn"] = options.Numeric == true ? "" : null;
            ht["kf"] = IntlProviderOptions.ToStringValue(options.CaseFirst);

            this.Locale      = ResolveLocale(locales, matcher, relevantExtensionKeys, ht, out ht);
            this.Collation   = (string)ht["co"];
            this.Numeric     = String.Empty.Equals(ht["kn"]);
            this.CaseFirst   = IntlProviderOptions.ParseEnum <CollatorCaseFirst>((string)ht["kf"]);
            this.Sensitivity = options.Sensitivity;
            if (this.Sensitivity == CollatorSensitivity.Unspecified)
            {
                if (this.Usage == CollatorUsage.Sort)
                {
                    this.Sensitivity = CollatorSensitivity.Variant;
                }
                else
                {
                    this.Sensitivity = CollatorSensitivity.Variant;
                }
            }
            this.IgnorePunctuation = options.IgnorePunctuation;
            this.BoundCompare      = Literal.FunctionLiteral(this.Compare);
            this.comparer          = CultureInfo.GetCultureInfo(this.Locale).CompareInfo;
        }
Exemplo n.º 2
0
        protected override void InitInternal(ICollection <string> locales, PluralRulesOptions options)
        {
            LocaleMatcher matcher = options.LocaleMatcher;

            this.Type             = options.Type;
            this.Digits           = options.Digits;
            this.Locale           = ResolveLocale(locales, matcher);
            this.resolver         = CldrPluralRules.Resolve(this.Type, this.Locale);
            this.PluralCategories = resolver.PluralCategories;
        }
Exemplo n.º 3
0
        protected override void InitInternal(ICollection <string> locales, RelativeTimeFormatOptions options)
        {
            LocaleMatcher matcher = options.LocaleMatcher;
            Hashtable     ht      = new Hashtable();

            ht["nu"]             = options.NumberingSystem;
            this.Locale          = ResolveLocale(locales, matcher, relevantExtensionKeys, ht, out ht);
            this.NumberingSystem = (string)ht["nu"];
            this.Style           = options.Style;
            this.Numeric         = options.Numeric;
            this.NumberFormat    = new NumberFormat(this.Locale);
            this.BoundFormat     = Literal.FunctionLiteral(this.FormatInternal);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public static string GetBestAvailableLocale(ICollection <string> availableLocales, ICollection <string> requestedLocales, LocaleMatcher lookupMatcher, out string extension)
        {
            Guard.ArgumentNotNull(availableLocales, "availableLocales");
            Guard.ArgumentNotNull(requestedLocales, "requestedLocales");
            ICollection <string> candidates = requestedLocales.Count == 0 ? new[] { IntlContext.DefaultLocale } : requestedLocales;

            foreach (string locale in candidates)
            {
                string normalized = RemoveUnicodeExtensions(locale);
                string matched    = GetBestAvailableLocale(availableLocales, normalized);
                extension = locale.Substring(normalized.Length);
                if (matched != null)
                {
                    return(matched);
                }
                if (lookupMatcher == LocaleMatcher.BestFit)
                {
                    BcpLanguageTagBuilder candidate = new BcpLanguageTagBuilder(BcpLanguageTag.Parse(normalized).Maximize());
                    matched = GetBestAvailableLocale(availableLocales, candidate.ToString());
                    if (matched == null && candidate.Script != "")
                    {
                        candidate.Script = "";
                        matched          = GetBestAvailableLocale(availableLocales, candidate.ToString());
                    }
                    if (matched != null)
                    {
                        return(matched);
                    }
                }
            }
            if (requestedLocales.Count > 0)
            {
                return(GetBestAvailableLocale(availableLocales, new string[0], lookupMatcher, out extension));
            }
            extension = "";
            return(availableLocales.First());
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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)));
        }
Exemplo n.º 8
0
 protected string ResolveLocale(ICollection <string> locales, LocaleMatcher matcher, IList <string> relevantExtensionKeys, Hashtable options)
 {
     return(ResolveLocale(locales, matcher, relevantExtensionKeys, options, out _));
 }
Exemplo n.º 9
0
 protected string ResolveLocale(ICollection <string> locales, LocaleMatcher matcher)
 {
     return(ResolveLocale(locales, matcher, null, null, out _));
 }