<?php/** * Collects every French player-visible string of "Rob revisited" (src/Revisited/** and src/Scene/RevisitedScene.php) * and updates the English dictionary assets/revisited/lang/en.php, keeping the translations already written. * * bin/jumphp php games/alis tools/reborn/i18n_extract.php [--check] [--list] * * --check : writes nothing, exits 1 when keys are new (not in the dictionary) or untranslated. * --list : prints every extracted key with its file:line. * * What is a key: * - a plain literal 'Une pierre lourde.' -> itself; * - a concatenation 'Tu ramasses ' . $n . ' ' . $name . '.' -> 'Tu ramasses {0} {1}.' (one {n} per expression); * - an interpolated "Il reste {$n} jours" / "$n jours" -> 'Il reste {0} jours'; * - the format of sprintf / printf: '%d km, %s' -> '{0} km, {1}' (%% -> %); * - strtr / Lang-style templates keep their own {name} tokens. * Skipped: array keys, ids / file names (no space, snake_case, paths), GLSL, strings without a French cue, * strings whose letters are all in placeholders. */$root = dirname(__DIR__, 2);$args = array_slice($_SERVER['argv'] ?? [], 1);$check = in_array('--check', $args, true);$list = in_array('--list', $args, true);$dictFile = $root . '/assets/revisited/lang/en.php';require_once $root . '/src/Revisited/I18n/Extractor.php';$files = App\Revisited\I18n\Extractor::sourceFiles($root);$found = []; // key => list<"file:line">foreach ($files as $file) { foreach (App\Revisited\I18n\Extractor::extract((string) file_get_contents($file)) as [$key, $line]) { $found[$key][] = substr($file, strlen($root) + 1) . ':' . $line; }}ksort($found);$old = is_file($dictFile) ? require $dictFile : [];$old = is_array($old) ? $old : [];$new = array_values(array_diff(array_keys($found), array_keys($old)));$removed = array_values(array_diff(array_keys($old), array_keys($found)));$untranslated = array_keys(array_filter($old, fn ($v, $k) => isset($found[$k]) && ($v === null || $v === ''), ARRAY_FILTER_USE_BOTH));if ($list) { foreach ($found as $k => $where) { echo $where[0], "\t", $k, "\n"; }}printf("%d files, %d keys, %d translated, %d new, %d untranslated, %d removed (kept in the file, marked)\n", count($files), count($found), count($found) - count($new) - count($untranslated), count($new), count($untranslated), count($removed));foreach ($new as $k) { echo " + ", $found[$k][0], " ", $k, "\n";}foreach ($removed as $k) { echo " - ", $k, "\n";}if ($check) { exit($new || $untranslated ? 1 : 0);}// Rewrites the dictionary: the glossary header of the file is kept, then every key (sorted), its English (or ''),// a comment with where it is; removed keys stay at the end under a marker (a code move must not lose a translation).$header = "<?php\n\n";if (is_file($dictFile)) { $text = (string) file_get_contents($dictFile); $cut = strpos($text, "return ["); if ($cut !== false) { $header = substr($text, 0, $cut); }}$e = static fn (string $s): string => "'" . str_replace(['\\', "'"], ['\\\\', "\\'"], $s) . "'";$out = $header . "return [\n";foreach ($found as $k => $where) { $out .= ' // ' . $where[0] . (count($where) > 1 ? ' (+' . (count($where) - 1) . ')' : '') . "\n"; $out .= ' ' . $e($k) . ' => ' . $e((string) ($old[$k] ?? '')) . ",\n";}if ($removed) { $out .= "\n // ---- no longer in the code (kept: delete when sure) ----\n"; foreach ($removed as $k) { $out .= ' ' . $e($k) . ' => ' . $e((string) $old[$k]) . ",\n"; }}$out .= "];\n";@mkdir(dirname($dictFile), 0777, true);file_put_contents($dictFile, $out);echo "written ", substr($dictFile, strlen($root) + 1), "\n";