From ec9061579d77a11acc61cdd2436b037e6af6f857 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 13 Feb 2018 12:20:45 +0100 Subject: [translation] Add support for global translation plugins Until now, modules could only supply extensions for translating module-related additional dictionaries. Now a module can provide a 'translation-global.inc.php' that will add additional translation sections to any module. --- modules-available/translation/page.inc.php | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'modules-available/translation/page.inc.php') diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php index a5cdebed..e41d4786 100644 --- a/modules-available/translation/page.inc.php +++ b/modules-available/translation/page.inc.php @@ -53,11 +53,30 @@ class Page_Translation extends Page private function loadCustomHandler($moduleName) { $path = 'modules/' . $moduleName . '/hooks/translation.inc.php'; - if (!file_exists($path)) - return false; $HANDLER = array(); - require $path; - return $HANDLER; + if (file_exists($path)) { + require $path; + } + $backup = $HANDLER; + foreach (glob('modules/*/hooks/translation-global.inc.php', GLOB_NOSORT) as $path) { + $HANDLER = array(); + require $path; + if (empty($HANDLER['subsections'])) + continue; + foreach ($HANDLER['subsections'] as $sub) { + $suf = ''; + while (isset($backup['subsections']) && in_array($sub . $suf, $backup['subsections'])) { + $suf = mt_rand(); + } + $backup['subsections'][] = $sub . $suf; + if (isset($HANDLER['grep_' . $sub])) { + $backup['grep_' . $sub . $suf] = $HANDLER['grep_' . $sub]; + } + } + } + if (empty($backup)) + return false; + return $backup; } /** @@ -81,7 +100,7 @@ class Page_Translation extends Page { User::load(); - if (!User::hasPermission('superadmin')) { + if (!User::isLoggedIn()) { Message::addError('main.no-permission'); Util::redirect('?do=Main'); } -- cgit v1.2.3-55-g7522 From cbf344a26ed449040bb6287810bc87d5f6911e4c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 13 Feb 2018 15:44:09 +0100 Subject: [translation] Fix saving language tags containing dots --- modules-available/translation/page.inc.php | 25 +++++++++-------------- modules-available/translation/templates/edit.html | 2 +- 2 files changed, 11 insertions(+), 16 deletions(-) (limited to 'modules-available/translation/page.inc.php') diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php index e41d4786..df7bc15e 100644 --- a/modules-available/translation/page.inc.php +++ b/modules-available/translation/page.inc.php @@ -997,24 +997,19 @@ class Page_Translation extends Page $data = array(); //find the tag requests to change the file - foreach ($_POST as $key => $value) { - $str = explode('#!#', $key, 2); - if (count($str) !== 2) + $tags = Request::post('langtag', array(), 'array'); + foreach ($tags as $tag => $value) { + $tag = trim($tag); + if (empty($tag)) { + Message::addWarning('i18n-empty-tag'); continue; - if ($str[0] === 'lang') { - $tag = trim($str[1]); - if (empty($tag)) { - Message::addWarning('i18n-empty-tag'); - continue; - } - if (empty($value)) { - unset($data[$tag]); - } else { - $data[$tag] = $value; - } + } + if (empty($value)) { + unset($data[$tag]); + } else { + $data[$tag] = $value; } } - $translation = Request::post('new-text', array(), 'array'); foreach (Request::post('new-id', array(), 'array') as $k => $tag) { if (empty($translation[$k]) || empty($tag)) diff --git a/modules-available/translation/templates/edit.html b/modules-available/translation/templates/edit.html index 7a41d9b4..15e69f3b 100644 --- a/modules-available/translation/templates/edit.html +++ b/modules-available/translation/templates/edit.html @@ -35,7 +35,7 @@ {{^big}} - + {{/big}} {{#big}} -- cgit v1.2.3-55-g7522 From f43d7611f7b35d8052c329ecbc944e9370b600de Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 21 Mar 2018 11:21:43 +0100 Subject: [translation] Fix saving of multiline texts --- modules-available/translation/page.inc.php | 1 + modules-available/translation/templates/edit.html | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'modules-available/translation/page.inc.php') diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php index df7bc15e..bd5d440e 100644 --- a/modules-available/translation/page.inc.php +++ b/modules-available/translation/page.inc.php @@ -999,6 +999,7 @@ class Page_Translation extends Page //find the tag requests to change the file $tags = Request::post('langtag', array(), 'array'); foreach ($tags as $tag => $value) { + error_log($tag . '=' . $value); $tag = trim($tag); if (empty($tag)) { Message::addWarning('i18n-empty-tag'); diff --git a/modules-available/translation/templates/edit.html b/modules-available/translation/templates/edit.html index 15e69f3b..5b957067 100644 --- a/modules-available/translation/templates/edit.html +++ b/modules-available/translation/templates/edit.html @@ -38,7 +38,7 @@ {{/big}} {{#big}} - + {{/big}} @@ -81,7 +81,7 @@ var slxNewTagCounter = 0; function slxAddTag() { - $('#moduleTable tr:last').after( + $('#moduleTable').find('tr:last').after( '' + ' ' + ' ' + -- cgit v1.2.3-55-g7522 From 41ac58d665ac9b4342f485db6932b4320084e4fe Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 21 Mar 2018 11:47:27 +0100 Subject: [translation] Fix tag collision for messages --- modules-available/translation/page.inc.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'modules-available/translation/page.inc.php') diff --git a/modules-available/translation/page.inc.php b/modules-available/translation/page.inc.php index bd5d440e..7d5229d7 100644 --- a/modules-available/translation/page.inc.php +++ b/modules-available/translation/page.inc.php @@ -464,19 +464,21 @@ class Page_Translation extends Page $allFiles = array_merge($allFiles, $this->getAllFiles('apis', '.php'), $this->getAllFiles('inc', '.php')); $allFiles[] = 'index.php'; } - $tags = $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?[^\'"\.]*)\.(?[^\'"]*)[\'"]\s*(?\)|\,.*)/i', + $full = $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?[^\'"\.]*\.[^\'"]*)[\'"]\s*(?\)|\,.*)/i', $allFiles); + $tags = []; // Filter out tags that don't refer to this module - foreach (array_keys($tags) as $tag) { + foreach ($full as $tag) { + $p = explode('.', $tag['tag'], 2); // Figure out if this is a message from this module or not - if ($tags[$tag]['module'] === $module->getIdentifier()) { + if ($p[0] === $module->getIdentifier()) { // Direct reference to this module via module.id - continue; + $tag['tag'] = $p[1]; + $tags[$p[1]] = $tag; } - unset($tags[$tag]); } - $tags += $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?[^\'"\.]*)[\'"]\s*(?\)|\,.*)/i', - $this->getModulePhpFiles($module)); + $tags = $this->loadTagsFromPhp('/Message\s*::\s*add\w+\s*\(\s*[\'"](?[^\'"\.]*)[\'"]\s*(?\)|\,.*)/i', + $this->getModulePhpFiles($module), $tags); return $tags; } @@ -831,12 +833,12 @@ class Page_Translation extends Page * * @param string $regexp regular expression * @param array $files list of files to scan + * @param array $tags existing tag array to append to * @return array of all tags found, where the tag is the key, and the value is as described above */ - private function loadTagsFromPhp($regexp, $files) + private function loadTagsFromPhp($regexp, $files, $tags = []) { // Get all php files, so we can find all strings that need to be translated - $tags = array(); // Now find all tags in all php files. Only works for literal usage, not something like $foo = 'bar'; Dictionary::translate($foo); foreach ($files as $file) { $content = file_get_contents($file); -- cgit v1.2.3-55-g7522