Translator
Build a Translator for any page, file, or site to copy and translate content from PHP. Mutate the model in place, then read the updated state back via model().
use JohannSchopplich\ContentTranslator\Translator;
$translator = new Translator(page('blog/article'));
$translator->copyContent('de', 'en');
$translator->translateContent('de', 'de', 'en');
$page = $translator->model();
Site, Page, and File: $page->translator(). The translator() global helper mirrors the constructor.Construction
public function __construct(Site|Page|File $model, array $options = [])
fieldTypes, includeFields, excludeFields, kirbyTags.$translator = translator(page('blog/article'), [
'fieldTypes' => ['text', 'textarea', 'blocks'],
'excludeFields' => ['date', 'author'],
]);
Instance Methods
copyContent
Copy a source language's content over to a target language. Overwrites the target.
public function copyContent(string $toLanguageCode, string $fromLanguageCode): void
$translator->copyContent('de', 'en'); // Copy English → German
translateContent
Translate the content already stored in $contentLanguageCode and save it back. The fourth parameter overrides the configured strategy for this call only.
public function translateContent(
string $contentLanguageCode,
string $toLanguageCode,
string|null $fromLanguageCode = null,
Strategy|null $strategy = null,
): void
Typical pattern – copy first, then translate the duplicated content:
$translator->copyContent('de', 'en');
$translator->translateContent('de', 'de', 'en');
TranslationException when the strategy fails for every collected unit. See Exceptions.translateTitle
public function translateTitle(
string $contentLanguageCode,
string $toLanguageCode,
string|null $fromLanguageCode = null,
): void
Translate the model title. Falls back to the source language when the target-language title is empty.
$translator->translateTitle('de', 'de', 'en');
translateSlug
public function translateSlug(
string $contentLanguageCode,
string $toLanguageCode,
string|null $fromLanguageCode = null,
): void
Rename the page slug from the translated title. No-op for the homepage, the error page, and non-page models.
$translator->translateSlug('de', 'de', 'en');
model
public function model(): Site|Page|File
Mutating methods don't return the new model. Reach for model() to grab the up-to-date instance:
$translator->translateContent('de', 'de');
// $page still holds the pre-translation model
echo $page->content()->get('text');
// Reassign to read the translation
$page = $translator->model();
echo $page->content()->get('text');
Static Helpers
Translate raw strings without a model. Both methods return the source unchanged for empty strings, pure numbers, and standalone URLs.
public static function translateText(
string $text,
string $targetLanguage,
string|null $sourceLanguage = null,
Strategy|null $strategy = null,
): string
public static function translateTexts(
array $texts,
string $targetLanguage,
string|null $sourceLanguage = null,
Strategy|null $strategy = null,
): array
Prefer translateTexts() over a loop – DeepL packs up to 50 texts per request, and the AI strategy chunks intelligently.
echo Translator::translateText('Hello world', 'de', 'en');
// "Hallo Welt"
$translated = Translator::translateTexts(['Hello', 'Goodbye'], 'de', 'en');
// ['Hallo', 'Auf Wiedersehen']
The optional $strategy parameter on translateText, translateTexts, and translateContent overrides the configured strategy for one call:
use JohannSchopplich\ContentTranslator\Translation\Strategies\CopilotAIStrategy;
Translator::translateText('Hello', 'de', 'en', new CopilotAIStrategy());
See Strategies for the full interface and built-in implementations.