zf2 翻译带有变量的验证器消息

zf2 translate validators messages with variables

我有这样的自定义消息:

'options' => array(
    'min' => $min,
    'max' => $date->format('Y-m-d'),
    'inclusive' => true,
    'messages' => array(
        Between::NOT_BETWEEN => "The input is not between '%min%' and '%max%', inclusively",
    ),
),

我将此字符串放入 .po 文件并生成 .mo,但无法翻译消息。对于没有变量的所有其他消息,它工作正常。

如果你能给出更多的解释就好了。 通常如果字符串包含变量,则必须用字符串格式化程序替换它们。

在你的情况下 msgid "The input is not between %s and %s"

希望它能解决您的问题。

您可以阅读 in the ZF2 official documentation 了解如何执行此操作。

验证器的翻译文件位于 /resources/languages。您只需使用这些资源文件或您自己的自定义消息文件将翻译器附加到 Zend\Validator\AbstractValidator

$validator->setDefaultTranslator($translator);

编辑

我误解了你的问题,这里编辑一下

你能不能在绑定变量之前不翻译消息:

$translator = ...translator...

'options' => array(
    'min' => $min,
    'max' => $date->format('Y-m-d'),
    'inclusive' => true,
    'messages' => array(
        Between::NOT_BETWEEN => $translator->translate(
            "The input is not between '%min%' and '%max%', inclusively"
        ),
    ),
),