From e412c056bb21cb38cf2edeefbe685b4041b7ca11 Mon Sep 17 00:00:00 2001 From: boehsermoe Date: Sun, 2 Sep 2018 20:45:00 +0200 Subject: [PATCH] Translation Helper for dev (#1844) * Translation helper controler * PHPdoc * PHPdoc * Reformat * Changelog #1844 --- core/CHANGELOG.md | 1 + dev/RepoController.php | 2 +- dev/TranslationController.php | 70 +++++++++++++++++++++++++++++++++++ dev/luyadev | 1 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 dev/TranslationController.php diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 5ca8473bd..3d04c718b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec + [#1840](https://github.com/luyadev/luya/issues/1840) Convert mail message into alt body automatically. + [#1816](https://github.com/luyadev/luya/issues/1816) View mapping to change the view folder of actions inside modules. ++ [#1844](https://github.com/luyadev/luya/pull/1844) Added translation command to add easier a new record to the translation files. This command is used in the luya-env-dev project in order to develop on the LUYA modules or create your own extensions/modules. ## 1.0.10 (18. July 2018) diff --git a/dev/RepoController.php b/dev/RepoController.php index 7d77ce18f..5ab38957e 100644 --- a/dev/RepoController.php +++ b/dev/RepoController.php @@ -15,7 +15,7 @@ /** * Dev Env cloning and updating. * - * Provdes functions to clone and update the repos. + * Provides functions to clone and update the repos. * * Usage * diff --git a/dev/TranslationController.php b/dev/TranslationController.php new file mode 100644 index 000000000..82c689ee4 --- /dev/null +++ b/dev/TranslationController.php @@ -0,0 +1,70 @@ + + * @since 1.0.11 + */ +class TranslationController extends BaseDevCommand +{ + /** + * @var bool Outputs the operations but will not execute anything. + */ + public $dry = false; + + public function options($actionId) + { + return array_merge(['dry'], parent::options($actionId)); + } + + /** + * Add a new translation to a repository by filename (for admin and frondend). + * + * @param string $repo Name of the repo directory (e.g. luya-module-cms) + * @param string $filename Name of the php file without suffix (e.g. cmsadmin) + * @param string $language (Optional) Add the translation only to one language. Use shortcode e.g. en, de, ... + */ + public function actionAdd($repo, $filename, $language = "*") + { + $repoPath = "repos/$repo"; + $messageFiles = glob("$repoPath/src/**/messages/$language/$filename.php") ?: glob("$repoPath/src/messages/$language/$filename.php"); + + $this->outputInfo('Following files will be affected:'); + $this->output(implode("\n", $messageFiles) . "\n"); + + $key = $this->prompt('Insert translation key:'); + $text = $this->prompt('Insert translation text:'); + + foreach ($messageFiles as $messageFile) { + $content = file_get_contents($messageFile); + $newContent = preg_replace("/(\];)/", "\t'$key' => '$text',\n$1", $content); + + if (!$this->dry) { + file_put_contents($messageFile, $newContent); + } else { + $this->outputInfo($messageFile); + } + } + + if (!$this->dry) { + if (exec("[ -d $repoPath/.git ] && command -v git")) { + $diffCommand = "git --git-dir=$repoPath/.git --work-tree=$repoPath diff -- " . str_replace($repoPath . '/', '', implode(" ", $messageFiles)); + exec($diffCommand, $diff); + $this->output(implode("\n", $diff)); + } + + $this->outputSuccess("Translations added. Review the changes before you commit them!"); + } + } +} \ No newline at end of file diff --git a/dev/luyadev b/dev/luyadev index 5d46d2dae..768fca092 100755 --- a/dev/luyadev +++ b/dev/luyadev @@ -27,6 +27,7 @@ $boot->setConfigArray([ 'defaultRoute' => 'repo', 'controllerMap' => [ 'repo' => 'luya\dev\RepoController', + 'translation' => 'luya\dev\TranslationController', ], ]); $boot->applicationConsole(); \ No newline at end of file