feat(translations): spreadsheet workflow for translating theme labels

translations/labels.csv holds every translatable label (164 strings extracted
from config.xml: option titles, category names, optionlabels, dropdown
optgroup/option labels) with one column per language (default nl).
translations/sync_labels.php re-extracts labels from config.xml and merges:
new strings appended empty, existing translations preserved, removed strings
dropped; --check exits 1 when stale (run after config.xml changes).
translations/csv_to_sql.php turns a filled-in CSV column into idempotent SQL
for lime_source_message/lime_message. README i18n section documents the
workflow.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Tammo ter Hark 2026-11-30 15:11:23 +01:00
commit 7a758d807a

View file

@ -0,0 +1,75 @@
<?php
/**
* Generate the SQL that loads the translations from translations/labels.csv
* into the LimeSurvey message tables (lime_source_message + lime_message) of
* one installation — the mechanism documented in README "Multiple languages in
* the theme options (i18n)".
*
* Usage:
* php translations/csv_to_sql.php <labels.csv> <language-code> > translations.sql
* e.g. php translations/csv_to_sql.php labels.csv nl > nl.sql
*
* Then run the generated SQL in the LimeSurvey database, e.g.:
* mysql -h localhost -u <user> -p <database> < translations.sql
*
* The generated SQL is idempotent: existing source messages are reused
* (INSERT IGNORE) and existing translations are updated (ON DUPLICATE KEY
* 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
* title/label in config.xml are translated by gT() — sync_labels.php keeps
* the CSV in sync with config.xml.
*/
if (PHP_SAPI !== 'cli') {
fwrite(STDERR, "This script must be run from the command line.\n");
exit(1);
}
$csvFile = $argv[1] ?? '';
$lang = $argv[2] ?? '';
if ($csvFile === '' || $lang === '' || !is_file($csvFile)) {
fwrite(STDERR, "Usage: php translations/csv_to_sql.php <labels.csv> <language-code>\n");
exit(1);
}
if (!preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $lang)) {
fwrite(STDERR, "Language code must look like 'nl' or 'pt_PT'\n");
exit(1);
}
$fh = fopen($csvFile, 'r');
$header = fgetcsv($fh);
if ($header === false || count($header) < 4) {
fwrite(STDERR, "CSV header must be context,key,source,<lang...>\n");
exit(1);
}
$langIndex = array_search($lang, array_slice($header, 3), true);
if ($langIndex === false) {
fwrite(STDERR, "No column '$lang' in $csvFile (header: " . implode(',', array_slice($header, 3)) . ")\n");
exit(1);
}
$langCol = 3 + $langIndex;
$sql = fn($s) => str_replace("'", "''", $s);
echo "-- Theme labels ($lang) 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 "SET NAMES utf8mb4;\n\n";
$n = 0;
while (($r = fgetcsv($fh)) !== false) {
if (count($r) <= $langCol) {
continue;
}
$source = $r[2] ?? '';
$translation = $r[$langCol] ?? '';
if (trim($source) === '' || trim($translation) === '') {
continue;
}
echo "INSERT IGNORE INTO lime_source_message (category, message) VALUES ('', '" . $sql($source) . "');\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 " ON DUPLICATE KEY UPDATE translation = VALUES(translation);\n\n";
$n++;
}
fclose($fh);
echo "-- $n translation(s) for '$lang'\n";