From 4b05a55da273e068aa48f02af61c5a5c0f4da2d9 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 29 Jul 2016 17:17:34 +0200 Subject: Add ugly scripts to transform translation files to new format --- tools/README | 23 +++++ tools/convert-modules.php | 226 ++++++++++++++++++++++++++++++++++++++++++++++ tools/move-lang-files.sh | 34 +++++++ 3 files changed, 283 insertions(+) create mode 100644 tools/README create mode 100644 tools/convert-modules.php create mode 100755 tools/move-lang-files.sh (limited to 'tools') diff --git a/tools/README b/tools/README new file mode 100644 index 00000000..969fd5b3 --- /dev/null +++ b/tools/README @@ -0,0 +1,23 @@ +The tools in this directory must be run when the current working directory +is the slx-admin root. + +Warning: These scripts are very quick and dirty, so make sure to ONLY run +them if your working set is clean, so you can check "git diff" first and +revert easily if something goes wrong. + + +-- move-lang-files.sh +This is supposed to be run first. +Move translation files from ./lang/... to ./modules//lang/... +Pass either "modules" or "templates" as parameter + +-- convert-modules.php +Run on the command line via +"php tools/convert-modules.php" +This is supposed to somewhat intelligently merge and rearrange most +of the remaining translations from the global lang directory to the +respective modules. + + +After running both scripts, double-check the git diff output and the +web interface itself if in doubt. diff --git a/tools/convert-modules.php b/tools/convert-modules.php new file mode 100644 index 00000000..9b479886 --- /dev/null +++ b/tools/convert-modules.php @@ -0,0 +1,226 @@ + 0 && file_put_contents($dir, json_encode($data, JSON_PRETTY_PRINT)); + } + } +} + +foreach (glob('lang/??/modules/*.json', GLOB_NOSORT) as $path) { + if (!preg_match('#lang/(..)/modules/(\w+)\.json#', $path, $out)) continue; + $data = json_decode(file_get_contents($path), true); + if (!is_array($data)) { + echo "Kackfile $path\n"; + continue; + } + echo "Handling $path\n"; + $lang = $out[1]; + $mod = $out[2]; + if (!in_array($mod, $tags)) continue; + @mkdir('modules/' . $mod . '/lang/' . $lang, 0755, true); + $destFile = 'modules/' . $mod . '/lang/' . $lang . '/module.json'; + $dest = json_decode(file_get_contents($destFile), true); + if (!is_array($dest)) { + $dest = array(); + } + foreach (array_keys($data) as $k) { + if (substr($k, 0, 5) !== 'lang_') continue; + if (empty($dest[$k]) && !empty($data[$k])) $dest[$k] = $data[$k]; + unset($data[$k]); + } + ksort($dest); + ksort($data); + file_put_contents($destFile, json_encode($dest, JSON_PRETTY_PRINT)) > 0 && file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT)); +} + +foreach ( + glob('modules/*/lang/??/templates/*.json', GLOB_NOSORT) + + glob('modules/*/lang/??/templates/*/*.json', GLOB_NOSORT) + + glob('modules/*/lang/??/templates/*/*/*.json', GLOB_NOSORT) + as $path) { + if (!preg_match('#modules/([^/]+)/lang/(..)/temp#', $path, $out)) continue; + $module = $out[1]; + $lang = $out[2]; + $old = @json_decode(@file_get_contents($path), true); + if (!is_array($old) || empty($old)) { + unlink($path); + continue; + } + $exFile = "modules/$module/lang/$lang/template-tags.json"; + $existing = @json_decode(@file_get_contents($exFile), true); + if (!is_array($existing)) $existing = array(); + $existing = $existing + $old; + ksort($existing); + if (file_put_contents($exFile, json_encode($existing, JSON_PRETTY_PRINT)) > 0) { + unlink($path); + } +} + +echo "LAST SECTION\n"; + +foreach (glob('modules/*/lang/??/module.json', GLOB_NOSORT) as $path) { + if (!preg_match('#modules/([^/]+)/lang/(..)/module.json#', $path, $out)) continue; + if (!is_array($data)) { + echo "Kackfile $path\n"; + continue; + } + $old = json_decode(file_get_contents($path), true); + if (!is_array($old) || empty($old)) { + echo "Is empty\n"; + unlink($path); + continue; + } + echo "Handling $path\n"; + $module = $out[1]; + $lang = $out[2]; + $exFile = "modules/$module/lang/$lang/template-tags.json"; + $existing = @json_decode(@file_get_contents($exFile), true); + if (!is_array($existing)) $existing = array(); + foreach (array_keys($old) as $k) { + if (substr($k, 0, 5) === 'lang_') { + if (empty($existing[$k])) { + $existing[$k] = $old[$k]; + } + unset($old[$k]); + } + } + ksort($existing); + ksort($old); + if (file_put_contents($exFile, json_encode($existing, JSON_PRETTY_PRINT)) > 0) { + if (empty($old)) { + echo "Old file deleted\n"; + unlink($path); + } else { + echo "Old file shortened\n"; + file_put_contents($path, json_encode($old, JSON_PRETTY_PRINT)); + } + } +} + + +echo "Fixing up messages...\n"; + + function getAllFiles($dir, $extension) + { + $php = array(); + $extLen = -strlen($extension); + foreach (scandir($dir, SCANDIR_SORT_NONE) as $name) { + if ($name === '.' || $name === '..') + continue; + $name = $dir . '/' . $name; + if (substr($name, $extLen) === $extension && is_file($name)) { + $php[] = $name; + } else if (is_dir($name)) { + $php = array_merge($php, getAllFiles($name, $extension)); + } + } + return $php; + } + +$files = array_merge(getAllFiles('modules-available', '.php'), getAllFiles('inc', '.php')); +$files[] = 'index.php'; +$messages = array(); + +foreach ($files as $file) { + $content = file_get_contents($file); + if ($content === false) + continue; + if (preg_match_all('/Message\s*::\s*add\w+\s*\(\s*[\'"](?[^\'"\.]*)[\'"]\s*[\)\,]/i', $content, $out, PREG_SET_ORDER) < 1) + continue; + foreach ($out as $set) { + if (preg_match('#modules-available/([^/]+)/#', $file, $a)) { + $id = $a[1]; + } else { + $id = 'main'; + } + $messages[$set['tag']][$id]++; + } +} + +$langs = array(); +foreach (glob('lang/??/messages.json', GLOB_NOSORT) as $path) { + $lang = substr($path, 5, 2); + if ($lang === '..') continue; + $data = json_decode(file_get_contents($path), true); + if (is_array($data)) { + $langs[$lang] = $data; + echo "Have $lang\n"; + } +} + +echo "Processing\n"; +foreach ($messages as $id => $modules) { + asort($modules, SORT_NUMERIC); + $modules = array_reverse($modules, true); + reset($modules); + list($topModule, $topCount) = each($modules); + reset($modules); + $sum = 0; + foreach ($modules as $c) { + $sum += $c; + } + $fac = $topCount / $sum; + echo "****************** $id\n"; + print_r($modules); + if (count($modules) > 3 || isset($modules['main'])) { + $destMods = array('main'); + } elseif (count($modules) === 1) { + $destMods = array($topModule); + } else { + $destMods = array_keys($modules); + } + foreach ($langs as $lang => &$source) { + if (!isset($source[$id])) + continue; + $del = true; + foreach ($destMods as $destMod) { + $dest = json_decode(file_get_contents('modules/' . $destMod . '/lang/' . $lang . '/messages.json'), true); + if (!is_array($dest)) $dest = array(); + if (!empty($dest[$id])) + continue; + $dest[$id] = $source[$id]; + mkdir('modules/' . $destMod . '/lang/' . $lang, 0775, true); + if (file_put_contents('modules/' . $destMod . '/lang/' . $lang . '/messages.json', json_encode($dest, JSON_PRETTY_PRINT)) < 1) { + $del = false; + } + } + if ($del) { + unset($source[$id]); + } + } + unset($source); +} + +foreach ($langs as $lang => $source) { + file_put_contents('lang/' . $lang . '/messages.json', json_encode($source, JSON_PRETTY_PRINT)); +} + diff --git a/tools/move-lang-files.sh b/tools/move-lang-files.sh new file mode 100755 index 00000000..39d0db51 --- /dev/null +++ b/tools/move-lang-files.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +declare -rg PID=$$ + +perror() { + echo "[ERROR] $*" + [ "$$" != "$PID" ] && kill "$PID" + exit 1 +} + +[ "$2" = "modules" -o "$2" = "templates" ] || perror "Second option must be modules or templates" + +declare -rg TRANS="$1" +[ -z "$TRANS" ] && perror "Usage: $0 modules|templates" +declare -rg DIR="lang/${TRANS}/$2" +[ -d "$DIR" ] || perror "No old modules dir for lang $TRANS" + +for mod in $(ls -1 "$DIR"); do + [ -d "$DIR/$mod" ] || continue + [ -z "$(ls -1 "$DIR/$mod")" ] && continue + DEST="modules/$mod/lang/$TRANS/templates" + echo " ******** $DIR/$mod --> $DEST *********" + mkdir -p "$DEST" || perror "Could not create $DEST" + cp -v -a "$DIR/$mod/"* "$DEST/" || perror "Could not copy" + git rm -r "$DIR/$mod" + git add "$DEST" +done + +echo " -- Categories --" + +if [ -n "$(ls -1 "lang/${TRANS}/settings/")" ]; then + git mv "lang/${TRANS}/settings/"*.json "modules/baseconfig/lang/${TRANS}/" || perror "Could not move settings/categories names" +fi + -- cgit v1.2.3-55-g7522