summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSimon Rettberg2016-07-29 17:17:34 +0200
committerSimon Rettberg2016-07-29 17:17:34 +0200
commit4b05a55da273e068aa48f02af61c5a5c0f4da2d9 (patch)
tree80b02bcfb4b88fa93cc2a3cf9e43a15ad5e09b6d /tools
parent[statistics] Fix minor layout issues (diff)
downloadslx-admin-4b05a55da273e068aa48f02af61c5a5c0f4da2d9.tar.gz
slx-admin-4b05a55da273e068aa48f02af61c5a5c0f4da2d9.tar.xz
slx-admin-4b05a55da273e068aa48f02af61c5a5c0f4da2d9.zip
Add ugly scripts to transform translation files to new format
Diffstat (limited to 'tools')
-rw-r--r--tools/README23
-rw-r--r--tools/convert-modules.php226
-rwxr-xr-xtools/move-lang-files.sh34
3 files changed, 283 insertions, 0 deletions
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/<mod>/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 @@
+<?php
+
+error_reporting(E_ALL);
+
+$tags = array();
+foreach (glob('modules/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
+ if (preg_match('#.*/(\w+)/?$#', $dir, $out)) {
+ $tags[] = $out[1];
+ }
+}
+
+foreach (glob('lang/??/messages-hardcoded.json', GLOB_NOSORT) as $dir) {
+ $data = json_decode(file_get_contents($dir), true);
+ if (!is_array($data)) {
+ echo "Kackfile $dir\n";
+ continue;
+ }
+ echo "Handling $dir\n";
+ $lang = substr($dir, 5, 2);
+ foreach ($tags as $mod) {
+ $tag = 'lang_' . $mod;
+ if (isset($data[$tag])) {
+ @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();
+ }
+ $dest['module_name'] = $data[$tag];
+ unset($data[$tag]);
+ ksort($dest);
+ ksort($data);
+ file_put_contents($destFile, json_encode($dest, JSON_PRETTY_PRINT)) > 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*[\'"](?<tag>[^\'"\.]*)[\'"]\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 <language> 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
+