12

言語ファイル ( ) のキーを使用して現在のハードコードされたテキストを翻訳するにはどうすればよいmessages.en.xliffですか?

を使ってみました

{% trans %} translation_key{% endtrans %}

成功しませんでした。Symfony はこのエラーを返します

メッセージは、'ProjectEventsBundle:Default:show_event.html.twig' 内の単純なテキストでなければなりません

500 内部サーバー エラー - Twig_Error_Syntax

{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}

前もって感謝します。

4

5 に答える 5

20

次のようなソリューションを使用します。

messages.en.xliff:

<trans-unit id="1">
    <source>some.translation.key</source>
    <target>{0} no.attendee|{1} one attendee|{2} two attendees|{3} three attendees|]3,Inf] many attendees</target>
</trans-unit>

小枝テンプレート:

{{ 'some.translation.key'|transchoice(count) }}

いくつかの引数を入れる必要がある場合は、それらを 2 番目の引数として渡す必要があります。

フィルターのプロトタイプは次のとおりです。

public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)
于 2012-05-30T13:39:19.953 に答える
11

Symfony Documentationからこれを見つけました:

Symfony2 は、テキストの静的ブロックのメッセージ翻訳を支援するために、特殊な Twig タグ (trans および transchoice) を提供します。

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

{% transchoice count %}

{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples

{% endtranschoice %}

transchoice タグは、現在のコンテキストから %count% 変数を自動的に取得し、それを翻訳者に渡します。このメカニズムは、%var% パターンに従ってプレースホルダーを使用する場合にのみ機能します。

于 2012-04-03T20:16:08.870 に答える
10

この主題はかなり古いですが、次のようなことをお勧めします。

あなたのmessages.LOCALE.ymlで

you.translaction.key: "{1}1 Comment|]1,Inf]%count% Comments"

あなたの小枝テンプレートで

{% set count = 2 %}

{% transchoice count with {'%count%': count} %}you.translaction.key{% endtranschoice %}

乾杯、

サイモン

于 2015-07-13T07:56:25.363 に答える
-18

解決策を見つけました。少し汚れていますが動作しています。より良い方法を見つけたら、忘れずに投稿してください。

    {% set noattendee %}{% trans %} no.attendee {% endtrans %}{% endset %}
    {% set oneattendee %}{% trans %} one.attendee {% endtrans %}{% endset %}
    {% set twoattendees %}{% trans %} two.attendees {% endtrans %}{% endset %}
    {% set treeattendees %}{% trans with {'%people%': people} %} tree.attendees {% endtrans %}{% endset %}
    {% set manyattendees %}{% trans with {'%people%': people} %} many.attendees {% endtrans %}{% endset %}

    {% transchoice count with {
        '%noattendee%': noattendee,
        '%oneattendee%': oneattendee,
        '%twoattendees%': twoattendees,
        '%treeattendees%': treeattendees,
        '%manyattendees%': manyattendees}
    %}
        {0}  %noattendee%|{1}  %oneattendee%|{2} %twoattendees%|{3} %treeattendees%|]3,Inf] %manyattendees%
    {% endtranschoice %}
于 2011-09-13T06:42:07.890 に答える