DeepLStrategy
Translate via DeepL with automatic batching and retries – the default strategy when nothing else is configured.
Selected automatically when neither strategy nor the deprecated translateFn is configured. Inject a custom DeepL client to override language mapping or stub it out in tests.
Behavior
Units are sent via DeepL::translateMany() (auto-chunked at 50 texts per request). When the upstream request fails, every unit triggers content-translator.translate:warning and a TranslationException is thrown.
Selected via
'strategy' => 'deepl'. Also the last-resort default – chosen whether or not DeepL.apiKey is set, with the AuthException for a missing key raised lazily on the first execute() call.Construction
public function __construct(DeepL|null $deepL = null)
deepL
DeepL | null
Inject a custom
DeepL client – useful in tests or when you want non-singleton instances. Defaults to DeepL::instance().Usage
config.php
return [
'johannschopplich.content-translator' => [
'strategy' => 'deepl',
'DeepL' => [
'apiKey' => env('DEEPL_API_KEY'),
],
],
];
use JohannSchopplich\ContentTranslator\Translation\Strategies\DeepLStrategy;
use JohannSchopplich\ContentTranslator\Translator;
Translator::translateText('Hello', 'de', 'en', new DeepLStrategy());
config.php
use JohannSchopplich\ContentTranslator\DeepL;
use JohannSchopplich\ContentTranslator\Translation\Strategies\DeepLStrategy;
return [
'ready' => fn () => [
'johannschopplich.content-translator' => [
'strategy' => new DeepLStrategy(new DeepL()),
],
],
];
Build the client inside Kirby's
ready callback, not in the returned array directly. new DeepL() reads DeepL.apiKey in its constructor, and at the top level of config.php the options are still being assembled – the constructor throws AuthException: Missing DeepL API key even though the key sits right next to it. ready runs once the app is booted and the plugin is autoloaded, so both are in place. The same applies to any Strategy instance built in the config.