Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Symfony 3将字符串复数化_Symfony_Bundle_Translation_Pluralize - Fatal编程技术网

用Symfony 3将字符串复数化

用Symfony 3将字符串复数化,symfony,bundle,translation,pluralize,Symfony,Bundle,Translation,Pluralize,在Symfony 3中,有没有办法将字符串复数化?例如:我有“对象”,我想得到“对象”。多元化是一个非常复杂的主题,Symfony将其视为以下情况的一部分: 消息多元化是一个棘手的话题,因为规则可能相当复杂 因此,您基本上需要激活系统的翻译,并使用翻译服务的transChoice()方法或细枝模板中的“transChoice”标记/过滤器翻译所需字符串 要处理此问题,请使用模板中的transChoice()方法或transChoice标记/过滤器 使用翻译服务 // the %count% pl

在Symfony 3中,有没有办法将字符串复数化?例如:我有“对象”,我想得到“对象”。

多元化是一个非常复杂的主题,Symfony将其视为以下情况的一部分:

消息多元化是一个棘手的话题,因为规则可能相当复杂

因此,您基本上需要激活系统的翻译,并使用翻译服务的
transChoice()
方法或细枝模板中的“transChoice”标记/过滤器翻译所需字符串

要处理此问题,请使用模板中的transChoice()方法或transChoice标记/过滤器

使用翻译服务

// the %count% placeholder is assigned to the second argument...
$translator->transChoice(
    'There is one apple|There are %count% apples',
    10
);


// ...but you can define more placeholders if needed
$translator->transChoice(
    'Hurry up %name%! There is one apple left.|There are %count% apples left.',
    10,
    // no need to include %count% here; Symfony does that for you
    array('%name%' => $user->getName())
);
树枝标签

{% trans %}Hello %name%{% endtrans %}

{% transchoice count %}
    {0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples
{% endtranschoice %}
更新

如果您事先不知道字符串,您可以使用internal,但请注意免责声明以及这仅适用于英语字符串的事实:

此组件当前标记为内部。不要在自己的代码中使用它。在Symfony的下一个小版本中可能会引入突破性的更改,或者组件本身甚至可能被完全删除


另一种方法是创建您自己的拐点类,例如,并从中创建服务。

我事先不知道字符串,因此无法使用该字符串。您可以尝试,尽管它被标记为仅供内部使用;-)我昨晚找到了解决办法。如您所说,拐点组件仅标记为内部使用。然而,有一些流行的bundle,比如FOSRestBundle,正在使用它。我还看到了教条的拐点。这个对我来说似乎有点先进。谢谢你的回答。