Skip to content

Translations Upload Developer Guide

Ed Mozley edited this page Dec 5, 2026 · 1 revision

Translations - Developer Guide

(After 5.19.0, #329.) User side: Translations. Design and decisions: Translations-Upload-Design.

The one idea: an administrator fills a language's gaps without a development environment - download, translate anywhere, upload - and nothing is ever written into the application. Uploads live in the database (the master copy); pages read a plain-JSON cache file; lang/*.php is never touched, so it works on read-only and Docker installs, survives upgrades, and a bug here can never write code.

πŸ“ Files

File What it does
βš™οΈ includes/lang_overrides.php Coverage, Download zip, Upload (check + store), Remove, Export, the cache (langOverridesCacheDir(), langOverridesRebuild()), the log
βš™οΈ includes/i18n_check.php The pipeline's checks, shared - i18nVerifyRows(), i18nParseTsv(), flatten / escape / placeholders / HTML. Moved here from scripts/
βš™οΈ includes/i18n.php I18n::loadNamespace() lays loadUploaded() over the shipped file
πŸ”Œ api/system/translations.php GET coverage / download / export, POST upload (multipart) / remove. admin_api_guard.php
πŸ–₯️ system/translations/index.php, assets/js/translations.js The page. Rebuilds every locale's cache when it opens
πŸ—„οΈ lang_overrides (locale, namespace, string_key, value, updated_by_id) - UNIQUE(locale, namespace, string_key) The master copy
❓ system/help/translations.php In-app help
πŸ§ͺ system.translations (Feature Bingo) Β· tests/translations-upload.php (16)

1. One copy of the checks

The upload has to check a line exactly as scripts/i18n_verify_chunk.php does - the gate that makes it safe to merge languages nobody here reads. But every file in scripts/ starts with a CLI-only guard (if (PHP_SAPI !== 'cli') { http_response_code(404); exit; }), so a web page cannot include them. The pure parts moved, unchanged, into includes/i18n_check.php (no files, no database, no output, no guard), and scripts/i18n_lib.php now requires it:

if (PHP_SAPI !== 'cli') { http_response_code(404); exit; }
// The pure checks (flatten, TSV escape and parse, placeholders, HTML, i18nVerifyRows) live in
// includes/ so the web upload (System -> Translations, GH #329) shares this one copy.
require_once __DIR__ . '/../includes/i18n_check.php';

i18nVerifyChunk() (files) became a thin wrapper that reads both TSVs and calls i18nVerifyRows($en, $tr, $errors) (rows). php scripts/i18n_verify_chunk.php --self-test still passes 21 of 21, and a gated merge runs as before.

⚠️ TRAP: nothing in includes/i18n_check.php may read or write a file - it is reachable from web code. Keep I/O in scripts/.

2. Upload: line by line, so one mistake loses one line

langOverridesImportTsv() checks each line on its own against its English, so a lost placeholder in one string does not throw away the other 400:

    foreach ($tr as $k => $v) {
        if (!array_key_exists($k, $en)) { $failed[] = ['key' => $k, 'error' => 'There is no such string in this part of FreeITSM.']; continue; }
        $r = i18nVerifyRows([$k => $en[$k]], [$k => $v]);
        if (!$r['ok']) { $failed[] = ['key' => $k, 'error' => preg_replace('/^' . preg_quote($k, '/') . ': /', '', $r['errors'][0])]; continue; }
        if ($v === $en[$k] && trim($v) !== '') { $same++; continue; }   // still English: nothing to store
        $ok[$k] = $v;
    }

A line still in English is not stored (it would only hide a later real translation). The namespace comes from the file name (<locale>__<namespace>.tsv as downloaded; anything after the namespace is ignored, so de__tickets.de.tsv works); a file naming another locale is refused. A zip is read with limits (500 entries, 5 MB each, 20 MB per upload).

3. What the pages read - never the database

Ed asked whether this would cost a database read on every page; it does not. After every upload and remove, langOverridesRebuild() writes the locale's strings to uploads/lang/<locale>/<namespace>.json (nested like a lang file; written to a temp name and renamed, so never half-written; an .htaccess denies the folder to the web). The loader lays it over the shipped file:

            // GH #329: strings uploaded under System -> Translations, laid on top.
            if ($locale !== self::FALLBACK_LOCALE && ($up = self::loadUploaded($namespace, $locale))) {
                self::$cache[$locale][$namespace] = self::deepMerge(self::$cache[$locale][$namespace], $up);
            }

loadUploaded() checks one folder per locale per request (cached in a static) and only then the namespace's file; a locale with no uploads looks no further, and English never looks. JSON, never PHP: the cache is data the loader decodes, so nothing uploaded can ever run.

When uploads/ cannot be written the cache goes to the server's temp folder (freeitsm-lang-<hash of the install path>), read the same way. A temp folder emptied by a restart is filled again whenever System β†’ Translations opens (langOverridesRebuildAll()), and after every change.

Why not the design's "one indexed query per namespace" fallback: the language loader runs on every page before anything opens a database connection, and checking the database whether to query would itself cost a query on every page of every install - including the ones that never upload anything. The temp-folder cache keeps the "no database per page" promise everywhere.

4. Download and Export

  • Download (langOverridesDownloadZip()): the keys a locale is missing - English keys in neither the shipped file nor the uploads - one TSV per namespace in English order, plus a README.txt: the rules (the checker's, in plain words) and a glossary of up to 60 short common terms the language already uses (langOverridesGlossary()).
  • Export (langOverridesExportZip()): every uploaded string as chunk pairs - <locale>__<ns>__upload.en.tsv and .<locale>.tsv - exactly what php scripts/i18n_gated_merge.php <locale> <folder> takes, so a contribution goes through the same gate as every other translation.

5. Precedence and lifecycle

  • An uploaded string wins over the shipped one - including one a later release translates. Coverage counts a key the shipped file now has as translated, not uploaded.
  • lang_overrides.updated_by_id is registered in includes/analyst_lifecycle.php as history.
  • Every upload and remove: one system_logs row, log_type = 'translations'.

Tests

tests/translations-upload.php (16), run from the install's own folder - see Developer-Tests-System.

Related

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally