File Metadata

Translate alt text, captions, and other file metadata for every file under a page – straight from the command line.

Walks every file under a page's children and runs the configured strategy over each translatable field.

Prerequisites

Install the Kirby CLI and create a site/commands folder if it does not exist yet.

Command

site/commands/translate-files.php
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();
        $titles = array_map('strval', $siteChildren->pluck('title'));
        $input = $cli->radio(
            'Which page\'s files metadata should be translated?',
            $titles
        );
        $response = $input->prompt();
        $cli->success('Selected parent page: ' . $response);

        $page = $siteChildren->findBy('title', $response);
        if ($page === null) {
            $cli->error('Page "' . $response . '" not found.');
            return;
        }

        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');
    }
];

Usage

# Translate all files metadata of children pages to German
kirby translate-files de