| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import assert from 'node:assert/strict';
- import fs from 'node:fs';
- import path from 'node:path';
- import vm from 'node:vm';
- import ts from 'typescript';
- const cache = new Map();
- function load(file) {
- file = path.resolve(file);
- if (!path.extname(file)) file += fs.existsSync(file + '.tsx') ? '.tsx' : '.ts';
- if (file.endsWith('.json')) return JSON.parse(fs.readFileSync(file, 'utf8'));
- if (cache.has(file)) return cache.get(file);
- const module = { exports: {} }; cache.set(file, module.exports);
- const compiled = ts.transpileModule(fs.readFileSync(file, 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText;
- const require = spec => load(spec.startsWith('@/') ? 'src/' + spec.slice(2) : path.resolve(path.dirname(file), spec));
- vm.runInNewContext(compiled, { module, exports: module.exports, require, Intl, Map, URL });
- return module.exports;
- }
- const { appLocales } = load('src/lib/i18n/config');
- const { getMessages, flattenMessages } = load('src/lib/i18n/dictionaries');
- const english = flattenMessages(load('src/lib/i18n/locales/en').en);
- const chinese = flattenMessages(load('src/lib/i18n/locales/zh').zh);
- assert.deepEqual(Object.keys(english).sort(), Object.keys(chinese).sort());
- const matrix = JSON.parse(fs.readFileSync('src/lib/i18n/messages.json', 'utf8'));
- assert.deepEqual(Object.keys(matrix).sort(), Object.keys(english).sort());
- for (const [key, row] of Object.entries(matrix)) assert(row.length === 5 && row.every(value => typeof value === 'string' && value.trim()), key);
- for (const [key, row] of Object.entries(JSON.parse(fs.readFileSync('src/lib/i18n/ui-messages.json', 'utf8')))) assert(row.length === 6 && row.every(value => value.trim()), key);
- const catalog = JSON.parse(fs.readFileSync('src/lib/i18n/catalog-messages.json', 'utf8'));
- const englishFormEdits = JSON.parse(fs.readFileSync('scripts/fixtures/english-form-copy.json', 'utf8')).edits;
- const englishFormCatalog = new Map(englishFormEdits.filter(edit => edit.source === 'catalog').map(edit => [edit.key, edit]));
- for (const [key, row] of Object.entries(catalog)) assert(row.length === 7 && row.every(value => value.trim()), key);
- for (const [key, row] of Object.entries(catalog)) assert.equal(row[0], englishFormCatalog.get(key)?.after ?? key, `English catalogue wording outside the form-copy scope changed: ${key}`);
- for (const edit of englishFormEdits) {
- assert(['base', 'catalog'].includes(edit.source), `Unsupported form-copy source: ${edit.source}`);
- assert.equal(edit.source === 'base' ? english[edit.key] : catalog[edit.key]?.[0], edit.after, `Reviewed English form wording: ${edit.key}`);
- if (edit.source === 'catalog') assert.equal(edit.before, edit.key, `Original schema wording: ${edit.key}`);
- }
- const placeholders = text => [...text.matchAll(/\{\{?\w+\}?\}/g)].map(m => m[0]).sort().join('|');
- for (const [key, row] of Object.entries(matrix)) for (const value of row) assert.equal(placeholders(value), placeholders(english[key]), key);
- let keys = new Set(), files = 0;
- const excluded = new Set(['JsonEditor.tsx', 'JsonConfigEditor.tsx', 'AdminUserSelect.tsx', 'ImageUploadField.tsx']);
- function walk(dir) {
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
- const file = path.join(dir, entry.name);
- if (entry.isDirectory()) { if (!['admin', 'blog'].includes(entry.name)) walk(file); continue; }
- if (!entry.name.endsWith('.tsx') || excluded.has(entry.name)) continue;
- files++;
- const ast = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
- function visit(node) {
- if (ts.isCallExpression(node) && node.expression.getText(ast) === 't' && ts.isStringLiteral(node.arguments[0])) keys.add(node.arguments[0].text);
- ts.forEachChild(node, visit);
- }
- visit(ast);
- }
- }
- walk('src/components');
- for (const locale of appLocales) {
- const messages = getMessages(locale);
- for (const key of keys) assert(typeof messages[key] === 'string' && messages[key].trim(), `${locale}: ${key}`);
- for (const status of ['pending', 'paid', 'succeeded', 'completed', 'failed', 'cancelled']) assert(messages['status.' + status]);
- }
- const { localizedPath, splitLocalePath, publicAppPaths } = load('src/lib/i18n/routes');
- for (const locale of appLocales) {
- for (const route of [...publicAppPaths, '/login', '/dashboard/settings', '/create-order/35', '/payment/confirm', '/blogs/netherlands-visa-appointments-dublin']) {
- const href = localizedPath(route + '?source=guide#details', locale);
- assert.equal(splitLocalePath(href).path, route + '?source=guide#details');
- assert.equal(splitLocalePath(href).locale, locale);
- assert.equal(localizedPath(href, 'en'), route + '?source=guide#details');
- }
- for (const route of ['/admin', '/api/vas/product/detail', 'https://example.com/', '//example.com/']) assert.equal(localizedPath(route, locale), route);
- }
- const { displayProduct, countryLabel } = load('src/lib/i18n/catalog');
- const catalogue = JSON.parse(fs.readFileSync('scripts/fixtures/public-products.json', 'utf8'));
- const products = Array.isArray(catalogue.data) ? catalogue.data : catalogue.data.items;
- for (const locale of appLocales) for (const product of products) {
- const before = JSON.stringify(product);
- const display = displayProduct(product, key => getMessages(locale)[key] ?? key);
- assert.equal(JSON.stringify(product), before, 'Must not mutate API data');
- for (const key of ['id', 'schema_id', 'price_amount', 'price_currency', 'country', 'city', 'visa_type', 'enabled']) assert.equal(display[key], product[key]);
- assert.equal(display.description, getMessages(locale)[product.description] ?? product.description, 'Translate the original API description without replacing its content');
- assert(countryLabel(product.country, locale));
- }
- assert.equal(countryLabel('unknown-catalogue-value', 'fr'), 'unknown-catalogue-value');
- console.log(`PASS: ${appLocales.length} languages, ${Object.keys(english).length} complete base translations, ${keys.size} active static UI keys in ${files} files, interpolation, locale-preserving public/user routes, admin/API exclusions and unchanged catalogue identifiers/values.`);
|