Files in Kirby can have translatable metadata like titles, alt text, captions, and descriptions. This command translates file metadata for all files within a page's children.
Install the Kirby CLI and create a site/commands folder if it does not exist yet.
use Kirby\CLI\CLI;
return [
'description' => 'Translates the files metadata of listed children pages.',
'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 files metadata 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) {
foreach ($child->files() as $file) {
$translator = $file->translator();
$translator->copyContent($targetLanguage, $defaultLanguage);
$translator->translateContent($targetLanguage, $targetLanguage, $defaultLanguage);
$cli->out('Translated ' . $file->id() . ' metadata');
}
}
$cli->success('Successfully translated all ' . $page->id() . ' files');
}
];
# Translate all files metadata of children pages to German
kirby translate-files de