// Generates the string resources of the Android app from the English source and the translations: // i18n/app-strings.json the English source ("strings" and "plurals") // i18n/translations/.json one file per language: { "strings": {key: text}, "plurals": {key: {category: text}} } // app/src/main/res/values/strings.xml, values-/strings.xml, res/xml/locales_config.xml (generated) // Usage: node android/scripts/gen-strings.mjs [--check] [--review] [--root ] // --check writes nothing; exits 1 if a generated file is stale or a translation is wrong (a missing or extra key, a placeholder that // does not match the English one, a missing plural form). // --review writes i18n/REVIEW.md: the strings of each language that were written for Coucou for Android and not taken from the Mac and // Windows apps (windows/src/i18n/strings.json), so a native speaker can check them. // --root work in another folder with the same layout (the tests use it). import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const here = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const argv = process.argv.slice(2); const rootArg = argv.indexOf("--root"); const root = rootArg >= 0 ? path.resolve(argv[rootArg + 1]) : here; const check = argv.includes("--check"); const review = argv.includes("--review"); // Every language the app can have: the BCP 47 tag, the folder qualifier of its resources, its name in its own language (the picker shows // it so a person can find their language whatever the phone speaks), and the plural categories Android needs for it (CLDR). export const LANGUAGES = [ { tag: "fr", qualifier: "fr", name: "Français", plural: ["one", "many", "other"] }, { tag: "es", qualifier: "es", name: "Español", plural: ["one", "many", "other"] }, { tag: "pt-BR", qualifier: "pt-rBR", name: "Português (Brasil)", plural: ["one", "many", "other"] }, { tag: "zh-Hans", qualifier: "b+zh+Hans", name: "简体中文", plural: ["other"] }, { tag: "hi", qualifier: "hi", name: "हिन्दी", plural: ["one", "other"] }, { tag: "bn", qualifier: "bn", name: "বাংলা", plural: ["one", "other"] }, { tag: "ru", qualifier: "ru", name: "Русский", plural: ["one", "few", "many", "other"] }, { tag: "id", qualifier: "in", name: "Bahasa Indonesia", plural: ["other"] }, { tag: "ar", qualifier: "ar", name: "العربية", plural: ["zero", "one", "two", "few", "many", "other"] }, { tag: "tr", qualifier: "tr", name: "Türkçe", plural: ["one", "other"] }, { tag: "de", qualifier: "de", name: "Deutsch", plural: ["one", "other"] }, { tag: "ja", qualifier: "ja", name: "日本語", plural: ["other"] }, { tag: "ko", qualifier: "ko", name: "한국어", plural: ["other"] }, { tag: "zh-Hant", qualifier: "b+zh+Hant", name: "繁體中文", plural: ["other"] }, { tag: "th", qualifier: "th", name: "ภาษาไทย", plural: ["other"] }, { tag: "it", qualifier: "it", name: "Italiano", plural: ["one", "many", "other"] }, ]; const ENGLISH_PLURAL = ["one", "other"]; const res = path.join(root, "app/src/main/res"); const source = JSON.parse(fs.readFileSync(path.join(root, "i18n/app-strings.json"), "utf8")); const strings = source.strings; const plurals = source.plurals ?? {}; const BS = String.fromCharCode(92); const esc = (s) => s.split(BS).join(BS + BS) .replace(/&/g, "&").replace(//g, ">") .replace(/'/g, BS + "'").replace(/"/g, BS + '"') .replace(/^([@?])/, BS + "$1"); // Placeholders must be positional printf arguments (%1$s), not {name}. const everyText = [...Object.values(strings), ...Object.values(plurals).flatMap((p) => Object.values(p))]; if (everyText.some((v) => /\{\w+\}/.test(v))) { console.error("Placeholders are not supported; use %1$s in app-strings.json."); process.exit(2); } /** The placeholders of a text as a sorted list ("%1$d", "%2$s", "%%"), so two texts can be compared whatever the word order. */ const placeholders = (s) => (s.match(/%(?:\d+\$)?[sd%]/g) ?? []).sort().join(" "); const xml = (body) => `\n\n\n${body}\n\n`; const stringLines = (map) => Object.entries(map).map(([k, v]) => ` ${esc(v)}`); const pluralBlocks = (map, order) => Object.entries(map).map(([k, forms]) => ` \n` + order.filter((c) => forms[c] !== undefined).map((c) => ` ${esc(forms[c])}`).join("\n") + `\n `); const ALL = ["zero", "one", "two", "few", "many", "other"]; const outputs = new Map(); // path -> content outputs.set(path.join(res, "values/strings.xml"), xml([...stringLines(strings), ...pluralBlocks(plurals, ALL)].join("\n"))); // The words of the pure rules are looked up by key at run time. A table of static references (R.string.x) lets the resource shrinker see // that every string is used, which a lookup by name would hide from it. outputs.set( path.join(root, "app/src/main/kotlin/com/coucou/android/app/TextIds.kt"), `// Generated by android/scripts/gen-strings.mjs. Do not edit.\npackage com.coucou.android.app\n\nimport com.coucou.android.R\n\n` + `/** Every string and plural of the app by key, for core/Texts.kt (static references, so the shrinker keeps them). */\ninternal object TextIds {\n` + ` val strings: Map = mapOf(\n${Object.keys(strings).map((k) => ` "${k}" to R.string.${k},`).join("\n")}\n )\n\n` + ` val plurals: Map = mapOf(\n${Object.keys(plurals).map((k) => ` "${k}" to R.plurals.${k},`).join("\n")}\n )\n}\n`, ); const problems = []; const found = []; const dir = path.join(root, "i18n/translations"); if (fs.existsSync(dir)) { for (const file of fs.readdirSync(dir).filter((f) => f.endsWith(".json")).sort()) { const tag = file.replace(/\.json$/, ""); const lang = LANGUAGES.find((l) => l.tag === tag); if (!lang) { problems.push(`${file}: not a language the app knows (see LANGUAGES in gen-strings.mjs)`); continue; } found.push(lang); const t = JSON.parse(fs.readFileSync(path.join(dir, file), "utf8")); const ts = t.strings ?? {}; const tp = t.plurals ?? {}; for (const k of Object.keys(strings)) { if (typeof ts[k] !== "string" || ts[k] === "") problems.push(`${tag}: missing string "${k}"`); else if (placeholders(ts[k]) !== placeholders(strings[k])) problems.push(`${tag}: "${k}" has the placeholders [${placeholders(ts[k])}], the English has [${placeholders(strings[k])}]`); } for (const k of Object.keys(ts)) if (!(k in strings)) problems.push(`${tag}: "${k}" is not in app-strings.json`); for (const [k, en] of Object.entries(plurals)) { const forms = tp[k]; if (!forms) { problems.push(`${tag}: missing plural "${k}"`); continue; } for (const c of lang.plural) { if (typeof forms[c] !== "string" || forms[c] === "") problems.push(`${tag}: plural "${k}" has no "${c}" form`); else if (placeholders(forms[c]) !== placeholders(en.other)) problems.push(`${tag}: plural "${k}" form "${c}" has the placeholders [${placeholders(forms[c])}], the English has [${placeholders(en.other)}]`); } for (const c of Object.keys(forms)) if (!lang.plural.includes(c)) problems.push(`${tag}: plural "${k}" has a "${c}" form that ${tag} does not use`); } for (const k of Object.keys(tp)) if (!(k in plurals)) problems.push(`${tag}: plural "${k}" is not in app-strings.json`); const ordered = Object.fromEntries(Object.keys(strings).filter((k) => ts[k] !== undefined).map((k) => [k, ts[k]])); const orderedPlurals = Object.fromEntries(Object.keys(plurals).filter((k) => tp[k] !== undefined).map((k) => [k, tp[k]])); outputs.set(path.join(res, `values-${lang.qualifier}/strings.xml`), xml([...stringLines(ordered), ...pluralBlocks(orderedPlurals, lang.plural)].join("\n"))); } } // The languages the system picker may offer: English and every language with a translation file. const localeTags = ["en", ...LANGUAGES.filter((l) => found.includes(l)).map((l) => l.tag)]; outputs.set( path.join(res, "xml/locales_config.xml"), `\n\n\n` + localeTags.map((t) => ` `).join("\n") + `\n\n`, ); // Folders of languages that no longer have a translation file must go (check mode reports them). const staleDirs = fs.existsSync(res) ? fs.readdirSync(res).filter((d) => d.startsWith("values-") && !["values-notnight"].includes(d) && !outputs.has(path.join(res, d, "strings.xml")) && fs.existsSync(path.join(res, d, "strings.xml"))) : []; /** The text of i18n/REVIEW.md: per language, the strings that were not taken from the Mac and Windows apps. */ function reviewText() { const windowsFile = path.join(root, "../windows/src/i18n/strings.json"); const windows = fs.existsSync(windowsFile) ? JSON.parse(fs.readFileSync(windowsFile, "utf8")).strings : {}; let md = "# Translations to check\n\nGenerated by `node android/scripts/gen-strings.mjs --review`. Every string below was written for Coucou for Android, by a machine, and has not been\n" + "read by a native speaker. The strings that match the Mac and Windows apps' own wording (windows/src/i18n/strings.json) are not listed: they are\n" + "Louis's. A fix is a change to the language's file in `i18n/translations/`, then `node android/scripts/gen-strings.mjs`.\n"; for (const lang of LANGUAGES.filter((l) => found.includes(l))) { const t = JSON.parse(fs.readFileSync(path.join(dir, `${lang.tag}.json`), "utf8")); const keys = Object.keys(strings).filter((k) => { const w = windows[strings[k]]?.[lang.tag]; return !(w && w === t.strings?.[k]); }); md += `\n## ${lang.name} (${lang.tag})\n\n${keys.length} of ${Object.keys(strings).length} strings and ${Object.keys(plurals).length} plural groups.\n\n` + (keys.length ? "`" + keys.join("`, `") + "`\n" : ""); } return md; } if (review) { fs.writeFileSync(path.join(root, "i18n/REVIEW.md"), reviewText()); console.log("i18n/REVIEW.md"); process.exit(0); } if (check) { for (const [file, content] of outputs) { // Windows checkouts may turn LF into CRLF; the content is what matters. const stale = !fs.existsSync(file) || fs.readFileSync(file, "utf8").replace(/\r\n/g, "\n") !== content; if (stale) problems.push(`stale: ${path.relative(root, file)}`); } for (const d of staleDirs) problems.push(`stale: app/src/main/res/${d} has no translation file`); // The list of strings to check is generated too (only once there are translations). const reviewFile = path.join(root, "i18n/REVIEW.md"); if (found.length && (!fs.existsSync(reviewFile) || fs.readFileSync(reviewFile, "utf8").replace(/\r\n/g, "\n") !== reviewText())) problems.push("stale: i18n/REVIEW.md (run gen-strings.mjs --review)"); if (problems.length) { for (const p of problems) console.error(p); process.exit(1); } process.exit(0); } if (problems.length) { for (const p of problems) console.error(p); process.exit(1); } for (const [file, content] of outputs) { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, content); } console.log(`${Object.keys(strings).length} strings, ${Object.keys(plurals).length} plurals, ${found.length} translations`);