1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.view.helpers.initial.translate">
4 <title>Translate Helper</title>
7 Often web sites are available in several languages. To translate the
8 content of a site you should simply use <link
9 linkend="zend.translate.introduction">Zend_Translate</link> and to
10 integrate <classname>Zend_Translate</classname> within your view you should use
11 the <emphasis>Translate</emphasis> View Helper.
15 In all following examples we are using the simple Array Translation
16 Adapter. Of course you can also use any instance of
17 <classname>Zend_Translate</classname> and also any subclasses of
18 <classname>Zend_Translate_Adapter</classname>. There are several ways to initiate
19 the <emphasis>Translate</emphasis> View Helper:
25 Registered, through a previously registered instance in
26 <classname>Zend_Registry</classname>
32 Afterwards, through the fluent interface
38 Directly, through initiating the class
44 A registered instance of <classname>Zend_Translate</classname> is the preferred
45 usage for this helper. You can also select the locale to be used simply
46 before you add the adapter to the registry.
51 We are speaking of locales instead of languages because a language
52 also may contain a region. For example English is spoken in
53 different dialects. There may be a translation for British and one
54 for American English. Therefore, we say "locale" instead of
59 <example id="zend.view.helpers.initial.translate.registered">
60 <title>Registered instance</title>
63 To use a registered instance just create an instance of
64 <classname>Zend_Translate</classname> or <classname>Zend_Translate_Adapter</classname>
65 and register it within <classname>Zend_Registry</classname> using
66 <classname>Zend_Translate</classname> as its key.
69 <programlisting language="php"><![CDATA[
70 // our example adapter
71 $adapter = new Zend_Translate(
74 'content' => array('simple' => 'einfach'),
78 Zend_Registry::set('Zend_Translate', $adapter);
81 echo $this->translate('simple');
82 // this returns 'einfach'
87 If you are more familiar with the fluent interface, then you can also
88 create an instance within your view and initiate the helper afterwards.
91 <example id="zend.view.helpers.initial.translate.afterwards">
92 <title>Within the view</title>
95 To use the fluent interface, create an instance of
96 <classname>Zend_Translate</classname> or <classname>Zend_Translate_Adapter</classname>,
97 call the helper without a parameter, and call the
98 <methodname>setTranslator()</methodname> method.
101 <programlisting language="php"><![CDATA[
103 $adapter = new Zend_Translate(
105 'adapter' => 'array',
106 'content' => array('simple' => 'einfach'),
110 $this->translate()->setTranslator($adapter)->translate('simple');
111 // this returns 'einfach'
116 If you are using the helper without <classname>Zend_View</classname> then you can
117 also use it directly.
120 <example id="zend.view.helpers.initial.translate.directly">
121 <title>Direct usage</title>
123 <programlisting language="php"><![CDATA[
124 // our example adapter
125 $adapter = new Zend_Translate(
127 'adapter' => 'array',
128 'content' => array('simple' => 'einfach'),
133 // initiate the adapter
134 $translate = new Zend_View_Helper_Translate($adapter);
135 print $translate->translate('simple'); // this returns 'einfach'
139 You would use this way if you are not working with
140 <classname>Zend_View</classname> and need to create translated output.
145 As already seen, the <methodname>translate()</methodname> method is used to return
146 the translation. Just call it with the needed messageid of your
147 translation adapter. But it can also replace parameters within the
148 translation string. Therefore, it accepts variable parameters in two ways:
149 either as a list of parameters, or as an array of parameters. As examples:
152 <example id="zend.view.helpers.initial.translate.parameter">
153 <title>Single parameter</title>
156 To use a single parameter just add it to the method.
159 <programlisting language="php"><![CDATA[
162 $this->translate("Today is %1\$s", $date);
163 // could return 'Heute ist Monday'
169 Keep in mind that if you are using parameters which are also text,
170 you may also need to translate these parameters.
174 <example id="zend.view.helpers.initial.translate.parameterlist">
175 <title>List of parameters</title>
178 Or use a list of parameters and add it to the method.
181 <programlisting language="php"><![CDATA[
186 $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s",
190 // Could return 'Heute ist Monday in April. Aktuelle Zeit: 11:20:55'
194 <example id="zend.view.helpers.initial.translate.parameterarray">
195 <title>Array of parameters</title>
198 Or use an array of parameters and add it to the method.
201 <programlisting language="php"><![CDATA[
203 $date = array("Monday", "April", "11:20:55");
204 $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
205 // Could return 'Heute ist Monday in April. Aktuelle Zeit: 11:20:55'
210 Sometimes it is necessary to change the locale of the translation. This
211 can be done either dynamically per translation or statically for all
212 following translations. And you can use it with both a parameter list
213 and an array of parameters. In both cases the locale must be given as
214 the last single parameter.
217 <example id="zend.view.helpers.initial.translate.dynamic">
218 <title>Change locale dynamically</title>
220 <programlisting language="php"><![CDATA[
222 $date = array("Monday", "April", "11:20:55");
223 $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date, 'it');
228 This example returns the Italian translation for the messageid. But it
229 will only be used once. The next translation will use the locale from
230 the adapter. Normally you will set the desired locale within the
231 translation adapter before you add it to the registry. But you can also
232 set the locale from within the helper:
235 <example id="zend.view.helpers.initial.translate.static">
236 <title>Change locale statically</title>
238 <programlisting language="php"><![CDATA[
240 $date = array("Monday", "April", "11:20:55");
241 $this->translate()->setLocale('it');
242 $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
247 The above example sets <emphasis>'it'</emphasis> as the new default locale which
248 will be used for all further translations.
252 Of course there is also a <methodname>getLocale()</methodname> method to get the
253 currently set locale.
256 <example id="zend.view.helpers.initial.translate.getlocale">
257 <title>Get the currently set locale</title>
259 <programlisting language="php"><![CDATA[
261 $date = array("Monday", "April", "11:20:55");
263 // returns 'de' as set default locale from our above examples
264 $this->translate()->getLocale();
266 $this->translate()->setLocale('it');
267 $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
269 // returns 'it' as new set default locale
270 $this->translate()->getLocale();