コード例 #1
0
ファイル: Collator.cs プロジェクト: misonou/codeless-ecma
        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;
        }
コード例 #2
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);
        }
コード例 #3
0
        public FormattedString Format(EcmaValue value, EcmaValue unit, out string[] units)
        {
            EcmaValue number  = value.ToNumber();
            string    unitStr = unit.ToStringOrThrow();

            if (number.IsNaN || !number.IsFinite)
            {
                throw new EcmaRangeErrorException("Value need to be finite number for Intl.RelativeTimeFormat.prototype.format()");
            }
            if (singularForms.TryGetValue(unitStr, out string singular))
            {
                unitStr = singular;
            }
            double           doubleValue = number.ToDouble();
            RelativeTimeUnit parsedUnit  = IntlProviderOptions.ParseEnum <RelativeTimeUnit>(unitStr);

            return(Format(doubleValue, parsedUnit, out units));
        }
コード例 #4
0
        protected override void InitInternal(ICollection <string> collection, LocaleOptions options)
        {
            if (collection.Count != 1)
            {
                throw new InvalidOperationException();
            }
            BcpLanguageTagBuilder builder = new BcpLanguageTagBuilder(collection.First());
            string language = options.Language;

            if (language != null)
            {
                try {
                    builder.Language = language;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            string script = options.Script;

            if (script != null)
            {
                try {
                    builder.Script = script;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            string region = options.Region;

            if (region != null)
            {
                try {
                    builder.Region = region;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            string calendar = options.Calendar;

            if (calendar != null)
            {
                try {
                    builder.UExtensions["ca"] = calendar;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            string collation = options.Collation;

            if (collation != null)
            {
                try {
                    builder.UExtensions["co"] = collation;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            string hourCycle = options.HourCycle;

            if (hourCycle != null)
            {
                IntlProviderOptions.ParseEnum <HourCycle>(hourCycle);
                builder.UExtensions["hc"] = hourCycle;
            }
            string caseFirst = options.CaseFirst;

            if (caseFirst != null)
            {
                IntlProviderOptions.ParseEnum <CollatorCaseFirst>(caseFirst);
                builder.UExtensions["kf"] = caseFirst;
            }
            bool?numeric = options.Numeric;

            if (numeric != null)
            {
                builder.UExtensions["kn"] = numeric.Value ? "true" : "false";
            }
            string NumberingSystem = options.NumberingSystem;

            if (NumberingSystem != null)
            {
                try {
                    builder.UExtensions["nu"] = NumberingSystem;
                } catch (FormatException) {
                    throw new EcmaRangeErrorException("Incorrect locale information provided");
                }
            }
            this.localeString = builder.Canonicalize().ToString();
        }