feat(translations): multi-language support in csv_to_sql + tutorial

csv_to_sql.php now accepts several language codes in one call
(labels.csv nl de fr > all.sql) and reports per-language counts; unknown
or missing columns are rejected with exit 1. translations/README.md shows
a concrete nl+de example and documents that sync_labels.php preserves any
language columns added to the CSV header.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Tammo ter Hark 2026-11-30 15:15:57 +01:00
commit 6786adf092
2 changed files with 68 additions and 34 deletions

View file

@ -28,10 +28,19 @@ Open `translations/labels.csv` in Excel, LibreOffice Calc or Google Sheets
Do not edit the `source` column: the source string must match the `title=` / Do not edit the `source` column: the source string must match the `title=` /
label in `config.xml` exactly, or the translation is not picked up. label in `config.xml` exactly, or the translation is not picked up.
## 2. Translate ## 2. Translate — multiple languages
Fill in the translation cells for the languages you need. Add another language Fill in the translation cells for the languages you need. The CSV ships with
by adding a column to the header row, e.g. `de`, `fr`, `pt_PT`. one language column (`nl`); **other languages follow the same pattern**: add a
column to the header row for each language, e.g. `de`, `fr`, `pt_PT`:
context,key,source,nl,de,fr
category,"(category name)",Header,Koptekst,Überschrift,Titre
option font (Default font and colors),font,"Default font (body text)","Standaard lettertype (bodytekst)","Standardschriftart (Fließtext)","Police par défaut (texte)"
`sync_labels.php` preserves every language column automatically — adding a
column never loses translations, and new strings get an empty cell in each
language column.
Notes: Notes:
@ -57,12 +66,18 @@ reported on screen). To check staleness without writing (e.g. in a dev check):
## 4. Generate the SQL and load it into the database ## 4. Generate the SQL and load it into the database
One language:
php translations/csv_to_sql.php translations/labels.csv nl > nl.sql php translations/csv_to_sql.php translations/labels.csv nl > nl.sql
mysql -h localhost -u <dbuser> -p <dbname> < nl.sql
Or several languages in one go (one SQL file with all of them):
php translations/csv_to_sql.php translations/labels.csv nl de fr > all.sql
mysql -h localhost -u <dbuser> -p <dbname> < all.sql
The generated SQL is idempotent (`INSERT IGNORE` + `ON DUPLICATE KEY UPDATE`), so The generated SQL is idempotent (`INSERT IGNORE` + `ON DUPLICATE KEY UPDATE`), so
re-run it after every edit of the CSV. Repeat per language (`... labels.csv de > re-run it after every edit of the CSV. Empty translation cells are skipped per
de.sql`, etc.). Empty translation cells are skipped. language.
The admin sees the translations as soon as their UI language matches — no theme The admin sees the translations as soon as their UI language matches — no theme
reset or asset flush is needed (the message tables are read at runtime). To ship reset or asset flush is needed (the message tables are read at runtime). To ship

View file

