Skip to content

Commit

Permalink
feat: add translation example
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikbehncke committed Aug 20, 2024
1 parent 5b638a8 commit 202e707
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/CreateModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$settings = $ask(new ConfirmationQuestion('Add settings example? [y/N] ', false));
$less = $ask(new ConfirmationQuestion('Add custom LESS example? [y/N] ', false));
$source = $ask(new ConfirmationQuestion('Add custom source example? [y/N] ', false));
$translator = $ask(
new ConfirmationQuestion('Add translation files example? [y/N] ', false),
);

$finders = [
'module' => (new Finder())
Expand Down Expand Up @@ -90,6 +93,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->in("{$this->stubs}/module");
}

if ($translator) {
$finders['translator'] = (new Finder())
->name(['TranslationListener.php', 'en_GB.json'])
->in("{$this->stubs}/module");
}

foreach ($finders as $name => $finder) {
foreach ($finder->files() as $file) {
$fs->dumpFile("{$path}/{$file->getRelativePathname()}", $file->getContents());
Expand Down Expand Up @@ -205,6 +214,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if ($translator) {
// add TranslationListener
$find = ['#// includes#', '#// add event handlers ...#'];
$replace = [
"\${0}\ninclude_once __DIR__ . '/src/TranslationListener.php';",
"\${0}\n\n 'customizer.init' => [
TranslationListener::class => ['initCustomizer', -10],
],",
];

$this->replaceInFile("{$path}/bootstrap.php", $find, $replace);

// namespace
$this->replaceInFile(
"{$path}/src/TranslationListener.php",
['#// namespace#'],
["namespace {$namespace};"],
);
}

$output->writeln('Module created successfully.');

return Command::SUCCESS;
Expand Down
6 changes: 6 additions & 0 deletions src/stubs/module/languages/de_DE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Title": "Titel",
"Content": "Inhalt",
"My Section": "Mein Abschnitt",
"A description text.": "Ein Beschreibungstext."
}
15 changes: 15 additions & 0 deletions src/stubs/module/src/TranslationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// namespace

use YOOtheme\Config;
use YOOtheme\Path;
use YOOtheme\Translator;

class TranslationListener
{
static function initCustomizer(Config $config, Translator $translator)
{
$translator->addResource(Path::get("../languages/{$config('locale.code')}.json"));
}
}

0 comments on commit 202e707

Please sign in to comment.