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:
parent
1c3871306d
commit
7a758d807a
4 changed files with 464 additions and 5 deletions
34
README.md
34
README.md
|
|
@ -92,6 +92,11 @@ Arguments for choosing Respondage Vanilla:
|
|||
`.gitignore`): `variants/*.json` (client definitions with house-style
|
||||
values), `variants/leak-patterns.txt` and `variants/tests/` — those live in
|
||||
the private variants repo, not here.
|
||||
- `translations/` — the translation spreadsheet workflow: `labels.csv` (all
|
||||
theme labels, one row per source string, one column per language) plus
|
||||
`sync_labels.php` (re-extracts labels from `config.xml`, keeps existing
|
||||
translations) and `csv_to_sql.php` (CSV → SQL for the `lime_source_message`/
|
||||
`lime_message` tables). See "Multiple languages in the theme options (i18n)".
|
||||
|
||||
## Multiple languages in the theme options (i18n)
|
||||
|
||||
|
|
@ -128,11 +133,30 @@ language — the message tables are the right mechanism for that (a feature
|
|||
request to expose the current language to twig has been prepared for the
|
||||
LimeSurvey developers).
|
||||
|
||||
**Still to be done: translations.** The theme ships only the English source
|
||||
strings — no translations of its own labels (option titles, category names,
|
||||
dropdown labels) are bundled or pre-filled yet. Filling in translations (per
|
||||
language, e.g. Dutch for the options page) via the message tables above is
|
||||
open work; contributions of translations to this repository are welcome.
|
||||
**Translating via a spreadsheet (recommended workflow).** All theme labels
|
||||
live in `translations/labels.csv` — one row per source string
|
||||
(`context,key,source,<language columns...>`, by default one `nl` column), ready
|
||||
to open in Excel/LibreOffice. Keep it up to date during development with:
|
||||
|
||||
php translations/sync_labels.php # re-extract labels from config.xml:
|
||||
# new strings are appended with empty
|
||||
# translation cells, existing translations
|
||||
# are preserved, removed strings are dropped
|
||||
php translations/sync_labels.php --check # exit 1 when labels.csv is stale
|
||||
# (run this after changing config.xml)
|
||||
|
||||
After editing translations in the spreadsheet, generate the SQL for the message
|
||||
tables and run it in the installation's database:
|
||||
|
||||
php translations/csv_to_sql.php translations/labels.csv nl > nl.sql
|
||||
mysql -h localhost -u <user> -p <database> < nl.sql
|
||||
|
||||
(`csv_to_sql.php` emits idempotent INSERTs — safe to re-run after each edit. Add
|
||||
a language by adding a column to the CSV header, e.g. `de`.) To ship
|
||||
translations with the theme itself, commit the filled-in `labels.csv` —
|
||||
**still to be done:** no translations are filled in yet; the theme ships only
|
||||
the English source strings. Contributions of translations to this repository
|
||||
are welcome.
|
||||
|
||||
## Header logos (left / center / right)
|
||||
|
||||
|
|
|
|||
75
translations/csv_to_sql.php
Normal file
75
translations/csv_to_sql.php
Normal 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";
|
||||
166
translations/labels.csv
Normal file
166
translations/labels.csv
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
context,key,source,nl
|
||||
category,"(category name)","Default font and colors",
|
||||
category,"(category name)",Header,
|
||||
category,"(category name)","Page background",
|
||||
category,"(category name)",Groups,
|
||||
category,"(category name)",Questions,
|
||||
category,"(category name)",Buttons,
|
||||
category,"(category name)","Other options",
|
||||
"option font (Default font and colors)",font,"Default font (body text)",
|
||||
"option font optgroup",font,"Bundled open source fonts",
|
||||
"option font dropdown option",font,"Atkinson Hyperlegible (sans-serif, accessibility)",
|
||||
"option font dropdown option",font,"Inter (sans-serif)",
|
||||
"option font dropdown option",font,"Noto Sans (sans-serif, multilingual fallback)",
|
||||
"option font dropdown option",font,"Noto Serif (serif)",
|
||||
"option font optgroup",font,"Custom (this theme)",
|
||||
"option font optgroup",font,"User browser",
|
||||
"option font dropdown option",font,Arial,
|
||||
"option font dropdown option",font,"Arial Black",
|
||||
"option font dropdown option",font,"Comic Sans",
|
||||
"option font dropdown option",font,"Courier New",
|
||||
"option font dropdown option",font,Georgia,
|
||||
"option font dropdown option",font,Impact,
|
||||
"option font dropdown option",font,"Lucida Console",
|
||||
"option font dropdown option",font,"Lucida Sans",
|
||||
"option font dropdown option",font,"Palatino Linotype",
|
||||
"option font dropdown option",font,"Times New Roman",
|
||||
"option font dropdown option",font,Trebuchet,
|
||||
"option fontsize (Default font and colors)",fontsize,"Base font size (body text)",
|
||||
"option fontsize optionlabel",fontsize,"Small (14px)",
|
||||
"option fontsize optionlabel",fontsize,"Normal (16px)",
|
||||
"option fontsize optionlabel",fontsize,"Large (18px)",
|
||||
"option fontsize optionlabel",fontsize,"XLarge
|
||||
(20 px)",
|
||||
"option fontcolor (Default font and colors)",fontcolor,"Default font color (body text)",
|
||||
"option fontcolor optionlabel",fontcolor,Yes,
|
||||
"option fontcolor optionlabel",fontcolor,No,
|
||||
"option defaultbackgroundcolor (Default font and colors)",defaultbackgroundcolor,"Default background color",
|
||||
"option primarycolor (Default font and colors)",primarycolor,"Color 1 (primary)",
|
||||
"option secondarycolor (Default font and colors)",secondarycolor,"Color 2",
|
||||
"option tertiarycolor (Default font and colors)",tertiarycolor,"Color 3",
|
||||
"option headerfont (Header)",headerfont,"Font for the header / top menu",
|
||||
"option navbarfontcolor (Header)",navbarfontcolor,"Font color for the header menu",
|
||||
"option navbarfontcolor optionlabel",navbarfontcolor,"Default font color",
|
||||
"option navbarfontcolor optionlabel",navbarfontcolor,"Color 1",
|
||||
"option navbarfontcolor optionlabel",navbarfontcolor,"Other ...",
|
||||
"option navbarfontcolorcustom (Header)",navbarfontcolorcustom,"Custom font color for the header menu (for 'Other ...')",
|
||||
"option headerbackgroundcolor (Header)",headerbackgroundcolor,"Header background color",
|
||||
"option headerbackgroundcolorcustom (Header)",headerbackgroundcolorcustom,"Custom header background color (for 'Other ...')",
|
||||
"option headersticky (Header)",headersticky,"Make header sticky",
|
||||
"option headerstickycomputer (Header)",headerstickycomputer,"Computer screen",
|
||||
"option headerstickytablet (Header)",headerstickytablet,Tablet,
|
||||
"option headerstickymobile (Header)",headerstickymobile,"Mobile phone",
|
||||
"option headerheight (Header)",headerheight,"Header bar height, in px",
|
||||
"option brandlogofile (Header)",brandlogofile,"Logo file (left)",
|
||||
"option brandlogotopmargin (Header)",brandlogotopmargin,"Margin between logo and top of screen (left), in px (0-100)",
|
||||
"option centerlogofile (Header)",centerlogofile,"Logo file (center)",
|
||||
"option centerlogotopmargin (Header)",centerlogotopmargin,"Margin between logo and top of screen (center), in px (0-100)",
|
||||
"option rightlogofile (Header)",rightlogofile,"Logo file (right)",
|
||||
"option rightlogotopmargin (Header)",rightlogotopmargin,"Margin between logo and top of screen (right), in px (0-100)",
|
||||
"option progressbarposition (Header)",progressbarposition,"Position of the progress bar",
|
||||
"option progressbarposition optionlabel",progressbarposition,"In header",
|
||||
"option progressbarposition optionlabel",progressbarposition,"Under header",
|
||||
"option progressbarfont (Header)",progressbarfont,"Font for the progress bar",
|
||||
"option progressbarcolor (Header)",progressbarcolor,"Progress bar color",
|
||||
"option progressbarcolorcustom (Header)",progressbarcolorcustom,"Custom progress bar color (for 'Other ...')",
|
||||
"option progressbarbackgroundcolor (Header)",progressbarbackgroundcolor,"Progress bar background color",
|
||||
"option progressbarbackgroundcolorcustom (Header)",progressbarbackgroundcolorcustom,"Custom progress bar background color (for 'Other ...')",
|
||||
"option progressbarfontcolor (Header)",progressbarfontcolor,"Font color for the progress bar",
|
||||
"option progressbarfontcolorcustom (Header)",progressbarfontcolorcustom,"Custom font color for the progress bar (for 'Other ...')",
|
||||
"option bodybackgroundcolor (Page background)",bodybackgroundcolor,"Overall background color",
|
||||
"option bodybackgroundcolorcustom (Page background)",bodybackgroundcolorcustom,"Custom overall background color (for 'Other ...')",
|
||||
"option backgroundimage (Page background)",backgroundimage,"Background image (behind body of page)",
|
||||
"option backgroundimagefile (Page background)",backgroundimagefile,"Background image file",
|
||||
"option backgroundimagefixed (Page background)",backgroundimagefixed,"Background image fixed or scroll",
|
||||
"option backgroundimagefixed optionlabel",backgroundimagefixed,Fixed,
|
||||
"option backgroundimagefixed optionlabel",backgroundimagefixed,Scroll,
|
||||
"option backgroundimagedisplay (Page background)",backgroundimagedisplay,"Background image display",
|
||||
"option backgroundimagedisplay optionlabel",backgroundimagedisplay,Tile,
|
||||
"option backgroundimagedisplay optionlabel",backgroundimagedisplay,Cover,
|
||||
"option privacybackgroundcolor (Page background)",privacybackgroundcolor,"Privacy background color",
|
||||
"option grouptitlefont (Groups)",grouptitlefont,"Font for group title",
|
||||
"option grouptitletextcolor (Groups)",grouptitletextcolor,"Font color for group titles",
|
||||
"option grouptitletextcolorcustom (Groups)",grouptitletextcolorcustom,"Custom font color for group titles (for 'Other ...')",
|
||||
"option grouptitlefontsize (Groups)",grouptitlefontsize,"Group title font size",
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,"Same as Base Font Size(BFS)",
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,"1.2 BFS",
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,"1.5 BFS",
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,"2.0 BFS",
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,20px,
|
||||
"option grouptitlefontsize optionlabel",grouptitlefontsize,24px,
|
||||
"option grouptitlefontoptions (Groups)",grouptitlefontoptions,"Group title font options",
|
||||
"option grouptitlefontoptions optionlabel",grouptitlefontoptions,Normal,
|
||||
"option grouptitlefontoptions optionlabel",grouptitlefontoptions,Bold,
|
||||
"option grouptitlefontoptions optionlabel",grouptitlefontoptions,Italic,
|
||||
"option grouptitlefontoptions optionlabel",grouptitlefontoptions,"Bold italic",
|
||||
"option groupdescriptionfont (Groups)",groupdescriptionfont,"Font for group description",
|
||||
"option groupdescriptiontextcolor (Groups)",groupdescriptiontextcolor,"Font color for group descriptions",
|
||||
"option groupdescriptiontextcolorcustom (Groups)",groupdescriptiontextcolorcustom,"Custom font color for group descriptions (for 'Other ...')",
|
||||
"option groupdescriptionfontsize (Groups)",groupdescriptionfontsize,"Group description font size",
|
||||
"option groupdescriptionfontoptions (Groups)",groupdescriptionfontoptions,"Group description font options",
|
||||
"option questiondistance (Questions)",questiondistance,"Distance between two questions (px or rem)",
|
||||
"option questiondistancebottom (Questions)",questiondistancebottom,"Distance between last question and buttons at bottom of page (px or rem)",
|
||||
"option questiontitlefont (Questions)",questiontitlefont,"Font for question titles",
|
||||
"option questiontitletextcolor (Questions)",questiontitletextcolor,"Font color for question titles",
|
||||
"option questiontitletextcolorcustom (Questions)",questiontitletextcolorcustom,"Custom font color for question titles (for 'Other ...')",
|
||||
"option questiontitlefontsize (Questions)",questiontitlefontsize,"Question title font size",
|
||||
"option questiontitlefontoptions (Questions)",questiontitlefontoptions,"Question title font options",
|
||||
"option questiontitlefontcolor (Questions)",questiontitlefontcolor,"Font color for question titles (legacy picker)",
|
||||
"option questiontitlebackgroundcolor (Questions)",questiontitlebackgroundcolor,"Question title background color",
|
||||
"option questiontitlebackgroundcolorcustom (Questions)",questiontitlebackgroundcolorcustom,"Custom question title background color (for 'Other ...')",
|
||||
"option questionbackgroundcolor (Questions)",questionbackgroundcolor,"Question body background color",
|
||||
"option questionbackgroundcolorcustom (Questions)",questionbackgroundcolorcustom,"Custom question body background color (for 'Other ...')",
|
||||
"option answersfont (Questions)",answersfont,"Font for answers and subquestions",
|
||||
"option answerstextcolor (Questions)",answerstextcolor,"Font color for answers and subquestions",
|
||||
"option answerstextcolorcustom (Questions)",answerstextcolorcustom,"Custom font color for answers and subquestions (for 'Other ...')",
|
||||
"option checkboxcolor (Questions)",checkboxcolor,"Checkbox/radio button color (when checked)",
|
||||
"option checkboxcolorcustom (Questions)",checkboxcolorcustom,"Custom checkbox/radiobutton color (for 'Other ...')",
|
||||
"option sliderhandlecolor (Questions)",sliderhandlecolor,"Slider handle color",
|
||||
"option sliderhandlecolorcustom (Questions)",sliderhandlecolorcustom,"Custom slider handle color (for 'Other ...')",
|
||||
"option questionhelpfont (Questions)",questionhelpfont,"Font for question help text",
|
||||
"option questionhelpfontcolor (Questions)",questionhelpfontcolor,"Font color for question help text",
|
||||
"option questionhelpfontcolorcustom (Questions)",questionhelpfontcolorcustom,"Custom font color for question help text (for 'Other ...')",
|
||||
"option questionhelpbackgroundcolor (Questions)",questionhelpbackgroundcolor,"Question help background color",
|
||||
"option questionhelpbackgroundcolorcustom (Questions)",questionhelpbackgroundcolorcustom,"Custom question help background color (for 'Other ...')",
|
||||
"option questionhelptextposition (Questions)",questionhelptextposition,"Question help text position",
|
||||
"option questionhelptextposition optionlabel",questionhelptextposition,Top,
|
||||
"option questionhelptextposition optionlabel",questionhelptextposition,Bottom,
|
||||
"option questionborder (Questions)",questionborder,"Question border",
|
||||
"option questionborder optionlabel",questionborder,"Box around",
|
||||
"option questionborder optionlabel",questionborder,"Top and bottom",
|
||||
"option questionborder optionlabel",questionborder,None,
|
||||
"option questionbordercolor (Questions)",questionbordercolor,"Question border color",
|
||||
"option questionbordercolorcustom (Questions)",questionbordercolorcustom,"Custom question border color (for 'Other ...')",
|
||||
"option questionborderstyle (Questions)",questionborderstyle,"Question border line style",
|
||||
"option questionborderstyle optionlabel",questionborderstyle,Solid,
|
||||
"option questionborderstyle optionlabel",questionborderstyle,Dotted,
|
||||
"option questionborderstyle optionlabel",questionborderstyle,Dashed,
|
||||
"option questiondropdown (Questions)",questiondropdown,"Question dropdown shadow",
|
||||
"option questiondropdown optionlabel",questiondropdown,Always,
|
||||
"option questiondropdown optionlabel",questiondropdown,"On hover only",
|
||||
"option questionborderradiusTL (Questions)",questionborderradiusTL,"Border radius top left",
|
||||
"option questionborderradiusTR (Questions)",questionborderradiusTR,"Border radius top right",
|
||||
"option questionborderradiusBL (Questions)",questionborderradiusBL,"Border radius bottom left",
|
||||
"option questionborderradiusBR (Questions)",questionborderradiusBR,"Border radius bottom right",
|
||||
"option buttonsfont (Buttons)",buttonsfont,"Font for the buttons (Previous/Next/Submit)",
|
||||
"option previousbuttonbackgroundcolor (Buttons)",previousbuttonbackgroundcolor,"Previous button background color",
|
||||
"option previousbuttonbackgroundcolor optionlabel",previousbuttonbackgroundcolor,"Default button color",
|
||||
"option previousbuttonbackgroundcolorcustom (Buttons)",previousbuttonbackgroundcolorcustom,"Custom previous button background color (for 'Other ...')",
|
||||
"option previousbuttontextcolor (Buttons)",previousbuttontextcolor,"Font color for the Previous button",
|
||||
"option previousbuttontextcolorcustom (Buttons)",previousbuttontextcolorcustom,"Custom font color for the Previous button (for 'Other ...')",
|
||||
"option nextbuttonbackgroundcolor (Buttons)",nextbuttonbackgroundcolor,"Next button background color",
|
||||
"option nextbuttonbackgroundcolorcustom (Buttons)",nextbuttonbackgroundcolorcustom,"Custom next button background color (for 'Other ...')",
|
||||
"option nextbuttontextcolor (Buttons)",nextbuttontextcolor,"Font color for the Next button",
|
||||
"option nextbuttontextcolorcustom (Buttons)",nextbuttontextcolorcustom,"Custom font color for the Next button (for 'Other ...')",
|
||||
"option submitbuttonbackgroundcolor (Buttons)",submitbuttonbackgroundcolor,"Submit button background color",
|
||||
"option submitbuttonbackgroundcolorcustom (Buttons)",submitbuttonbackgroundcolorcustom,"Custom submit button background color (for 'Other ...')",
|
||||
"option submitbuttontextcolor (Buttons)",submitbuttontextcolor,"Font color for the Submit button",
|
||||
"option submitbuttontextcolorcustom (Buttons)",submitbuttontextcolorcustom,"Custom font color for the Submit button (for 'Other ...')",
|
||||
"option hideprivacyinfo (Other options)",hideprivacyinfo,"Hide privacy info",
|
||||
"option container (Other options)",container,"Survey container",
|
||||
"option showpopups (Other options)",showpopups,"Show popups",
|
||||
"option showpopups optionlabel",showpopups,Popup,
|
||||
"option showpopups optionlabel",showpopups,"On page",
|
||||
"option showclearall (Other options)",showclearall,"Show Clear All button",
|
||||
"option fixnumauto (Other options)",fixnumauto,"Fix automatically numeric value",
|
||||
"option fixnumauto optionlabel",fixnumauto,"For expression",
|
||||
|
194
translations/sync_labels.php
Normal file
194
translations/sync_labels.php
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
<?php
|
||||
/**
|
||||
* Extract every translatable label of this theme from config.xml into a
|
||||
* spreadsheet-friendly CSV (translations/labels.csv) and keep that CSV up to
|
||||
* date during development.
|
||||
*
|
||||
* What is extracted (all of it is shown to the admin through gT() on the
|
||||
* custom options page, so the English source string is the translation key):
|
||||
* - every <options>/<key> title="..." (context "option <key>")
|
||||
* - every distinct category="..." name (context "category")
|
||||
* - every optionlabels="A|B|..." entry (context "option <key> optionlabel")
|
||||
* - every dropdown <optgroup label="..."> (context "option <key> optgroup")
|
||||
* - every dropdown <option> label text (context "option <key> dropdown option")
|
||||
*
|
||||
* Usage:
|
||||
* php translations/sync_labels.php update labels.csv (merge, preserve translations)
|
||||
* php translations/sync_labels.php --check exit 1 when labels.csv is stale (for dev/CI checks)
|
||||
*
|
||||
* CSV layout: context,key,source,<language columns...>
|
||||
* The language columns are whatever the header row has after "source"
|
||||
* (default: "nl"). Add a language by adding a column in a spreadsheet editor;
|
||||
* existing translations are never overwritten, new strings are appended with
|
||||
* empty translation cells, strings that disappeared from config.xml are removed
|
||||
* (and reported). Rows are keyed by the source string, because gT() translates
|
||||
* by message text.
|
||||
*
|
||||
* After editing translations in the CSV, generate the SQL for the LimeSurvey
|
||||
* message tables with:
|
||||
* php translations/csv_to_sql.php translations/labels.csv nl > nl.sql
|
||||
* and run that SQL in the installation's database (see README "Multiple
|
||||
* languages in the theme options (i18n)").
|
||||
*/
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$here = __DIR__;
|
||||
$themeDir = dirname($here);
|
||||
$configFile = $themeDir . DIRECTORY_SEPARATOR . 'config.xml';
|
||||
$csvFile = $here . DIRECTORY_SEPARATOR . 'labels.csv';
|
||||
$check = in_array('--check', $argv, true);
|
||||
|
||||
if (!is_file($configFile)) {
|
||||
fwrite(STDERR, "config.xml not found: $configFile\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$doc = new DOMDocument();
|
||||
if (!$doc->load($configFile)) {
|
||||
fwrite(STDERR, "Could not parse $configFile\n");
|
||||
exit(1);
|
||||
}
|
||||
$xpath = new DOMXPath($doc);
|
||||
|
||||
$strings = []; // source => [context, key] (first occurrence wins)
|
||||
$add = function (string $context, string $key, string $source) use (&$strings) {
|
||||
$source = trim($source);
|
||||
if ($source === '' || isset($strings[$source])) {
|
||||
return;
|
||||
}
|
||||
$strings[$source] = [$context, $key];
|
||||
};
|
||||
|
||||
// categories
|
||||
foreach ($xpath->query('//options/*[@category]') as $opt) {
|
||||
$add('category', '(category name)', (string) $opt->getAttribute('category'));
|
||||
}
|
||||
// option titles + optionlabels + dropdown labels
|
||||
foreach ($xpath->query('//options/*') as $opt) {
|
||||
$key = $opt->nodeName;
|
||||
$cat = $opt->getAttribute('category');
|
||||
$ctx = $cat !== '' ? "option $key ($cat)" : "option $key";
|
||||
if ($opt->hasAttribute('title')) {
|
||||
$add($ctx, $key, (string) $opt->getAttribute('title'));
|
||||
}
|
||||
if ($opt->hasAttribute('optionlabels') && $opt->hasAttribute('options')) {
|
||||
$labels = explode('|', (string) $opt->getAttribute('optionlabels'));
|
||||
foreach ($labels as $label) {
|
||||
$add("option $key optionlabel", $key, $label);
|
||||
}
|
||||
}
|
||||
foreach ($xpath->query('./dropdownoptions/optgroup', $opt) as $og) {
|
||||
if ($og->hasAttribute('label')) {
|
||||
$add("option $key optgroup", $key, (string) $og->getAttribute('label'));
|
||||
}
|
||||
foreach ($xpath->query('./option', $og) as $o) {
|
||||
$add("option $key dropdown option", $key, (string) $o->textContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- read existing CSV (if any) ---
|
||||
$langs = ['nl']; // default language column
|
||||
$rows = []; // source => [context, key, [lang => translation]]
|
||||
if (is_file($csvFile)) {
|
||||
$fh = fopen($csvFile, 'r');
|
||||
$header = fgetcsv($fh);
|
||||
if ($header === false || count($header) < 3 || $header[0] !== 'context' || $header[1] !== 'key' || $header[2] !== 'source') {
|
||||
fwrite(STDERR, "Existing $csvFile has an unexpected header (expected context,key,source,...). Fix or remove it.\n");
|
||||
exit(1);
|
||||
}
|
||||
$langs = array_slice($header, 3);
|
||||
if (empty($langs)) {
|
||||
$langs = ['nl'];
|
||||
}
|
||||
while (($r = fgetcsv($fh)) !== false) {
|
||||
if (count($r) < 3 || $r[2] === '') {
|
||||
continue;
|
||||
}
|
||||
$trans = [];
|
||||
foreach ($langs as $i => $lang) {
|
||||
$trans[$lang] = $r[3 + $i] ?? '';
|
||||
}
|
||||
$rows[$r[2]] = [$r[0], $r[1], $trans];
|
||||
}
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
// --- merge: keep translations, append new strings, drop stale ones ---
|
||||
$order = array_keys($rows); // preserve existing order
|
||||
$new = 0;
|
||||
foreach ($strings as $source => [$context, $key]) {
|
||||
if (!isset($rows[$source])) {
|
||||
$trans = array_fill_keys($langs, '');
|
||||
$rows[$source] = [$context, $key, $trans];
|
||||
$order[] = $source;
|
||||
$new++;
|
||||
} else {
|
||||
// keep context/key up to date with config.xml
|
||||
$rows[$source][0] = $context;
|
||||
$rows[$source][1] = $key;
|
||||
// a language column added later gets an empty cell
|
||||
foreach ($langs as $lang) {
|
||||
if (!array_key_exists($lang, $rows[$source][2])) {
|
||||
$rows[$source][2][$lang] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$removed = 0;
|
||||
foreach (array_keys($rows) as $source) {
|
||||
if (!isset($strings[$source])) {
|
||||
unset($rows[$source]);
|
||||
$order = array_values(array_diff($order, [$source]));
|
||||
$removed++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- serialize ---
|
||||
$out = fopen('php://temp', 'r+');
|
||||
fputcsv($out, array_merge(['context', 'key', 'source'], $langs));
|
||||
foreach ($order as $source) {
|
||||
[$context, $key, $trans] = $rows[$source];
|
||||
fputcsv($out, array_merge([$context, $key, $source], array_map(fn($l) => $trans[$l] ?? '', $langs)));
|
||||
}
|
||||
rewind($out);
|
||||
$csv = stream_get_contents($out);
|
||||
fclose($out);
|
||||
// normalize to LF line endings
|
||||
$csv = str_replace("\r\n", "\n", $csv);
|
||||
|
||||
if ($check) {
|
||||
$current = is_file($csvFile) ? str_replace("\r\n", "\n", file_get_contents($csvFile)) : '';
|
||||
if ($current !== $csv) {
|
||||
fwrite(STDERR, "STALE: $csvFile is not in sync with config.xml — run: php translations/sync_labels.php\n");
|
||||
fwrite(STDERR, " ($new new string(s), $removed removed string(s), " . count($strings) . " strings total)\n");
|
||||
exit(1);
|
||||
}
|
||||
echo "labels.csv in sync with config.xml (" . count($strings) . " strings, languages: " . implode(', ', $langs) . ")\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
file_put_contents($csvFile, $csv);
|
||||
|
||||
$filled = [];
|
||||
foreach ($langs as $lang) {
|
||||
$n = 0;
|
||||
foreach ($rows as $r) {
|
||||
if (($r[2][$lang] ?? '') !== '') {
|
||||
$n++;
|
||||
}
|
||||
}
|
||||
$filled[$lang] = $n;
|
||||
}
|
||||
echo "Wrote $csvFile: " . count($strings) . " strings, languages: " . implode(', ', $langs) . "\n";
|
||||
echo " new strings appended (empty translations): $new\n";
|
||||
echo " strings removed from config.xml: $removed\n";
|
||||
foreach ($filled as $lang => $n) {
|
||||
echo " translated [$lang]: $n / " . count($strings) . "\n";
|
||||
}
|
||||
echo "Next: edit the translations in a spreadsheet editor (CSV), then generate SQL with:\n";
|
||||
echo " php translations/csv_to_sql.php " . basename($csvFile) . " nl > nl.sql\n";
|
||||
Loading…
Reference in a new issue