@ -6,18 +6,21 @@
* the theme options (i18n)". * the theme options (i18n)".
* *
* Usage: * Usage:
* php translations/csv_to_sql.php <labels.csv> <language-code> > translations.sql * php translations/csv_to_sql.php <labels.csv> <language-code> [<language-code> ...]
* > translations.sql
* e.g. php translations/csv_to_sql.php labels.csv nl > nl.sql * e.g. php translations/csv_to_sql.php labels.csv nl > nl.sql
* php translations/csv_to_sql.php labels.csv nl de fr > all.sql
* *
* Then run the generated SQL in the LimeSurvey database, e.g.: * Then run the generated SQL in the LimeSurvey database, e.g.:
* mysql -h localhost -u <user> -p <database> < translations.sql * mysql -h localhost -u <user> -p <database> < translations.sql
* *
* Multiple language codes in one call produce one SQL file with all of them.
* The generated SQL is idempotent: existing source messages are reused * The generated SQL is idempotent: existing source messages are reused
* (INSERT IGNORE) and existing translations are updated (ON DUPLICATE KEY * (INSERT IGNORE) and existing translations are updated (ON DUPLICATE KEY
* UPDATE), so re-running after editing the CSV is safe. Rows with an empty * UPDATE), so re-running after editing the CSV is safe. Rows with an empty
* translation cell are skipped. Only strings whose source exactly matches a * translation cell are skipped per language. Only strings whose source exactly
* title/label in config.xml are translated by gT() — sync_labels.php keeps * matches a title/label in config.xml are translated by gT() — sync_labels.php
* the CSV in sync with config.xml. * keeps the CSV in sync with config.xml.
*/ */
if (PHP_SAPI !== 'cli') { if (PHP_SAPI !== 'cli') {
@ -26,14 +29,16 @@ if (PHP_SAPI !== 'cli') {
} }
$csvFile = $argv[1] ?? ''; $csvFile = $argv[1] ?? '';
$lang = $argv[2] ?? ''; $langs = array_slice($argv, 2);
if ($csvFile === '' || $lang === '' || !is_file($csvFile)) { if ($csvFile === '' || empty($langs) || !is_file($csvFile)) {
fwrite(STDERR, "Usage: php translations/csv_to_sql.php <labels.csv> <language-code>\n"); fwrite(STDERR, "Usage: php translations/csv_to_sql.php <labels.csv> <language-code> [<language-code> ...]\n");
exit(1); exit(1);
} }
if (!preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $lang)) { foreach ($langs as $lang) {
fwrite(STDERR, "Language code must look like 'nl' or 'pt_PT'\n"); if (!preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $lang)) {
fwrite(STDERR, "Language code must look like 'nl' or 'pt_PT': $lang\n");
exit(1); exit(1);
}
} }
$fh = fopen($csvFile, 'r'); $fh = fopen($csvFile, 'r');
@ -42,34 +47,48 @@ if ($header === false || count($header) < 4) {
fwrite(STDERR, "CSV header must be context,key,source,<lang...>\n"); fwrite(STDERR, "CSV header must be context,key,source,<lang...>\n");
exit(1); exit(1);
} }
$langIndex = array_search($lang, array_slice($header, 3), true); $langCols = [];
if ($langIndex === false) { foreach ($langs as $lang) {
$idx = array_search($lang, array_slice($header, 3), true);
if ($idx === false) {
fwrite(STDERR, "No column '$lang' in $csvFile (header: " . implode(',', array_slice($header, 3)) . ")\n"); fwrite(STDERR, "No column '$lang' in $csvFile (header: " . implode(',', array_slice($header, 3)) . ")\n");
exit(1); exit(1);
}
$langCols[$lang] = 3 + $idx;
} }
$langCol = 3 + $langIndex;
$sql = fn($s) => str_replace("'", "''", $s); $sql = fn($s) => str_replace("'", "''", $s);
echo "-- Theme labels ($lang) generated from $csvFile by translations/csv_to_sql.php\n"; echo "-- Theme labels (" . implode(', ', $langs) . ") generated from $csvFile by translations/csv_to_sql.php\n";
echo "-- Run in the LimeSurvey database. Idempotent: safe to re-run after editing the CSV.\n"; echo "-- Run in the LimeSurvey database. Idempotent: safe to re-run after editing the CSV.\n";
echo "SET NAMES utf8mb4;\n\n"; echo "SET NAMES utf8mb4;\n\n";
$n = 0; $counts = array_fill_keys($langs, 0);
while (($r = fgetcsv($fh)) !== false) { while (($r = fgetcsv($fh)) !== false) {
if (count($r) <= $langCol) {
continue;
}
$source = $r[2] ?? ''; $source = $r[2] ?? '';
$translation = $r[$langCol] ?? ''; if (trim($source) === '') {
if (trim($source) === '' || trim($translation) === '') {
continue; continue;
} }
$emitted = false;
foreach ($langCols as $lang => $col) {
$translation = $r[$col] ?? '';
if (trim($translation) === '') {
continue;
}
if (!$emitted) {
echo "INSERT IGNORE INTO lime_source_message (category, message) VALUES ('', '" . $sql($source) . "');\n"; echo "INSERT IGNORE INTO lime_source_message (category, message) VALUES ('', '" . $sql($source) . "');\n";
$emitted = true;
}
echo "INSERT INTO lime_message (id, language, translation)\n"; echo "INSERT INTO lime_message (id, language, translation)\n";
echo " SELECT id, '$lang', '" . $sql($translation) . "' FROM lime_source_message WHERE category = '' AND message = '" . $sql($source) . "'\n"; echo " SELECT id, '$lang', '" . $sql($translation) . "' FROM lime_source_message WHERE category = '' AND message = '" . $sql($source) . "'\n";
echo " ON DUPLICATE KEY UPDATE translation = VALUES(translation);\n\n"; echo " ON DUPLICATE KEY UPDATE translation = VALUES(translation);\n";
$n++; $counts[$lang]++;
}
if ($emitted) {
echo "\n";
}
} }
fclose($fh); fclose($fh);
echo "-- $n translation(s) for '$lang'\n"; foreach ($counts as $lang => $n) {
echo "-- $n translation(s) for '$lang'\n";
}