localizePlural function
String
localizePlural(
- Object? modifier,
- String key,
- ITranslations translations, {
- String? locale,
Returns the translated version for the plural modifier. After getting the version, substring %d will be replaced with the modifier.
Note: This will try to get the most specific plural modifier. For example,
.two
is more specific than .many
.
If no applicable modifier can be found, it will default to the unversioned
string. For example, this: "a".zero("b").four("c:")
will default to "a"
for 1, 2, 3, or more than 5 elements.
The modifier should usually be an integer. But in case it's not, it will be converted into an integer. The rules are:
-
If the modifier is an int, its absolute value will be used. Note: absolute value means a negative value will become positive.
-
If the modifier is a double, its absolute value will be used, like so:
- 1.0 will be 1.
- Values below 1.0 will become 0.
- Values larger than 1.0 will be rounded up.
- Strings will be converted to integer or if that fails to a double. Conversion is done like so:
- First, it will discard other chars than numbers, dot and the minus sign, by converting them to spaces.
- Then it will convert to int using
int.tryParse
. - Then it will convert to double using
double.tryParse
. - If all fails, it will be zero.
- Other objects will be converted to a string (using the toString method), and then the above rules will apply.
Implementation
String localizePlural(
Object? modifier,
String key,
ITranslations translations, {
String? locale,
}) {
int modifierInt = convertToIntegerModifier(modifier);
locale = locale?.toLowerCase();
Map<String?, String> versions = localizeAllVersions(key, translations, locale: locale);
String? text;
/// For plural(0), returns the version 0, otherwise the version the version
/// 0-1, otherwise the version many, otherwise the unversioned.
if (modifierInt == 0)
text = versions["0"] ?? versions["F"] ?? versions["M"] ?? versions[null];
/// For plural(1), returns the version 1, otherwise the version the version
/// 0-1, otherwise the version the version 1-many, otherwise the unversioned.
else if (modifierInt == 1)
text = versions["1"] ?? versions["F"] ?? versions["R"] ?? versions[null];
/// For plural(2), returns the version 2, otherwise the version 2-3-4,
/// otherwise the version many/1-many, otherwise the unversioned.
else if (modifierInt == 2)
text = versions["2"] ?? versions["C"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(3), returns the version 3, otherwise the version 2-3-4,
/// otherwise the version many/1-many, otherwise the unversioned.
else if (modifierInt == 3)
text = versions["3"] ?? versions["C"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(4), returns the version 4, otherwise the version 2-3-4,
/// otherwise the version many/1-many, otherwise the unversioned.
else if (modifierInt == 4)
text = versions["4"] ?? versions["C"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(5), returns the version 5, otherwise the version many/1-many,
/// otherwise the unversioned.
else if (modifierInt == 5)
text = versions["5"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(6), returns the version 6, otherwise the version many/1-many,
/// otherwise the unversioned.
else if (modifierInt == 6)
text = versions["6"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(10), returns the version 10, otherwise the version many/1-many,
/// For plural(10), returns the version 10, otherwise the version many/1-many,
/// otherwise the unversioned.
else if (modifierInt == 10)
text = versions["T"] ?? versions["M"] ?? versions["R"] ?? versions[null];
/// For plural(<0 or >2), returns the version many/1-many,
/// otherwise the unversioned.
else
text = versions[modifierInt.toString()] ?? versions["M"] ?? versions["R"] ?? versions[null];
// ---
if (text == null)
throw TranslationsException("No version found "
"(modifier: $modifierInt, "
"key: '$key', "
"locale: '${_effectiveLocale(locale)}').");
text = text.replaceAll("%d", modifierInt.toString());
return text;
}