143 lines
4.5 KiB
JavaScript
143 lines
4.5 KiB
JavaScript
/**
|
|
* Exhaustive export of user-facing i18n keys (web EN/FR + mobile app_strings).
|
|
* Excludes web admin.* namespace.
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
|
|
const root = path.resolve(__dirname, '..');
|
|
const outDir = path.join(root, 'docs', 'exports');
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
|
|
function flatten(obj, prefix = '', out = []) {
|
|
if (obj === null || obj === undefined) return out;
|
|
if (typeof obj !== 'object' || Array.isArray(obj)) {
|
|
out.push({ key: prefix, value: String(obj) });
|
|
return out;
|
|
}
|
|
for (const [k, v] of Object.entries(obj)) {
|
|
const next = prefix ? `${prefix}.${k}` : k;
|
|
if (v !== null && typeof v === 'object' && !Array.isArray(v)) flatten(v, next, out);
|
|
else out.push({ key: next, value: v == null ? '' : String(v) });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function isAdminKey(key) {
|
|
return key === 'admin' || key.startsWith('admin.');
|
|
}
|
|
|
|
function loadLocaleTs(file) {
|
|
let src = fs.readFileSync(file, 'utf8');
|
|
src = src.replace(/^import[^\n]*\n/gm, '');
|
|
// Drop trailing type-only exports.
|
|
src = src.replace(/\nexport\s+type[\s\S]*$/m, '\n');
|
|
src = src.replace(/export\s+const\s+(\w+)\s*=\s*/, 'var __locale = ');
|
|
src = src.replace(/\s+as\s+const\s*;?\s*$/m, '');
|
|
src = src.replace(/:\s*Record<[^>]+>/g, '');
|
|
src = src.replace(/\s+satisfies\s+[^=;\n]+/g, '');
|
|
// admin.reports.catalog references reportCatalogEn/Fr — stub (filtered out via admin.*).
|
|
src = `var reportCatalogEn = {};\nvar reportCatalogFr = {};\n` + src;
|
|
src += '\n;module.exports = __locale;';
|
|
const sandbox = { module: { exports: {} }, exports: {} };
|
|
vm.runInNewContext(src, sandbox, { filename: file });
|
|
return sandbox.module.exports;
|
|
}
|
|
|
|
function unescapeJsString(s) {
|
|
return s
|
|
.replace(/\\'/g, "'")
|
|
.replace(/\\"/g, '"')
|
|
.replace(/\\n/g, '\n')
|
|
.replace(/\\\\/g, '\\');
|
|
}
|
|
|
|
function extractMobileStrings(dartPath) {
|
|
const dart = fs.readFileSync(dartPath, 'utf8');
|
|
const rows = [];
|
|
const seen = new Set();
|
|
|
|
// Multiline-safe: String get name => t('en', 'fr');
|
|
// and String name(...) => t('en', 'fr');
|
|
const re =
|
|
/String\s+(?:get\s+)?(\w+)\s*(?:\([^;{]*\))?\s*=>\s*t\(\s*((?:'(?:\\'|[^'])*'|"(?:\\"|[^"])*")\s*,\s*(?:'(?:\\'|[^'])*'|"(?:\\"|[^"])*"))\s*\)/gs;
|
|
|
|
let m;
|
|
while ((m = re.exec(dart))) {
|
|
const key = m[1];
|
|
if (seen.has(key)) continue;
|
|
const pair = m[2];
|
|
const parts = pair.match(/'(?:\\'|[^'])*'|"(?:\\"|[^"])*"/g);
|
|
if (!parts || parts.length < 2) continue;
|
|
const en = unescapeJsString(parts[0].slice(1, -1));
|
|
const fr = unescapeJsString(parts[1].slice(1, -1));
|
|
seen.add(key);
|
|
rows.push({
|
|
platform: 'mobile',
|
|
feature: 'app_strings',
|
|
key,
|
|
en,
|
|
fr,
|
|
});
|
|
}
|
|
|
|
// Fallback: localizeReferralReason style maps with t() inside switch — skip for key dump
|
|
return rows;
|
|
}
|
|
|
|
function csvEscape(s) {
|
|
const v = String(s ?? '');
|
|
if (/[",\n\r]/.test(v)) return `"${v.replace(/"/g, '""')}"`;
|
|
return v;
|
|
}
|
|
|
|
const en = loadLocaleTs(path.join(root, 'prediqo_web/src/shared/i18n/locales/en.ts'));
|
|
const fr = loadLocaleTs(path.join(root, 'prediqo_web/src/shared/i18n/locales/fr.ts'));
|
|
|
|
const enFlat = flatten(en).filter((x) => !isAdminKey(x.key));
|
|
const frMap = Object.fromEntries(flatten(fr).map((x) => [x.key, x.value]));
|
|
|
|
const webRows = enFlat.map((x) => ({
|
|
platform: 'web',
|
|
feature: x.key.split('.')[0] || '',
|
|
key: x.key,
|
|
en: x.value,
|
|
fr: frMap[x.key] ?? '',
|
|
}));
|
|
|
|
const mobileRows = extractMobileStrings(
|
|
path.join(root, 'prediqo_mobile/lib/l10n/app_strings.dart')
|
|
);
|
|
|
|
const missingFr = webRows.filter((r) => !r.fr).map((r) => r.key);
|
|
const all = [...webRows, ...mobileRows];
|
|
|
|
const payload = {
|
|
generatedAt: new Date().toISOString(),
|
|
scope:
|
|
'Exhaustive user-facing i18n keys. Web excludes admin.*. Mobile from app_strings.dart getters/methods using t(en, fr).',
|
|
counts: {
|
|
total: all.length,
|
|
web: webRows.length,
|
|
mobile: mobileRows.length,
|
|
webMissingFr: missingFr.length,
|
|
},
|
|
webMissingFr: missingFr,
|
|
items: all,
|
|
};
|
|
|
|
const header = ['platform', 'feature', 'key', 'en', 'fr'];
|
|
const csv = [header.join(',')]
|
|
.concat(all.map((r) => header.map((h) => csvEscape(r[h])).join(',')))
|
|
.join('\n');
|
|
|
|
const jsonPath = path.join(outDir, 'user-i18n-keys.json');
|
|
const csvPath = path.join(outDir, 'user-i18n-keys.csv');
|
|
fs.writeFileSync(jsonPath, JSON.stringify(payload, null, 2), 'utf8');
|
|
fs.writeFileSync(csvPath, `\uFEFF${csv}\n`, 'utf8'); // BOM for Excel
|
|
|
|
console.log(JSON.stringify(payload.counts, null, 2));
|
|
console.log('JSON:', jsonPath);
|
|
console.log('CSV:', csvPath);
|