Cardinalities
-
CARDINALITY_ONE
Matches
v = 0 and i % 100 = 1 or f % 100 = 1Example Integers:
1, 101, 201, 301, 401, 501, 601, 701, 1001, …Example Decimals:
0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 10.1, 100.1, 1000.1, … -
CARDINALITY_TWO
Matches
v = 0 and i % 100 = 2 or f % 100 = 2Example Integers:
2, 102, 202, 302, 402, 502, 602, 702, 1002, …Example Decimals:
0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 10.2, 100.2, 1000.2, … -
CARDINALITY_FEW
Matches
v = 0 and i % 100 = 3..4 or f % 100 = 3..4Example Integers:
3, 4, 103, 104, 203, 204, 303, 304, 403, 404, 503, 504, 603, 604, 703, 704, 1003, …Example Decimals:
0.3, 0.4, 1.3, 1.4, 2.3, 2.4, 3.3, 3.4, 4.3, 4.4, 5.3, 5.4, 6.3, 6.4, 7.3, 7.4, 10.3, 100.3, 1000.3, … -
CARDINALITY_OTHER
Matches
all other valuesExample Integers:
0, 5, 19, 100, 1000, 10000, 100000, 1000000, …Example Decimals:
0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 2.7, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …
Cardinality example
This localized strings file selects the localized form of “day” for a delivery estimate. Raw numbers choose the CLDR category; values formatted for display are interpolated.
Sample localized strings file
{ "Delivery.LeadTime": { "commentary": "Estimated delivery time, expressed in days.", "translation": "{{leadTime}}", "placeholders": { "leadTime": { "value": "dayCount", "translations": { "CARDINALITY_ONE": "{{formattedDayCount}} źeń", "CARDINALITY_TWO": "{{formattedDayCount}} dnja", "CARDINALITY_FEW": "{{formattedDayCount}} dny", "CARDINALITY_OTHER": "{{formattedDayCount}} dnjow" } } } } }
Usage and expected results
JavaScript
The JavaScript code passes each raw number for cardinality selection and formats its display value separately with Intl.NumberFormat.
import { cardinalityForNumber, createStrings } from "lokalized"; import { readStringsFromDirectory } from "lokalized/node"; const locale = "dsb"; const strings = createStrings({ localizedStringSupplier: () => readStringsFromDirectory("strings").catalogs, fallbackLocale: locale, localeSupplier: () => locale, }); const numberFormat = new Intl.NumberFormat(locale); // Raw numbers select language forms; Intl.NumberFormat produces the display values // CARDINALITY_ONE const oneDayCount = 1; const oneFormattedDayCount = numberFormat.format(oneDayCount); cardinalityForNumber(oneDayCount, locale).name; // => "CARDINALITY_ONE" strings.get("Delivery.LeadTime", { dayCount: oneDayCount, formattedDayCount: oneFormattedDayCount, }); // => "1 źeń" // CARDINALITY_TWO const twoDayCount = 2; const twoFormattedDayCount = numberFormat.format(twoDayCount); cardinalityForNumber(twoDayCount, locale).name; // => "CARDINALITY_TWO" strings.get("Delivery.LeadTime", { dayCount: twoDayCount, formattedDayCount: twoFormattedDayCount, }); // => "2 dnja" // CARDINALITY_FEW const fewDayCount = 3; const fewFormattedDayCount = numberFormat.format(fewDayCount); cardinalityForNumber(fewDayCount, locale).name; // => "CARDINALITY_FEW" strings.get("Delivery.LeadTime", { dayCount: fewDayCount, formattedDayCount: fewFormattedDayCount, }); // => "3 dny" // CARDINALITY_OTHER const otherDayCount = 0; const otherFormattedDayCount = numberFormat.format(otherDayCount); cardinalityForNumber(otherDayCount, locale).name; // => "CARDINALITY_OTHER" strings.get("Delivery.LeadTime", { dayCount: otherDayCount, formattedDayCount: otherFormattedDayCount, }); // => "0 dnjow"
Java
The Java code passes each raw number for cardinality selection and formats its display value separately with NumberFormat.
Locale locale = Locale.forLanguageTag("dsb"); Strings strings = Strings.withFallbackLocale(locale) .localizedStringSupplier(() -> LocalizedStringLoader.loadFromFilesystem(Paths.get("strings"))) .localeSupplier((matcher) -> matcher.bestMatchFor(locale)) .build(); NumberFormat numberFormat = NumberFormat.getNumberInstance(locale); // Raw numbers select language forms; NumberFormat produces the display values // CARDINALITY_ONE Number oneDayCount = 1; String oneFormattedDayCount = numberFormat.format(oneDayCount); assertEquals(Cardinality.ONE, Cardinality.forNumber(oneDayCount, locale)); // Expected: 1 źeń assertEquals(oneFormattedDayCount + " źeń", strings.get( "Delivery.LeadTime", Map.of("dayCount", oneDayCount, "formattedDayCount", oneFormattedDayCount) )); // CARDINALITY_TWO Number twoDayCount = 2; String twoFormattedDayCount = numberFormat.format(twoDayCount); assertEquals(Cardinality.TWO, Cardinality.forNumber(twoDayCount, locale)); // Expected: 2 dnja assertEquals(twoFormattedDayCount + " dnja", strings.get( "Delivery.LeadTime", Map.of("dayCount", twoDayCount, "formattedDayCount", twoFormattedDayCount) )); // CARDINALITY_FEW Number fewDayCount = 3; String fewFormattedDayCount = numberFormat.format(fewDayCount); assertEquals(Cardinality.FEW, Cardinality.forNumber(fewDayCount, locale)); // Expected: 3 dny assertEquals(fewFormattedDayCount + " dny", strings.get( "Delivery.LeadTime", Map.of("dayCount", fewDayCount, "formattedDayCount", fewFormattedDayCount) )); // CARDINALITY_OTHER Number otherDayCount = 0; String otherFormattedDayCount = numberFormat.format(otherDayCount); assertEquals(Cardinality.OTHER, Cardinality.forNumber(otherDayCount, locale)); // Expected: 0 dnjow assertEquals(otherFormattedDayCount + " dnjow", strings.get( "Delivery.LeadTime", Map.of("dayCount", otherDayCount, "formattedDayCount", otherFormattedDayCount) ));
Swift
Swift uses pinned CLDR rules for raw numeric values. The display strings in these assertions are fixed examples; applications format display values separately with Foundation. Default .rtlLocales bidi isolation adds FSI/PDI to interpolated values in right-to-left output.
import Foundation import Lokalized let locale = try LocaleTag("dsb") let files = try LocalizedStringLoader.loadFromDirectory(URL(fileURLWithPath: "strings")) let catalogs = files.mapValues { LocalizedCatalog(strings: $0.strings) } let strings = try DefaultStrings(configuration: StringsConfiguration( localizedStringSupplier: { catalogs }, localeSupplier: { _ in locale }, fallbackLocale: locale )) // CARDINALITY_ONE let number1 = try NumericValue.forDecimal("1") let category1 = try Cardinality.forNumber(number1, locale: locale.tag) assert(category1 == .one) // This fixed display value makes the translation assertion reproducible. let display1 = "1" let message1 = try strings.get("Delivery.LeadTime", placeholders: [ "dayCount": .number(number1), "formattedDayCount": .text(display1), ]) assert(ExactString(message1) == ExactString("1 źeń")) // CARDINALITY_TWO let number2 = try NumericValue.forDecimal("2") let category2 = try Cardinality.forNumber(number2, locale: locale.tag) assert(category2 == .two) // This fixed display value makes the translation assertion reproducible. let display2 = "2" let message2 = try strings.get("Delivery.LeadTime", placeholders: [ "dayCount": .number(number2), "formattedDayCount": .text(display2), ]) assert(ExactString(message2) == ExactString("2 dnja")) // CARDINALITY_FEW let number3 = try NumericValue.forDecimal("3") let category3 = try Cardinality.forNumber(number3, locale: locale.tag) assert(category3 == .few) // This fixed display value makes the translation assertion reproducible. let display3 = "3" let message3 = try strings.get("Delivery.LeadTime", placeholders: [ "dayCount": .number(number3), "formattedDayCount": .text(display3), ]) assert(ExactString(message3) == ExactString("3 dny")) // CARDINALITY_OTHER let number4 = try NumericValue.forDecimal("0") let category4 = try Cardinality.forNumber(number4, locale: locale.tag) assert(category4 == .other) // This fixed display value makes the translation assertion reproducible. let display4 = "0" let message4 = try strings.get("Delivery.LeadTime", placeholders: [ "dayCount": .number(number4), "formattedDayCount": .text(display4), ]) assert(ExactString(message4) == ExactString("0 dnjow"))
Cardinality Ranges
No explicit CLDR cardinality-range mapping is defined for this language, so every range falls back to the ending cardinality.
Cardinality range example
Because CLDR defines no explicit range mapping for this locale, the ending cardinality selects the result. The selected form wraps a separately formatted display range.
Sample localized strings file
{ "Delivery.Window": { "commentary": "Estimated delivery window, expressed as a range of days.", "translation": "{{window}}", "placeholders": { "window": { "range": { "start": "minDays", "end": "maxDays" }, "translations": { "CARDINALITY_ONE": "{{formattedDayRange}} źeń", "CARDINALITY_TWO": "{{formattedDayRange}} dnja", "CARDINALITY_FEW": "{{formattedDayRange}} dny", "CARDINALITY_OTHER": "{{formattedDayRange}} dnjow" } } } } }
Usage and expected results
JavaScript
The JavaScript code passes raw range endpoints for selection, formats both endpoints separately with Intl.NumberFormat, and joins them for display; choose range punctuation appropriate to your application. Range rules are an optional import, lokalized/data/ranges.
import { cardinalityForNumber, createStrings, decimal } from "lokalized"; import { cardinalRangeData, cardinalityForRange } from "lokalized/data/ranges"; import { readStringsFromDirectory } from "lokalized/node"; const locale = "dsb"; const strings = createStrings({ localizedStringSupplier: () => readStringsFromDirectory("strings").catalogs, fallbackLocale: locale, localeSupplier: () => locale, pluralData: { ranges: cardinalRangeData }, }); const numberFormat = new Intl.NumberFormat(locale); // Raw numbers select language forms; Intl.NumberFormat produces the display values // CARDINALITY_ONE range (ending-cardinality fallback) const minDays = 0; const formattedMinDays = numberFormat.format(minDays); const maxDays = decimal("0.1"); const formattedMaxDays = new Intl.NumberFormat(locale, { minimumFractionDigits: 1, maximumFractionDigits: 1 }) .format("0.1"); const formattedDayRange = formattedMinDays + "-" + formattedMaxDays; cardinalityForRange( cardinalityForNumber(minDays, locale), cardinalityForNumber(maxDays, locale), locale ).name; // => "CARDINALITY_ONE" strings.get("Delivery.Window", { minDays, maxDays, formattedDayRange }); // => "0-0,1 źeń"
Java
The Java code passes raw range endpoints for selection. Because NumberFormat formats one number at a time, it formats both endpoints separately and joins them for display; choose range punctuation appropriate to your application.
Locale locale = Locale.forLanguageTag("dsb"); Strings strings = Strings.withFallbackLocale(locale) .localizedStringSupplier(() -> LocalizedStringLoader.loadFromFilesystem(Paths.get("strings"))) .localeSupplier((matcher) -> matcher.bestMatchFor(locale)) .build(); NumberFormat numberFormat = NumberFormat.getNumberInstance(locale); // Raw numbers select language forms; NumberFormat produces the display values // CARDINALITY_ONE range (ending-cardinality fallback) Number minDays = 0; String formattedMinDays = numberFormat.format(minDays); Number maxDays = new java.math.BigDecimal("0.1"); NumberFormat maxDaysFormat = NumberFormat.getNumberInstance(locale); maxDaysFormat.setMinimumFractionDigits(1); maxDaysFormat.setMaximumFractionDigits(1); String formattedMaxDays = maxDaysFormat.format(maxDays); String formattedDayRange = formattedMinDays + "-" + formattedMaxDays; assertEquals(Cardinality.ONE, Cardinality.forRange( Cardinality.forNumber(minDays, locale), Cardinality.forNumber(maxDays, locale), locale )); // Expected: 0-0,1 źeń assertEquals(formattedDayRange + " źeń", strings.get( "Delivery.Window", Map.of("minDays", minDays, "maxDays", maxDays, "formattedDayRange", formattedDayRange) ));
Swift
Swift uses pinned CLDR rules for raw numeric values. The display strings in these assertions are fixed examples; applications format display values separately with Foundation. Default .rtlLocales bidi isolation adds FSI/PDI to interpolated values in right-to-left output.
import Foundation import Lokalized let locale = try LocaleTag("dsb") let files = try LocalizedStringLoader.loadFromDirectory(URL(fileURLWithPath: "strings")) let catalogs = files.mapValues { LocalizedCatalog(strings: $0.strings) } let strings = try DefaultStrings(configuration: StringsConfiguration( localizedStringSupplier: { catalogs }, localeSupplier: { _ in locale }, fallbackLocale: locale )) let start = try NumericValue.forDecimal("0") let end = try NumericValue.forDecimal("0.1") let startForm = try Cardinality.forNumber(start, locale: locale.tag) let endForm = try Cardinality.forNumber(end, locale: locale.tag) let rangeForm = try Cardinality.forRange(startForm, endForm, locale: locale.tag) assert(rangeForm == .one) let message1 = try strings.get("Delivery.Window", placeholders: [ "minDays": .number(start), "maxDays": .number(end), "formattedDayRange": .text("0-0,1"), ]) assert(ExactString(message1) == ExactString("0-0,1 źeń"))
Ordinalities
All ordinal numbers use ORDINALITY_OTHER; no value selects a different form.
Example: Na 2. wotbocenju napšawo..
Addendum: Language Form Rules
The language form expressions above are specified by Unicode Technical Standard #35 and use the following notation:
nabsolute value of the source number (integer and decimals)iinteger digits ofnvnumber of visible fraction digits inn, with trailing zeroswnumber of visible fraction digits inn, without trailing zerosfvisible fractional digits inn, with trailing zerostvisible fractional digits inn, without trailing zerosc/ecompact decimal exponent operands used by some CLDR compact-number rules