Skip to content

Commit def2cb1

Browse files
committed
add support for v4 plural
1 parent 138825a commit def2cb1

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/parser.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const defaults = {
6161

6262
defaultValue: '', // default value used if not passed to `parser.set`
6363

64+
pluralSuffixes: undefined, // override plural suffixes
65+
6466
// resource
6567
resource: {
6668
// The path where resources get loaded from. Relative to current working directory.
@@ -257,7 +259,7 @@ class Parser {
257259
this.resStore[lng] = this.resStore[lng] || {};
258260
this.resScan[lng] = this.resScan[lng] || {};
259261

260-
this.pluralSuffixes[lng] = i18nextInstance.services.pluralResolver.getSuffixes(lng);
262+
this.pluralSuffixes[lng] = this.options.pluralSuffixes ?? i18nextInstance.services.pluralResolver.getSuffixes(lng);
261263

262264
if (this.pluralSuffixes[lng].length === 0) {
263265
this.log(`No plural rule found for: ${lng}`);
@@ -511,6 +513,9 @@ class Parser {
511513
const supportedOptions = [
512514
'defaultValue',
513515
'defaultValue_plural',
516+
'defaultValue_one',
517+
'defaultValue_other',
518+
'defaultValue_many',
514519
'count',
515520
'context',
516521
'ns',
@@ -1052,9 +1057,13 @@ class Parser {
10521057
}
10531058

10541059
resKeys.forEach((resKey) => {
1060+
const pluralSuffix = resKey.substring(key.length);
1061+
const defaultPluralValueProp = `defaultValue${pluralSuffix}`;
1062+
const pluralKey = pluralSuffix.substring(1);
1063+
10551064
if (resLoad[resKey] === undefined) {
1056-
if (options.defaultValue_plural !== undefined && resKey.endsWith(`${pluralSeparator}plural`)) {
1057-
resLoad[resKey] = options.defaultValue_plural;
1065+
if (options[defaultPluralValueProp] !== undefined && resKey.endsWith(`${pluralSeparator}${pluralKey}`)) {
1066+
resLoad[resKey] = options[defaultPluralValueProp];
10581067
} else {
10591068
// Fallback to `defaultValue`
10601069
resLoad[resKey] = _.isFunction(defaultValue)
@@ -1065,7 +1074,7 @@ class Parser {
10651074
if (resLoad[resKey] !== undefined) {
10661075
this.log(`Added a new translation key { ${chalk.yellow(JSON.stringify(resKey))}: ${chalk.yellow(JSON.stringify(resLoad[resKey]))} } to ${chalk.yellow(JSON.stringify(this.formatResourceLoadPath(lng, ns)))}`);
10671076
}
1068-
} else if (options.defaultValue && (!options.defaultValue_plural || !resKey.endsWith(`${pluralSeparator}plural`))) {
1077+
} else if (options.defaultValue && (!options[defaultPluralValueProp] || !resKey.endsWith(`${pluralSeparator}${pluralKey}`))) {
10691078
const value = _.isFunction(defaultValue)
10701079
? defaultValue(lng, ns, key, options)
10711080
: (options.defaultValue || defaultValue); // Use `options.defaultValue` if specified
@@ -1076,12 +1085,12 @@ class Parser {
10761085
// A default value has provided but it's different with the expected default
10771086
this.log(`The translation key ${chalk.yellow(JSON.stringify(resKey))}, with a default value of "${chalk.yellow(options.defaultValue)}" has a different default value, you may need to check the translation key of default language (${defaultLng})`);
10781087
}
1079-
} else if (options.defaultValue_plural && resKey.endsWith(`${pluralSeparator}plural`)) {
1088+
} else if (options[defaultPluralValueProp] && resKey.endsWith(`${pluralSeparator}${pluralKey}`)) {
10801089
if (!resLoad[resKey]) {
1081-
resLoad[resKey] = options.defaultValue_plural;
1082-
} else if ((resLoad[resKey] !== options.defaultValue_plural) && (lng === defaultLng)) {
1090+
resLoad[resKey] = options[defaultPluralValueProp];
1091+
} else if ((resLoad[resKey] !== options[defaultPluralValueProp]) && (lng === defaultLng)) {
10831092
// A default value has provided but it's different with the expected default
1084-
this.log(`The translation key ${chalk.yellow(JSON.stringify(resKey))}, with a default value of "${chalk.yellow(options.defaultValue_plural)}" has a different default value, you may need to check the translation key of default language (${defaultLng})`);
1093+
this.log(`The translation key ${chalk.yellow(JSON.stringify(resKey))}, with a default "${pluralKey}" value of "${chalk.yellow(options[defaultPluralValueProp])}" has a different default value, you may need to check the translation key of default language (${defaultLng})`);
10851094
}
10861095
}
10871096

0 commit comments

Comments
 (0)