-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Translation helper controler * PHPdoc * PHPdoc * Reformat * Changelog #1844
- Loading branch information
1 parent
bb4c5ac
commit 550632b
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters