Skip to content

Commit 73ff7ce

Browse files
committed
chore(i18n): improved extraction of plural strings
1 parent 492f30a commit 73ff7ce

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

bin/extract-strings.mjs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ const OUTPUT_BANNER = `#
8383

8484
/** @type {import("i18next-parser").UserConfig} */
8585
const config = {
86-
// Our translation system requires that we add ".zero", ".one", ".other" keys for plurals
86+
// Our translation system requires that we add all 6 forms (zero, one, two, few, many, other) keys for plurals
8787
// i18next-parser extracts plural keys based on the target locale, so we are passing a
88-
// locale that need exactly the ".zero", ".one", ".other" keys, even if we are extracting English strings
89-
locales: ["lv"],
88+
// locale that need exactly the 6 forms, even if we are extracting English strings
89+
locales: ["ar"],
9090
keySeparator: false,
9191
namespaceSeparator: false,
9292
pluralSeparator: ".",
@@ -132,7 +132,9 @@ class SourceYmlTransform extends Transform {
132132
}
133133

134134
// Add new keys to the source YAML or log mismatched value
135-
for (const [key, value] of Object.entries(strings)) {
135+
for (let [key, value] of Object.entries(strings)) {
136+
value = this.#fixPluralValue(key, value, strings);
137+
136138
const existingPart = outputContent.parts.find(
137139
(part) => part.translation.key === key
138140
);
@@ -205,6 +207,32 @@ class SourceYmlTransform extends Transform {
205207

206208
return result;
207209
}
210+
211+
// if the key ends with .zero, .one, .two, .few, .many, .other and the value is empty
212+
// find the same key with the `.other` suffix in strings and return the value
213+
#fixPluralValue(key, value, strings) {
214+
if (key.endsWith(".zero") && value === "") {
215+
return strings[key.replace(".zero", ".other")] || "";
216+
}
217+
218+
if (key.endsWith(".one") && value === "") {
219+
return strings[key.replace(".one", ".other")] || "";
220+
}
221+
222+
if (key.endsWith(".two") && value === "") {
223+
return strings[key.replace(".two", ".other")] || "";
224+
}
225+
226+
if (key.endsWith(".few") && value === "") {
227+
return strings[key.replace(".few", ".other")] || "";
228+
}
229+
230+
if (key.endsWith(".many") && value === "") {
231+
return strings[key.replace(".many", ".other")] || "";
232+
}
233+
234+
return value;
235+
}
208236
}
209237

210238
const sourceFilesGlob = args.module

0 commit comments

Comments
 (0)