最佳实践 Symfony 翻译占位符命名文本 vs. key

Best practice Symfony translation placeholder naming text vs. key

我们正在通过使用翻译组件提取文本并另外使用 php-translation/symfony-bundle

来翻译现有的 Symfony4 应用程序

我们不确定是使用现有文本作为键还是插入占位符更好。

使用文本的优点是:

缺点是:

您和您的团队如何处理这个问题? 翻译文件是您存储库的一部分吗?

link 建议使用键: https://medium.com/@smaine.milianni/straightforward-symfony-4-best-practices-e6d1b3c0a9dd

According to Symfony:

Always use keys for translations instead of content strings.

Using keys simplifies the management of the translation files because you can change the original contents without having to update all of the translation files.

Keys should always describe their purpose and not their location. For example, if a form has a field with the label Username, then a nice key would be label.username, not edit_form.label.username.

使用键而不是文本通常会避免在内容中设置键时出错,并显示更小的内容。

例子

<div>
    <span>{% trans %}Please enter the value of this input in order for the calculation to work{% endtrans %}</span>
    <label>{% trans %}Simple Input{% endtrans %}</label>
    <input type="text"/>
</div>

在这种情况下,如果您在其他地方写 {% trans %}Simple input{% endtrans %},那么即使是相同的,您也必须管理另一个翻译。

当然,我们可能会争辩说您想要进行大写/小写转换,但是如果使用 twig 为例,您可以使用一些帮助函数来处理 lower, upper and capitalize

VS

<div>
    <span>{% trans %}help_text.simple_input{% endtrans %}</span>
    <label>{% trans %}label.simple_input{% endtrans %}</label>
    <input type="text"/>
</div>