Skip to content

Commit

Permalink
Translation Helper for dev (#1844)
Browse files Browse the repository at this point in the history
* Translation helper controler

* PHPdoc

* PHPdoc

* Reformat

* Changelog #1844
  • Loading branch information
boehsermoe authored and nadar committed Aug 27, 2019
1 parent bb4c5ac commit 550632b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion dev/RepoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
70 changes: 70 additions & 0 deletions dev/TranslationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace luya\dev;

/**
* Dev tool for translators. This is only a helper tool for developer to edit the many translation files in different repositories.
*
* Provides functions to add new translations.
*
* Usage
*
* ```sh
* ./vendor/bin/luyadev translation/add luya-module-cms cmsadmin
* ```
*
* @author Bennet Klarhölter <[email protected]>
* @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!");
}
}
}
1 change: 1 addition & 0 deletions dev/luyadev
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $boot->setConfigArray([
'defaultRoute' => 'repo',
'controllerMap' => [
'repo' => 'luya\dev\RepoController',
'translation' => 'luya\dev\TranslationController',
],
]);
$boot->applicationConsole();

0 comments on commit 550632b

Please sign in to comment.