Skip to content

Commit e412c05

Browse files
boehsermoenadar
authored andcommitted
Translation Helper for dev (#1844)
* Translation helper controler * PHPdoc * PHPdoc * Reformat * Changelog #1844
1 parent b327e02 commit e412c05

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

core/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec
66

77
+ [#1840](https://github.com/luyadev/luya/issues/1840) Convert mail message into alt body automatically.
88
+ [#1816](https://github.com/luyadev/luya/issues/1816) View mapping to change the view folder of actions inside modules.
9+
+ [#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.
910

1011
## 1.0.10 (18. July 2018)
1112

dev/RepoController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Dev Env cloning and updating.
1717
*
18-
* Provdes functions to clone and update the repos.
18+
* Provides functions to clone and update the repos.
1919
*
2020
* Usage
2121
*

dev/TranslationController.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace luya\dev;
4+
5+
/**
6+
* Dev tool for translators. This is only a helper tool for developer to edit the many translation files in different repositories.
7+
*
8+
* Provides functions to add new translations.
9+
*
10+
* Usage
11+
*
12+
* ```sh
13+
* ./vendor/bin/luyadev translation/add luya-module-cms cmsadmin
14+
* ```
15+
*
16+
* @author Bennet Klarhölter <boehsermoe@me.com>
17+
* @since 1.0.11
18+
*/
19+
class TranslationController extends BaseDevCommand
20+
{
21+
/**
22+
* @var bool Outputs the operations but will not execute anything.
23+
*/
24+
public $dry = false;
25+
26+
public function options($actionId)
27+
{
28+
return array_merge(['dry'], parent::options($actionId));
29+
}
30+
31+
/**
32+
* Add a new translation to a repository by filename (for admin and frondend).
33+
*
34+
* @param string $repo Name of the repo directory (e.g. luya-module-cms)
35+
* @param string $filename Name of the php file without suffix (e.g. cmsadmin)
36+
* @param string $language (Optional) Add the translation only to one language. Use shortcode e.g. en, de, ...
37+
*/
38+
public function actionAdd($repo, $filename, $language = "*")
39+
{
40+
$repoPath = "repos/$repo";
41+
$messageFiles = glob("$repoPath/src/**/messages/$language/$filename.php") ?: glob("$repoPath/src/messages/$language/$filename.php");
42+
43+
$this->outputInfo('Following files will be affected:');
44+
$this->output(implode("\n", $messageFiles) . "\n");
45+
46+
$key = $this->prompt('Insert translation key:');
47+
$text = $this->prompt('Insert translation text:');
48+
49+
foreach ($messageFiles as $messageFile) {
50+
$content = file_get_contents($messageFile);
51+
$newContent = preg_replace("/(\];)/", "\t'$key' => '$text',\n$1", $content);
52+
53+
if (!$this->dry) {
54+
file_put_contents($messageFile, $newContent);
55+
} else {
56+
$this->outputInfo($messageFile);
57+
}
58+
}
59+
60+
if (!$this->dry) {
61+
if (exec("[ -d $repoPath/.git ] && command -v git")) {
62+
$diffCommand = "git --git-dir=$repoPath/.git --work-tree=$repoPath diff -- " . str_replace($repoPath . '/', '', implode(" ", $messageFiles));
63+
exec($diffCommand, $diff);
64+
$this->output(implode("\n", $diff));
65+
}
66+
67+
$this->outputSuccess("Translations added. Review the changes before you commit them!");
68+
}
69+
}
70+
}

dev/luyadev

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $boot->setConfigArray([
2727
'defaultRoute' => 'repo',
2828
'controllerMap' => [
2929
'repo' => 'luya\dev\RepoController',
30+
'translation' => 'luya\dev\TranslationController',
3031
],
3132
]);
3233
$boot->applicationConsole();

0 commit comments

Comments
 (0)