Skip to content

Commit

Permalink
docs: simplify CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jun 14, 2024
1 parent f2e83a0 commit 0953cb5
Showing 1 changed file with 74 additions and 74 deletions.
148 changes: 74 additions & 74 deletions content/1.docs/2.content-translator/3.php-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,106 +194,106 @@ $translator = $page->translator([

### CLI Commands

You can leverage the [Kirby CLI Prompts](https://github.com/getkirby/cli) to select the parent page and then translate all children of the selected page.
You can leverage the [Kirby CLI](https://github.com/getkirby/cli) to automate the translation process. Below are two examples of CLI commands that you can use to translate content.

For these examples to work, install the Kirby CLI and create a new `site/commands` folder if it doesn't exist yet.

#### Translate Content of a Page

The most common use case is to synchronize the content from the default language to a secondary language and then translate the content to the target language. You can create a CLI command to automate this process.
The most common use case is to copy the content from the default language to a secondary language and then translate the duplicated content to the target language. You can create a CLI command to automate this process.

Create a new PHP file for your command, for example `site/commands/translate-page.php`:

```php
use Kirby\CLI\CLI;
use Kirby\Cms\App;

App::plugin('company/service', [
'commands' => [
'translate:page' => [
'description' => 'Translates the content of a specific page.',
'args' => [
'language' => [
'description' => 'The target language to translate the content to.',
'defaultValue' => 'de'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$defaultLanguage = $kirby->defaultLanguage()->code();
$targetLanguage = $cli->arg('language');

$siteChildren = $kirby->site()->children();
$input = $cli->radio(
'Which page should be translated?',
$siteChildren->pluck('title')
);
$response = $input->prompt();
$cli->success('Selected page: ' . $response);

$page = $siteChildren->findBy('title', $response);
$translator = $page->translator();
$translator->copyContent($targetLanguage, $defaultLanguage);
$translator->translateContent($targetLanguage, $targetLanguage, $defaultLanguage);

$cli->success('Successfully translated ' . $page->id());
}
],
]
]);
return [
'description' => 'Translates the content of a specific page.',
'args' => [
'language' => [
'description' => 'The target language to translate the content to.',
'defaultValue' => 'de'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$defaultLanguage = $kirby->defaultLanguage()->code();
$targetLanguage = $cli->arg('language');

$siteChildren = $kirby->site()->children();
$input = $cli->radio(
'Which page should be translated?',
$siteChildren->pluck('title')
);
$response = $input->prompt();
$cli->success('Selected page: ' . $response);

$page = $siteChildren->findBy('title', $response);
$translator = $page->translator();
$translator->copyContent($targetLanguage, $defaultLanguage);
$translator->translateContent($targetLanguage, $targetLanguage, $defaultLanguage);

$cli->success('Successfully translated ' . $page->id());
}
];
```

Run the command in your terminal:

```bash
# Translate the content of a specific page to German
kirby translate:page de
kirby translate-page de
```

#### Translate All Children of a Page

By leveraging the prompts, you can create a CLI command to translate all children of a specific page to a target language.

Create a new PHP file for your command, for example `site/commands/translate-children.php`:

```php
use Kirby\CLI\CLI;
use Kirby\Cms\App;

App::plugin('company/service', [
'commands' => [
'translate:children' => [
'description' => 'Translates the content of all children of a specific page.',
'args' => [
'language' => [
'description' => 'The target language to translate the content to.',
'defaultValue' => 'de'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$defaultLanguage = $kirby->defaultLanguage()->code();
$targetLanguage = $cli->arg('language');

$siteChildren = $kirby->site()->children();
$input = $cli->radio(
'Which page\'s children should be translated?',
$siteChildren->pluck('title')
);
$response = $input->prompt();
$cli->success('Selected parent page: ' . $response);

$page = $siteChildren->findBy('title', $response);

foreach ($page->children()->listed() as $child) {
$translator = $child->translator();
$translator->copyContent($targetLanguage, $defaultLanguage);
$translator->translateContent($targetLanguage, $targetLanguage, $defaultLanguage);
$cli->out('Translated ' . $child->id());
}

$cli->success('Successfully translated all ' . $page->id() . ' children');
}
],
]
]);
return [
'description' => 'Translates the content of all children of a specific page.',
'args' => [
'language' => [
'description' => 'The target language to translate the content to.',
'defaultValue' => 'de'
]
],
'command' => static function (CLI $cli): void {
$kirby = $cli->kirby();
$defaultLanguage = $kirby->defaultLanguage()->code();
$targetLanguage = $cli->arg('language');

$siteChildren = $kirby->site()->children();
$input = $cli->radio(
'Which page\'s children should be translated?',
$siteChildren->pluck('title')
);
$response = $input->prompt();
$cli->success('Selected parent page: ' . $response);

$page = $siteChildren->findBy('title', $response);

foreach ($page->children()->listed() as $child) {
$translator = $child->translator();
$translator->copyContent($targetLanguage, $defaultLanguage);
$translator->translateContent($targetLanguage, $targetLanguage, $defaultLanguage);
$cli->out('Translated ' . $child->id());
}

$cli->success('Successfully translated all ' . $page->id() . ' children');
}
];
```

Run the command in your terminal:

```bash
# Translate all children of a selected page to German
kirby translate:children de
kirby translate-children de
```

0 comments on commit 0953cb5

Please sign in to comment.