IPluralRules
IPluralRules is a record that contains rule providers.
/// <summary>Table containing providers for plural rules. May contain one or multiple rulesets.</summary>
/// <example>
/// category=cardinal
/// case="zero": "no cars"
/// case="one": "a car"
/// case="other": "{0} cars"
///
/// category=ordinal
/// case="one": "Take first exit out."
/// case="two": "Take second exit out."
/// case="few": "Take third exit out."
/// case="other": "Take {0}th exit out."
/// </example>
public interface IPluralRules
{
/// <summary>All rules</summary>
IPluralRule[] AllRules { get; set; }
/// <summary>Queries rules</summary>
IProvider<PluralRuleInfo, IPluralRule[]> Rules { get; set; }
/// <summary>Queries and caches rules</summary>
IProvider<PluralRuleInfo, IPluralRule[]> RulesCached { get; set; }
/// <summary>Creates rule evaluator</summary>
IProvider<PluralRuleInfo, IPluralRulesEvaluator> Evaluator { get; set; }
/// <summary>Creates and caches rule evaluator</summary>
IProvider<PluralRuleInfo, IPluralRulesEvaluator> EvaluatorCached { get; set; }
}
IPluralRulesEvaluator evaluates strings against associated rules.
/// <summary>Evaluates numbers against rules.</summary>
public interface IPluralRulesEvaluator
{
/// <summary>
/// Evaluates <paramref name="number"/> against sub-set of rules.
///
/// First array elements are optional rules, and the last array element is mandatory rule.
/// </summary>
/// <param name="number">(optional) numeric and text representation of numberic value</param>
/// <returns>Matching cases, first ones are optional, the last one is always mandatory (and only mandatory). Or null if evaluate failed.</returns>
IPluralRule[]? Evaluate<N>(N number) where N : IPluralNumber;
}
.Query(ruleset, category, culture, case, required) queries for a sub-set of rules.
// Read plurals.xml and ordinals.xml (from cldr-common-40.0.zip)
IPluralRule[] rulearray =
CLDRs.PluralReader("plurals.xml", "Unicode.CLDR40")
.Concat(CLDRs.PluralReader("ordinals.xml", "Unicode.CLDR40"))
.ToArray();
// Query "fi" rules
IPluralRule[] fi_rules = rulearray.Query(
ruleset: null,
category: null,
culture: "fi",
@case: null,
required: null
);
// Print rules
foreach (IPluralRule rule in fi_rules) WriteLine(rule);
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=fi,Case=one,Required=True] i=1 and v=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=fi,Case=other,Required=True]
// [RuleSet=Unicode.CLDR40,Category=ordinal,Culture=fi,Case=other,Required=True]
PluralRules(enumerable) adapts rule enumerable into rule providers.
IPluralRules rules = new PluralRules(rulearray);
.RulesCached[query] queries and caches result. The constant PluralRuleInfo.NEWEST returns rules from the latest rule-set. CLDRs.PluralRules contains all builtin rules.
// Get intrinsic rules
IPluralRules rules = CLDRs.All;
// Get german rules
IPluralRule[] de_rules = rules.RulesCached[(PluralRuleInfo.NEWEST, "cardinal", "de", null, null)];
// Print rules
foreach (IPluralRule rule in de_rules) WriteLine(rule);
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=zero,Required=False] n=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=one,Required=True] i=1 and v=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=other,Required=True] true
.EvaluatorCached[query] queries for evaluator and caches result.
// Get german cardinal rule evaluator
IPluralRulesEvaluator de_evaluator = rules.EvaluatorCached[(PluralRuleInfo.NEWEST, "cardinal", "de", null, null)];
Evaluator compares string against plurality rules. Result is an array of matching rules. Highest priority is first, lowest last.
// Text representation of the number
string text = "100.0";
// Create number feature extraction
TextNumber textNumber = new TextNumber(text);
// Evaluate rule
IPluralRule[]? matching_rules = de_evaluator.Evaluate<TextNumber>(textNumber);
// Get first matching case
WriteLine(matching_rules![0].Info.Case); // "other"
Full Example
Full example
using Avalanche.Localization;
using Avalanche.Localization.Pluralization;
using static System.Console;
class pluralrules
{
public static void Run()
{
{
// <01>
// Read plurals.xml and ordinals.xml (from cldr-common-40.0.zip)
IPluralRule[] rulearray =
CLDRs.PluralReader("plurals.xml", "Unicode.CLDR40")
.Concat(CLDRs.PluralReader("ordinals.xml", "Unicode.CLDR40"))
.ToArray();
// Query "fi" rules
IPluralRule[] fi_rules = rulearray.Query(
ruleset: null,
category: null,
culture: "fi",
@case: null,
required: null
);
// Print rules
foreach (IPluralRule rule in fi_rules) WriteLine(rule);
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=fi,Case=one,Required=True] i=1 and v=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=fi,Case=other,Required=True]
// [RuleSet=Unicode.CLDR40,Category=ordinal,Culture=fi,Case=other,Required=True]
// </01>
// <02>
IPluralRules rules = new PluralRules(rulearray);
// </02>
}
{
// <03>
// Get intrinsic rules
IPluralRules rules = CLDRs.All;
// Get german rules
IPluralRule[] de_rules = rules.RulesCached[(PluralRuleInfo.NEWEST, "cardinal", "de", null, null)];
// Print rules
foreach (IPluralRule rule in de_rules) WriteLine(rule);
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=zero,Required=False] n=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=one,Required=True] i=1 and v=0
// [RuleSet=Unicode.CLDR40,Category=cardinal,Culture=de,Case=other,Required=True] true
// </03>
// <04>
// Get german cardinal rule evaluator
IPluralRulesEvaluator de_evaluator = rules.EvaluatorCached[(PluralRuleInfo.NEWEST, "cardinal", "de", null, null)];
// </04>
// <05>
// Text representation of the number
string text = "100.0";
// Create number feature extraction
TextNumber textNumber = new TextNumber(text);
// Evaluate rule
IPluralRule[]? matching_rules = de_evaluator.Evaluate<TextNumber>(textNumber);
// Get first matching case
WriteLine(matching_rules![0].Info.Case); // "other"
// </05>
}
}
}