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 <[email protected]> |
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
|
|
|
} |