75 lines
3.3 KiB
PHP
75 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Baut aus einer JSON-Datei {"Deutscher Text": "English text", …} die Übersetzungsdateien (.po und .mo) des Plugins.
|
|
* php wordpress-plugin/tools/build-mo.php wordpress-plugin/tools/en.json
|
|
* Quellsprache des Plugins ist Deutsch, deshalb gibt es "Übersetzungen" nach Englisch (en_US, en_GB, …).
|
|
* Es wird kein gettext gebraucht: Das .mo-Format wird hier selbst geschrieben.
|
|
*/
|
|
if ($argc < 2) { fwrite(STDERR, "Aufruf: php build-mo.php <en.json>\n"); exit(1); }
|
|
$map = json_decode((string) file_get_contents($argv[1]), true);
|
|
if (!is_array($map)) { fwrite(STDERR, "Ungültiges JSON.\n"); exit(1); }
|
|
$missing = array_keys(array_filter($map, fn($v) => !is_string($v) || trim($v) === ''));
|
|
if ($missing) { fwrite(STDERR, count($missing) . " Texte ohne Übersetzung, z. B.: " . $missing[0] . "\n"); exit(1); }
|
|
foreach ($map as $de => $en) {
|
|
// Platzhalter (%s, %d) müssen übereinstimmen
|
|
preg_match_all('/%(?:\d+\$)?[sd]/', $de, $a);
|
|
preg_match_all('/%(?:\d+\$)?[sd]/', $en, $b);
|
|
if ($a[0] !== $b[0]) { fwrite(STDERR, "Platzhalter ungleich: $de\n"); exit(1); }
|
|
}
|
|
|
|
$dir = dirname(__DIR__) . '/deskhub-tickets/languages/';
|
|
@mkdir($dir, 0777, true);
|
|
|
|
function po_quote(string $s): string
|
|
{
|
|
return '"' . str_replace(["\\", "\"", "\n", "\t"], ['\\\\', '\\"', '\\n', '\\t'], $s) . '"';
|
|
}
|
|
|
|
function build_po(array $map, string $locale): string
|
|
{
|
|
$out = "# DeskHub Tickets: English translation (source language is German)\n"
|
|
. "msgid \"\"\nmsgstr \"\"\n"
|
|
. "\"Project-Id-Version: DeskHub Tickets 1.0.0\\n\"\n"
|
|
. "\"Language: $locale\\n\"\n"
|
|
. "\"MIME-Version: 1.0\\n\"\n"
|
|
. "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
|
|
. "\"Content-Transfer-Encoding: 8bit\\n\"\n"
|
|
. "\"Plural-Forms: nplurals=2; plural=(n != 1);\\n\"\n\n";
|
|
foreach ($map as $de => $en) {
|
|
$out .= 'msgid ' . po_quote($de) . "\nmsgstr " . po_quote($en) . "\n\n";
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
function build_mo(array $map, string $locale): string
|
|
{
|
|
$entries = ['' => "Project-Id-Version: DeskHub Tickets 1.0.0\nLanguage: $locale\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\nPlural-Forms: nplurals=2; plural=(n != 1);\n"] + $map;
|
|
ksort($entries, SORT_STRING);
|
|
$n = count($entries);
|
|
$ids = '';
|
|
$strs = '';
|
|
$idTable = [];
|
|
$strTable = [];
|
|
foreach ($entries as $id => $str) {
|
|
$idTable[] = [strlen($id), strlen($ids)];
|
|
$ids .= $id . "\0";
|
|
$strTable[] = [strlen($str), strlen($strs)];
|
|
$strs .= $str . "\0";
|
|
}
|
|
$headerSize = 28;
|
|
$idTableOffset = $headerSize;
|
|
$strTableOffset = $idTableOffset + $n * 8;
|
|
$idsOffset = $strTableOffset + $n * 8;
|
|
$strsOffset = $idsOffset + strlen($ids);
|
|
$mo = pack('V*', 0x950412de, 0, $n, $idTableOffset, $strTableOffset, 0, 0);
|
|
foreach ($idTable as [$len, $off]) $mo .= pack('VV', $len, $idsOffset + $off);
|
|
foreach ($strTable as [$len, $off]) $mo .= pack('VV', $len, $strsOffset + $off);
|
|
return $mo . $ids . $strs;
|
|
}
|
|
|
|
foreach (['en_US', 'en_GB', 'en_AU', 'en_CA', 'en_NZ', 'en_ZA'] as $locale) {
|
|
file_put_contents($dir . "deskhub-tickets-$locale.po", build_po($map, $locale));
|
|
file_put_contents($dir . "deskhub-tickets-$locale.mo", build_mo($map, $locale));
|
|
}
|
|
echo count($map) . " Texte, 6 Sprachvarianten in " . $dir . "\n";
|