Files
Guillaume AssierandClaude Opus 5.5 0c24b2b5d2
CI / Formatting, tests and builds (push) Successful in 3m19s
refactor: code en anglais, rustscreen ouvre l'application de bureau
Code en anglais (#83): identifiants, noms de fichiers et de modules,
commentaires, journal (champ event, noms d'événements anglais),
messages d'erreur, commandes Tauri, types générés, clés de traduction,
sorties de rustscreen-cli, scripts, Nix et CI. Les textes affichés
passent toujours par locales/ (français et anglais); la documentation et
les commits restent en français. AGENTS.md porte la règle.

Ce qui est déjà écrit chez l'utilisateur se relit: anciens noms de
champs des réglages (alias serde), reglages.json à défaut de
settings.json, anciennes clés localStorage dont l'identifiant d'appareil,
jeton de restauration du portail, anciennes options de la ligne de
commande, ancien argument du démarrage automatique.

rustscreen ouvre l'application de bureau (#84): la ligne de commande
devient rustscreen-cli. Le paquet Nix par défaut construit l'application
(interface pnpm, Tauri avec WebKitGTK et PipeWire, icône de barre
système), avec rustscreen-cli et le viewer.

Début du jalon 0.6.0, pas encore essayé en vrai: miroir du viewer
(#29), barres qui s'effacent en fenêtre et en plein écran (#31, #81),
confirmation avant de quitter pendant un partage (#43), démarrage
automatique caché dans la barre système et fichier .desktop réparé au
lancement (#32).

Closes #83, #84
Refs #29, #31, #32, #43, #81

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 11:11:34 +01:00

193 lines
6.6 KiB
JavaScript

#!/usr/bin/env node
// Checks the translations (#21), without dependencies:
//
// - each `locales/<code>.json` file carries its code and its name;
// - no language has a key that English lacks (a typo does not pass);
// - English and French are complete: English is the fallback language,
// French the working language of the project;
// - each key called through `t('...')` in the code exists in English;
// - no component writes hard-coded text, neither between two tags nor in
// `aria-label`, `title`, `placeholder` or `alt`.
//
// Hard-coded text detection is a reading heuristic, not a JSX parser: it
// catches what is usually written, not everything. A false positive is fixed
// by going through `t()`, never by weakening the check.
//
// Usage: node scripts/check-i18n.mjs <src dir> <section> [...]
// node scripts/check-i18n.mjs apps/viewer/src viewer apps/desktop/src desktop
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { dirname, join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const localesDir = join(root, 'locales');
const COMPLETE = ['en', 'fr'];
const CATEGORIES = new Set(['zero', 'one', 'two', 'few', 'many', 'other']);
// The language header of each locale file, also read by the host.
const HEADER = '_language';
const errors = [];
function isPlural(value) {
return (
typeof value === 'object' &&
value !== null &&
Object.keys(value).length > 0 &&
Object.entries(value).every(
([key, v]) => (CATEGORIES.has(key) || /^\d+$/.test(key)) && typeof v === 'string',
)
);
}
/** The leaf keys of a catalog, a plural counting as one key. */
function keys(node, prefix = '') {
const out = new Set();
for (const [key, value] of Object.entries(node)) {
if (key === HEADER) continue;
const path = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string' || isPlural(value)) {
out.add(path);
} else if (typeof value === 'object' && value !== null) {
for (const sub of keys(value, path)) out.add(sub);
} else {
errors.push(`locales: ${path} is neither a text nor a plural`);
}
}
return out;
}
const catalogs = new Map();
for (const file of readdirSync(localesDir).filter((f) => f.endsWith('.json'))) {
const code = file.replace(/\.json$/, '');
let catalog;
try {
catalog = JSON.parse(readFileSync(join(localesDir, file), 'utf8'));
} catch (e) {
errors.push(`locales/${file}: invalid JSON (${e.message})`);
continue;
}
// Project writing convention: no em dash.
if (readFileSync(join(localesDir, file), 'utf8').includes('\u2014')) {
errors.push(`locales/${file}: em dash forbidden`);
}
if (catalog[HEADER]?.code !== code || !catalog[HEADER]?.name) {
errors.push(`locales/${file}: ${HEADER}.code must be "${code}", with a name`);
}
catalogs.set(code, keys(catalog));
}
const english = catalogs.get('en');
if (!english) {
console.error('locales/en.json is missing: it is the fallback language');
process.exit(1);
}
for (const [code, set] of catalogs) {
for (const key of set) {
if (!english.has(key)) errors.push(`locales/${code}.json: key unknown to English: ${key}`);
}
if (COMPLETE.includes(code)) {
for (const key of english) {
if (!set.has(key)) errors.push(`locales/${code}.json: missing ${key}`);
}
}
}
function sourceFiles(dir) {
const out = [];
for (const name of readdirSync(dir)) {
const path = join(dir, name);
if (statSync(path).isDirectory()) {
if (name === 'bindings' || name === 'proto' || name === 'node_modules') continue;
out.push(...sourceFiles(path));
} else if (/\.(ts|tsx)$/.test(name) && !/\.test\.tsx?$/.test(name)) {
out.push(path);
}
}
return out;
}
const LETTER = /[A-Za-zÀ-ÿ]/;
const ATTRIBUTE = /\b(aria-label|title|placeholder|alt)=(['"])([^'"]*)\2/g;
const SAME_LINE = />\s*([^<>{}\n]*?)\s*<\//g;
// Text stuck to an expression in a JSX line: `> port {p}</span>`.
const AROUND = /[>}]([^<>{}\n]*)(?=[{<])/g;
const CALL = /\bt\(\s*(['"`])([^'"`]+)\1/g;
const args = process.argv.slice(2);
if (args.length % 2 !== 0) {
console.error('usage: check-i18n.mjs <src dir> <section> [...]');
process.exit(2);
}
for (let i = 0; i < args.length; i += 2) {
const dir = join(root, args[i]);
const section = args[i + 1];
for (const file of sourceFiles(dir)) {
const name = relative(root, file);
// Comments are not displayed.
const source = readFileSync(file, 'utf8')
.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
.replace(/(^|[^:])\/\/.*$/gm, '$1');
for (const [, quote, key] of source.matchAll(CALL)) {
if (quote === '`') continue; // computed key (`errors.${code}`)
if (!english.has(`${section}.${key}`)) {
errors.push(`${name}: t('${key}') does not exist in locales/en.json (${section})`);
}
}
if (!file.endsWith('.tsx')) continue;
const lines = source.split('\n');
lines.forEach((line, n) => {
for (const [, attribute, , text] of line.matchAll(ATTRIBUTE)) {
if (LETTER.test(text)) {
errors.push(`${name}:${n + 1}: hard-coded ${attribute}: "${text}"`);
}
}
const jsx = line.includes('</') || line.trim().startsWith('<');
const seen = new Set();
for (const [, text] of [...line.matchAll(SAME_LINE), ...(jsx ? line.matchAll(AROUND) : [])]) {
const trimmed = text.trim();
// `=>` and the operators of an expression are not text.
if (LETTER.test(trimmed) && !/[=&|?:;]/.test(trimmed) && !seen.has(trimmed)) {
seen.add(trimmed);
errors.push(`${name}:${n + 1}: hard-coded text: "${trimmed}"`);
}
}
// Text alone on its line, between an opening tag and what follows.
const alone = line.trim();
const previous = lines
.slice(0, n)
.reverse()
.find((l) => l.trim() !== '')
?.trim();
if (
previous?.endsWith('>') &&
!previous.endsWith('=>') &&
/^[A-Za-zÀ-ÿ][^;{}()=<>]*$/.test(alone)
) {
errors.push(`${name}:${n + 1}: hard-coded text: "${alone}"`);
}
});
}
}
// Viewer error messages are called by code (`errors.${code}`): every
// protocol code, plus the viewer's own, must have one.
const errorCodes = join(root, 'apps/viewer/src/proto/ErrorCode.ts');
if (args.includes('viewer')) {
const codes = [...readFileSync(errorCodes, 'utf8').matchAll(/"(\w+)"/g)].map((m) => m[1]);
for (const code of [...codes, 'network', 'unsupported']) {
if (!english.has(`viewer.errors.${code}`)) {
errors.push(`locales/en.json: error code ${code} has no message (viewer.errors)`);
}
}
}
if (errors.length > 0) {
for (const error of errors) console.error(error);
console.error(`\n${errors.length} translation problem(s)`);
process.exit(1);
}
console.log(`translations checked: ${[...catalogs.keys()].sort().join(', ')}`);