2016-12-27 122 views
0

在我的Twig項目中,我使用.yaml文件翻譯。我需要翻譯的文本可以在.html.twig文件中找到,例如,Twig翻譯一個字符串,其中包含參數

「我的翻譯文本包含要翻譯的{{參數}}。」

我知道我可以代替這整個字符串的關鍵詞,例如 - to_translate%參數%to_translate2,我可以使用翻譯從我.yaml文件中像這樣

to_translate:「我的文本翻譯包含「 to_translate2:」翻譯「

並且該參數將被傳遞。但是,如何在不破壞這麼多部分的句子的情況下做到這一點?

回答

2

也許我沒有明白你的觀點,但你可以根據需要添加任意數量的參數?

YAML文件:

my_translation_key: Hello %firstname%, %lastname%, welcome here ! 

在枝杈:

{{ 'my_translation_key' | trans({ 
     '%firstname%': 'John', 
     '%lastname%': 'Doe' 
    }) }} 

如果你想要的是讓嵌套塊在您的翻譯,你也許可以嘗試這樣的事:

YAML文件:

my_translation_key: Hello %firstname%, %lastname%, %welcome% ! 
welcome_block: welcome %where% 

枝條:

{% set welcome = 'welcome_block' | trans({'%where%': 'here'}) %} 
{{ 'my_translation_key' | trans({ 
     '%firstname%': 'John', 
     '%lastname%': 'Doe', 
     '%welcome%': welcome 
    }) }} 
+1

謝謝!問題是我使用獨立的Twig,也許一些symfony部件不起作用。因此,現在我使用{%trans與{'%var%':var}%}翻譯{%endtrans%}並在我翻譯的.yaml文件中傳遞參數:我的句子%var%Thanks !!! – Dimentica

+0

不客氣,很高興我能幫助你! –

1

也許你應該試試這個:

{{ ('My text for translation contains a '~parameter)|trans }} 

Documentation

〜:所有的操作數轉換爲字符串,並連接它們。 {{「Hello」〜name>〜「!」 }}會返回(假設名字是'John')Hello John !.