Zend framework2 zf2使用变量转换验证器消息

Zend framework2 zf2使用变量转换验证器消息,zend-framework2,Zend Framework2,我有如下自定义消息: '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",

我有如下自定义消息:

'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",
    ),
),
$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"
        ),
    ),
),

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

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

在您的案例中,msgid“输入不在%s和%s之间”

我希望它能解决您的问题。

您可以阅读如何做到这一点

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

$validator->setDefaultTranslator($translator);
编辑 我误解了你的问题,这里是编辑

在像这样绑定变量之前,不能转换消息:

'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",
    ),
),
$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"
        ),
    ),
),

我以前试过在这里问,没有翻译发生你读过我的最后一句话吗?我将此字符串放入.po文件并生成.mo,但消息不会翻译。对于所有其他没有变量的消息,它工作正常。所以这意味着我阅读文档并这样做。唯一的问题是变量。