CallableStrategy
Wrap a closure as a translation backend – the simplest extension point and the migration path off the old translateFn option.
Wrap a closure as a Strategy. The closure runs once per text – no batching, no TranslationMode awareness. Reach for a custom Strategy implementation when batching matters.
This is what wraps the deprecated
translateFn option behind the scenes. New code should set strategy => $closure directly, or implement Strategy for batching support.Construction
public function __construct(Closure $translate)
translate
Closure(string $text, string $target, ?string $source): string
Translation function. Returns the translated text. The same signature as the legacy
translateFn.Usage
config.php
return [
'johannschopplich.content-translator' => [
'strategy' => function (string $text, string $target, ?string $source): string {
return myTranslateFunction($text, $target, $source);
},
],
];
The closure is auto-wrapped in CallableStrategy.
use JohannSchopplich\ContentTranslator\Translation\Strategies\CallableStrategy;
return [
'johannschopplich.content-translator' => [
'strategy' => new CallableStrategy(
fn (string $text, string $target, ?string $source) => /* … */,
),
],
];
When to Use Something Else
CallableStrategy is fine for low-volume sites and translateFn migration. For batching, mode-aware dispatch, or structured exceptions, see Implementing a Custom Strategy.