private static void AddRules(String[] languages, PluralRules rules) { foreach (var language in languages) { _allRules[language] = rules; } }
public static EcmaValue ResolvedOptions([This] EcmaValue thisValue) { PluralRules rules = thisValue.GetUnderlyingObject <PluralRules>(); List <EcmaValue> categories = new List <EcmaValue>(); foreach (KeyValuePair <string, PluralCategories> e in StringValueMap <PluralCategories> .Default.Forward) { if ((rules.PluralCategories & e.Value) != 0) { categories.Add(e.Key); } } EcmaObject obj = new EcmaObject(); obj.CreateDataPropertyOrThrow(PropertyKey.Locale, rules.Locale); obj.CreateDataPropertyOrThrow(PropertyKey.PluralCategories, new EcmaArray(categories)); obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(rules.Type)); if (rules.Digits.UseSignificantDigits) { obj.CreateDataPropertyOrThrow(PropertyKey.MinimumSignificantDigits, rules.Digits.MinimumSignificantDigits); obj.CreateDataPropertyOrThrow(PropertyKey.MaximumSignificantDigits, rules.Digits.MaximumSignificantDigits); } else { obj.CreateDataPropertyOrThrow(PropertyKey.MinimumIntegerDigits, rules.Digits.MinimumSignificantDigits); obj.CreateDataPropertyOrThrow(PropertyKey.MinimumFractionDigits, rules.Digits.MinimumFractionDigits); obj.CreateDataPropertyOrThrow(PropertyKey.MaximumFractionDigits, rules.Digits.MaximumFractionDigits); } return(obj); }
protected CultureRules( [NotNull] Func <PluralRules, PluralRules> plurals, [NotNull] Func <SingularRules, SingularRules> singulars, [NotNull] Func <IrregularRules, IrregularRules> irregulars, [NotNull] Func <UncountableRules, UncountableRules> uncountables, [NotNull] Func <int, string> ordanizeFunc) { if (plurals == null) { throw new ArgumentNullException(nameof(plurals)); } if (singulars == null) { throw new ArgumentNullException(nameof(singulars)); } if (uncountables == null) { throw new ArgumentNullException(nameof(uncountables)); } if (ordanizeFunc == null) { throw new ArgumentNullException(nameof(ordanizeFunc)); } RuleSet = new InflectorRuleSet(new List <InflectorRule>(), new List <InflectorRule>(), new List <string>(), ordanizeFunc); Plurals = new PluralRules(RuleSet); Singulars = new SingularRules(RuleSet); Uncountables = new UncountableRules(RuleSet); Irregulars = new IrregularRules(Singulars, Plurals); plurals(Plurals); singulars(Singulars); uncountables(Uncountables); irregulars(Irregulars); }
private PluralRules.PluralRuleDelegate?GetPluralRule(IFormattingInfo formattingInfo) { // See if the language was explicitly passed: var pluralOptions = formattingInfo.FormatterOptions; if (pluralOptions?.Length != 0) { return(PluralRules.GetPluralRule(pluralOptions)); } // See if a CustomPluralRuleProvider is available from the FormatProvider: var provider = formattingInfo.FormatDetails.Provider; var pluralRuleProvider = (CustomPluralRuleProvider?)provider?.GetFormat(typeof(CustomPluralRuleProvider)); if (pluralRuleProvider != null) { return(pluralRuleProvider.GetPluralRule()); } // Use the CultureInfo, if provided: if (provider is CultureInfo cultureInfo) { var culturePluralRule = PluralRules.GetPluralRule(cultureInfo.TwoLetterISOLanguageName); return(culturePluralRule); } // Use the default, if provided: return(PluralRules.GetPluralRule(DefaultTwoLetterISOLanguageName));; }
public override string ParseArguments(PluralRules pluralRules, string resourceName, out FormatString.Argument [] arguments) { var stack = new Stack <FormatString.Argument> ( ); while (true) { if (TryExtractPluralForm(ref resourceName, out var pluralForm, out var numberForm)) { stack.Push(new FormatString.Argument(null, numberForm, pluralForm, null)); continue; } if (stack.Count > 0 && TryExtractPluralRangeForm(ref resourceName)) { var last = stack.Peek( ); last.PluralRangeForm = last.AvailablePluralForms; last.AvailablePluralForms = pluralRules.CardinalForm.PluralForms; stack.Push(new FormatString.Argument(null, last.NumberForm, last.AvailablePluralForms, last.PluralRangeForm)); continue; } break; } arguments = stack.ToArray( ); return(resourceName); }
public void Add_Invalid_Custom_Language_TimeTextInfo_Should_Throw() { var language = "123xyz"; Assert.That(() => { TimeTextInfo custom = new() { PluralRule = PluralRules.GetPluralRule(language), Ptxt_week = new[] { "{0} week", "{0} weeks" }, Ptxt_day = new[] { "{0} day", "{0} days" }, Ptxt_hour = new[] { "{0} hour", "{0} hours" }, Ptxt_minute = new[] { "{0} minute", "{0} minutes" }, Ptxt_second = new[] { "{0} second", "{0} seconds" }, Ptxt_millisecond = new[] { "{0} millisecond", "{0} milliseconds" }, Ptxt_w = new[] { "{0}w" }, Ptxt_d = new[] { "{0}d" }, Ptxt_h = new[] { "{0}h" }, Ptxt_m = new[] { "{0}m" }, Ptxt_s = new[] { "{0}s" }, Ptxt_ms = new[] { "{0}ms" }, Ptxt_lessThan = "less than {0}" }; CommonLanguagesTimeTextInfo.AddLanguage(language, custom); }, Throws.ArgumentException.And.Message.Contains("IsoLangToDelegate not found")); } }
public static void SetRules(string code) { if (_allRules.TryGetValue(code, out PluralRules rules)) { _currentRules = rules; } }
public static void GetPluralRulesReturnsWithTheCorrectCulture( ) { foreach (var culture in AllCultures) { Assert.Equal(culture, PluralRules.GetPluralRules(culture).Culture); } }
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); }
/* * Initializes the <code>PluralRules</code> object. * Postcondition:<br/> * <code>ulocale</code> : is <code>locale</code><br/> * <code>pluralRules</code>: if <code>rules</code> != <code>null</code> * it's set to rules, otherwise it is the * predefined plural rule set for the locale * <code>ulocale</code>.<br/> * <code>parsedValues</code>: is <code>null</code><br/> * <code>pattern</code>: is <code>null</code><br/> * <code>numberFormat</code>: a <code>NumberFormat</code> for the locale * <code>ulocale</code>. */ private void Init(PluralRules rules, PluralType type, UCultureInfo locale, NumberFormat numberFormat) { ulocale = locale; pluralRules = (rules == null) ? PluralRules.ForLocale(ulocale, type) // ICU4N TODO: Make extension method for UCultureInfo.GetPluralRules(PluralType)..? : rules; pluralRulesWrapper = new PluralSelectorAdapter(pluralRules); // ICU4N: Have to pass a reference to pluralRules in the constructor ResetPattern(); this.numberFormat = (numberFormat == null) ? NumberFormat.GetInstance(ulocale) : numberFormat; }
public static string Declension(string key, int count) { if (_currentRules == null) { _currentRules = _allRules["en"]; } return(string.Format(LocaleService.Current.GetString(key, _currentRules.QuantityForNumber(count)), count.ToString("N0"))); }
/* * Initializes the <code>PluralRules</code> object. * Postcondition:<br/> * <code>ulocale</code> : is <code>locale</code><br/> * <code>pluralRules</code>: if <code>rules</code> != <code>null</code> * it's set to rules, otherwise it is the * predefined plural rule set for the locale * <code>ulocale</code>.<br/> * <code>parsedValues</code>: is <code>null</code><br/> * <code>pattern</code>: is <code>null</code><br/> * <code>numberFormat</code>: a <code>NumberFormat</code> for the locale * <code>ulocale</code>. */ private void Init(PluralRules rules, PluralType type, ULocale locale, NumberFormat numberFormat) { ulocale = locale; pluralRules = (rules == null) ? PluralRules.ForLocale(ulocale, type) : rules; pluralRulesWrapper = new PluralSelectorAdapter(pluralRules); // ICU4N: Have to pass a reference to pluralRules in the constructor ResetPattern(); this.numberFormat = (numberFormat == null) ? NumberFormat.GetInstance(ulocale) : numberFormat; }
public static string Declension(string key, int count) { if (_currentRules == null) { _currentRules = _allRules["en"]; } return(string.Format(_loader.GetString(key + StringForQuantity(_currentRules.QuantityForNumber(count))), count.ToString("N0"))); }
/// <summary> /// Gets the rule from the rulesId. If there is no rule for this rulesId, /// return null. /// </summary> public virtual PluralRules GetRulesForRulesId(string rulesId) { // synchronize on the map. release the lock temporarily while we build the rules. PluralRules rules = null; bool hasRules; // Separate boolean because stored rules can be null. lock (rulesIdToRulesLock) { hasRules = rulesIdToRules.ContainsKey(rulesId); if (hasRules) { rulesIdToRules.TryGetValue(rulesId, out rules); // can be null } } if (!hasRules) { try { UResourceBundle pluralb = GetPluralBundle(); UResourceBundle rulesb = pluralb.Get("rules"); UResourceBundle setb = rulesb.Get(rulesId); StringBuilder sb = new StringBuilder(); for (int i = 0; i < setb.Length; ++i) { UResourceBundle b = setb.Get(i); if (i > 0) { sb.Append("; "); } sb.Append(b.Key); sb.Append(": "); sb.Append(b.GetString()); } rules = PluralRules.ParseDescription(sb.ToString()); } catch (FormatException) { } catch (MissingManifestResourceException) { } lock (rulesIdToRulesLock) { if (rulesIdToRules.ContainsKey(rulesId)) { rulesIdToRules.TryGetValue(rulesId, out rules); } else { rulesIdToRules[rulesId] = rules; // can be null } } } return(rules); }
private void AssertTranslations(string orig, LanguageGender gender, Func <int, string> messageGetter) { ForEachCatalog(lang => { for (int i = 0; i < 10000; i++) { var order = PluralRules.GetOrder(Catalog.Implementation.CurrentIsoLanguageCode, i); Assert.AreEqual(lang + ",P" + order + "|" + orig + GenderTag(gender), messageGetter(i)); } }); }
public void TestNested2() { ForEachCatalog(lang => { for (int i = 0; i < 10000; i++) { Assert.AreEqual(String.Format("a$_B$_C$_D:P{0}$_Ef$_Ghi", PluralRules.GetOrder(lang, i)), Strings.Nested2(i)); } }); }
public void Get_All_Plural_Rules() { foreach (var deleg in PluralRules.IsoLangToDelegate) { var rule = PluralRules.GetPluralRule(deleg.Key); for (var i = 0; i <= 100; i++) { // not testing plural rules logic Assert.DoesNotThrow(() => rule(i, 1)); } } }
public void Test_With_CustomPluralRuleProvider() { var actualResult = Smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("de")), "{0:plural:Frau|Frauen}", new string[2], "more"); Assert.AreEqual("Frauen", actualResult); actualResult = Smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("en")), "{0:plural:person|people}", new string[2], "more"); Assert.AreEqual("people", actualResult); actualResult = Smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("en")), "{0:plural:person|people}", new string[1], "one"); Assert.AreEqual("person", actualResult); }
protected override PluralResourceSet LoadResourceSet(PluralRules pluralRules) { var resources = resourceManager.GetResourceSet(pluralRules.Culture, true, true); var pluralResources = new Dictionary <string, List <FormatString> > ( ); var resource = resources.GetEnumerator( ); while (resource.MoveNext( )) { if (!(resource.Value is string)) { continue; } var name = (string)resource.Key; var baseName = namingStrategy.ParseArguments(pluralRules, name, out var arguments); if (baseName != name) { if (!pluralResources.TryGetValue(baseName, out var pluralFormats)) { pluralResources.Add(baseName, pluralFormats = new List <FormatString> ( )); } var pluralFormat = FormatString.Parse(resource.Value?.ToString( )); pluralFormat.Arguments = arguments; pluralFormats.Add(pluralFormat); } } var resourceSet = new PluralResourceSet(pluralRules, pluralResources.Count); foreach (var pluralResource in pluralResources) { var name = pluralResource.Key; var value = resources.GetString(name); if (value == null) { continue; } var defaultFormat = FormatString.Parse(value); var pluralFormats = pluralResource.Value.ToArray( ); resourceSet.Add(name, new PluralFormatString(defaultFormat, pluralFormats)); } return(resourceSet); }
public PluralResourceSet GetResourceSet(CultureInfo culture) { if (culture == null) { culture = CultureInfo.CurrentUICulture; } if (cache.TryGet(culture.Name, out var resourceSet)) { return(resourceSet); } cache.Add(culture.Name, resourceSet = LoadResourceSet(PluralRules.GetPluralRules(culture))); return(resourceSet); }
public FormatString ApplyPluralRules(PluralRules pluralRules, params object [] args) { var pluralForms = pluralRules.SelectPluralForms(defaultFormat, args); ApplyPluralRangeRules(pluralRules, defaultFormat.Arguments, pluralForms); for (var index = 0; index < pluralFormats.Length; index++) { var candidateFormat = pluralFormats [index]; if (Matches(candidateFormat, pluralForms)) { return(candidateFormat); } } return(defaultFormat); }
/// <summary> /// Returns the plural rules for the the locale. If we don't have data, /// <see cref="PluralRules.Default"/> is returned. /// </summary> #pragma warning disable 672 public override PluralRules ForLocale(ULocale locale, PluralType type) #pragma warning restore 672 { string rulesId = GetRulesIdForLocale(locale, type); if (rulesId == null || rulesId.Trim().Length == 0) { return(PluralRules.Default); } PluralRules rules = GetRulesForRulesId(rulesId); if (rules == null) { rules = PluralRules.Default; } return(rules); }
private static void ApplyPluralRangeRules(PluralRules pluralRules, FormatString.Argument [] arguments, PluralForm [] pluralForms) { for (var index = 0; index < arguments.Length; index++) { var argument = arguments [index]; if (argument.PluralRangeForm == null) { continue; } var start = pluralForms [index]; var end = pluralForms [index + 1]; var rangeForm = pluralRules.PluralFormRange.SelectPluralForm(start, end); pluralForms [index++] = PluralForm.Other; pluralForms [index] = rangeForm; } }
public void Test_With_CustomPluralRuleProvider() { var smart = GetFormatter(); var actualResult = smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("de")), "{0:plural:Frau|Frauen}", new string[2], "more"); Assert.AreEqual("Frauen", actualResult); actualResult = smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("en")), "{0:plural:person|people}", new string[2], "more"); Assert.AreEqual("people", actualResult); actualResult = smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("en")), "{0:plural:person|people}", new string[1], "one"); Assert.AreEqual("person", actualResult); actualResult = smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("fr")), "{0:plural:une personne|deux personnes|plusieurs personnes}", new string[3], "several"); Assert.AreEqual("plusieurs personnes", actualResult); actualResult = smart.Format(new CustomPluralRuleProvider(PluralRules.GetPluralRule("fr")), "{0:plural:une personne|deux personnes|plusieurs personnes|beaucoup de personnes}", new string[3], "several"); Assert.AreEqual("beaucoup de personnes", actualResult); }
protected CultureRules( [NotNull] Func<PluralRules, PluralRules> plurals, [NotNull] Func<SingularRules, SingularRules> singulars, [NotNull] Func<IrregularRules, IrregularRules> irregulars, [NotNull] Func<UncountableRules, UncountableRules> uncountables, [NotNull] Func<int, string> ordanizeFunc) { if (plurals == null) throw new ArgumentNullException(nameof(plurals)); if (singulars == null) throw new ArgumentNullException(nameof(singulars)); if (uncountables == null) throw new ArgumentNullException(nameof(uncountables)); if (ordanizeFunc == null) throw new ArgumentNullException(nameof(ordanizeFunc)); RuleSet = new InflectorRuleSet(new List<InflectorRule>(), new List<InflectorRule>(), new List<string>(), ordanizeFunc); Plurals = new PluralRules(RuleSet); Singulars = new SingularRules(RuleSet); Uncountables = new UncountableRules(RuleSet); Irregulars = new IrregularRules(Singulars, Plurals); plurals(Plurals); singulars(Singulars); uncountables(Uncountables); irregulars(Irregulars); }
public void Add_Valid_Custom_Language_TimeTextInfo() { var language = "nl"; // dummy - it's English, not Dutch ;-) TimeTextInfo custom = new() { PluralRule = PluralRules.GetPluralRule(language), Ptxt_week = new[] { "{0} week", "{0} weeks" }, Ptxt_day = new[] { "{0} day", "{0} days" }, Ptxt_hour = new[] { "{0} hour", "{0} hours" }, Ptxt_minute = new[] { "{0} minute", "{0} minutes" }, Ptxt_second = new[] { "{0} second", "{0} seconds" }, Ptxt_millisecond = new[] { "{0} millisecond", "{0} milliseconds" }, Ptxt_w = new[] { "{0}w" }, Ptxt_d = new[] { "{0}d" }, Ptxt_h = new[] { "{0}h" }, Ptxt_m = new[] { "{0}m" }, Ptxt_s = new[] { "{0}s" }, Ptxt_ms = new[] { "{0}ms" }, Ptxt_lessThan = "less than {0}" }; Assert.That(() => CommonLanguagesTimeTextInfo.AddLanguage(language, custom), Throws.Nothing); Assert.That(() => CommonLanguagesTimeTextInfo.GetTimeTextInfo(language), Is.EqualTo(custom)); }
public abstract string ParseArguments(PluralRules pluralRules, string resourceName, out FormatString.Argument [] arguments);
public void Illegal_Iso_Language() { Assert.Throws <System.ArgumentException>(() => PluralRules.GetPluralRule("-for-sure-illegal-code")); }
public PluralResourceSet(PluralRules rules, int capacity) { pluralRules = rules; resources = new Cache <string, PluralFormatString> (capacity); }
/// <summary> /// Создает экземпляр PluralFormatProvider с заданным языком. /// </summary> /// <param name="cultureInfo">Нужный языковой стандарт</param> public PluralFormatProvider(CultureInfo cultureInfo) { _rules = new PluralRules(cultureInfo); }
protected abstract PluralResourceSet LoadResourceSet(PluralRules pluralRules);
/// <summary> /// Adds the plural. /// </summary> /// <param name="rule">The rule.</param> /// <param name="replacement">The replacement.</param> protected void AddPlural(string rule, string replacement) { PluralRules.Add(new Rule(rule, replacement)); }