-
Notifications
You must be signed in to change notification settings - Fork 49
/
translations.html.twig
129 lines (94 loc) · 4.95 KB
/
translations.html.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<p>Translations are handled by a Translator service that uses the user's locale to lookup and return translated messages. Before using it, enable the Translator in your configuration:</p>
{% raw %}<pre><code># app/config/config.yml
framework:
translator: { fallback: en }
default_locale: en
</code></pre>{% endraw %}
<p><strong>Basic translation</strong></p>
{% raw %}<pre><code>$t = $this->get('translator')->trans('Symfony2 is great');
$t = $this->get('translator')->trans('Hello %name%', array('%name%' => $name));
</code></pre>{% endraw %}
<p>When this code is executed, Symfony2 will attempt to translate the message "Symfony2 is great" based on the locale of the user.</p>
{% raw %}<pre><code><!-- messages.fr.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>Symfony2 is great</source>
<target>J'aime Symfony2</target>
</trans-unit>
<trans-unit id="2">
<source>Hello %name%</source>
<target>Bonjour %name%</target>
</trans-unit>
</body>
</file>
</xliff>
</code></pre>{% endraw %}
<p>Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resource:</p>
<p><strong>Using Real or Keyword Messages</strong></p>
{% raw %}<pre><code>$t = $translator->trans('Symfony2 is great');
$t = $translator->trans('symfony2.great');
</code></pre>{% endraw %}
<p>In the first method, messages are written in the language of the default locale (English in this case). That message is then used as the "id" when creating translations.
In the second method, messages are actually "keywords" that convey the idea of the message. The keyword message is then used as the "id" for any translations. In this case, translations must be made for the default locale (i.e. to translate symfony2.great to Symfony2 is great).</p>
{% raw %}<pre><code>symfony2.is.great: Symfony2 is great
symfony2.is.amazing: Symfony2 is amazing
symfony2.has.bundles: Symfony2 has bundles
user.login: Login
</code></pre>{% endraw %}
<p><strong>Using Message Domains</strong></p>
<p>When translating strings that are not in the default domain (messages), you must specify the domain as the third argument of trans():</p>
{% raw %}<pre><code>* messages.fr.xliff
* admin.fr.xliff
* navigation.fr.xliff
$this->get('translator')->trans('Symfony2 is great', array(), 'admin');
</code></pre>{% endraw %}
<p><strong>Pluralization</strong></p>
<p>To translate pluralized messages, use the transChoice() method:</p>
{% raw %}<pre><code>$t = $this->get('translator')->transChoice(
'There is one apple|There are %count% apples',
10,
array('%count%' => 10)
);
</code></pre>{% endraw %}
<h3>Translations in Templates</h3>
<p>Translating in Twig templates example:</p>
{% raw %}<pre><code>//you can set de translation domain for entire twig temples
{% trans_default_domain "app" %}
{% trans %}Hello %name%{% endtrans %}
{% transchoice count %}
{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
{% endtranschoice %}
//variables traduction
{{ message|trans }}
{{ message|transchoice(5) }}
{{ message|trans({'%name%': 'Fabien'}, "app") }}
{{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
</code></pre>{% endraw %}
<p>If you need to use the percent character (%) in a string, escape it by doubling it: {% raw %}{% trans %}Percent: %percent%%%{% endtrans %}{% endraw %}</p>
<h3>Translating Database Content</h3>
<p>The translation of database content should be handled by Doctrine through the
<a href="https://github.com/l3pp4rd/DoctrineExtensions/blob/master/README.md">Translatable Extension</a></p>
<p><strong>Translating constraint messages</strong></p>
{% raw %}<pre><code># src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
properties:
name:
- NotBlank: { message: "author.name.not_blank" }
</code></pre>{% endraw %}
<p>Create a translation file under the validators catalog:</p>
{% raw %}<pre><code><!-- validators.en.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>author.name.not_blank</source>
<target>Please enter an author name.</target>
</trans-unit>
</body>
</file>
</xliff>
</code></pre>{% endraw %}