Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation Helper for dev #1844

Merged
merged 6 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version should be 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();