File: Zend_Translate-SourceCreation.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (399 lines) | stat: -rw-r--r-- 13,957 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.translate.sourcecreation">
    <title>Creating source files</title>

    <para>
        Below you will find a description of the different source formats
        which can be used with <classname>Zend_Translate</classname>.
    </para>

    <note>
        <para>
            Note that most of the described formats should be created by using
            a tool or a generation process. These Tools and processes are not part
            of Zend Framework and for most of the described formats free tools
            are available.
        </para>
    </note>

    <sect2 id="zend.translate.sourcecreation.array">
        <title>Creating Array source files</title>

        <para>
            Array source files are plain arrays. But you have to define them
            manually since there is no tool to aid this.
            But because they are so simple, it's the fastest way to look up
            messages if your code works as expected. It's generally the best
            adapter to get started with translation business.
        </para>

        <programlisting language="php"><![CDATA[
$english = array(
    'message1' => 'message1',
    'message2' => 'message2',
    'message3' => 'message3');

$german = array(
    'message1' => 'Nachricht1',
    'message2' => 'Nachricht2',
    'message3' => 'Nachricht3');

$translate = new Zend_Translate(
    array(
        'adapter' => 'array',
        'content' => $english,
        'locale'  => 'en'
    )
);
$translate->addTranslation(array('content' => $german, 'locale' => 'de'));
]]></programlisting>

        <para>
            Since release 1.5 it is also supported to have arrays included within an external file.
            You just have to provide the filename and <classname>Zend_Translate</classname> will
            automatically include it and look for the array. See the following example for details:
        </para>

        <programlisting language="php"><![CDATA[
// myarray.php
return array(
    'message1' => 'Nachricht1',
    'message2' => 'Nachricht2',
    'message3' => 'Nachricht3');

// controller
$translate = new Zend_Translate(
    array(
        'adapter' => 'array',
        'content' => '/path/to/myarray.php',
        'locale'  => 'de'
    )
);
]]></programlisting>

        <note>
            <para>
                Files which do not return an array will fail to be included.
                Also any output within this file will be ignored and suppressed.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.translate.sourcecreation.gettext">
        <title>Creating Gettext source files</title>

        <para>
            Gettext source files are created by GNU's gettext library.
            There are several free tools available that can parse your
            code files and create the needed gettext source files.
            These have the extension <emphasis>*.mo</emphasis>
            and they are binary files.
            An open source tool for creating the files is
            <ulink url="http://sourceforge.net/projects/poedit/">poEdit</ulink>.
            This tool also supports you during the translation process itself.
        </para>

        <programlisting language="php"><![CDATA[
// We accume that we have created the mo files and translated them
$translate = new Zend_Translate(
    array(
        'adapter' => 'gettext',
        'content' => '/path/to/english.mo',
        'locale'  => 'en'
    )
);
$translate->addTranslation(
    array(
        'content' => '/path/to/german.mo',
        'locale' => 'de'
    )
);
]]></programlisting>

        <para>
            As you can see the adapters are used exactly the same way,
            with one small difference:
            change <emphasis>array</emphasis> to <emphasis>gettext</emphasis>. All other usages are
            exactly the same as with all other adapters.
            With the gettext adapter you no longer have to be aware of
            gettext's standard directory structure,
            bindtextdomain and textdomain.
            Just give the path and filename to the adapter.
        </para>

        <note>
            <para>
                 You should always use UTF-8 as source encoding.
                 Otherwise you will have problems when using two
                 different source encodings.
                 E.g. one of your source files is encoded
                 with ISO-8815-11 and another one with CP815.
                 You can set only one encoding for your source file,
                 so one of your languages probably will not display correctly.
            </para>

            <para>
                 UTF-8 is a portable format which supports all languages.
                 When using UTF-8 for all languages, you will eliminate
                 the problem of incompatible encodings.
            </para>
        </note>

        <para>
            Many gettext editors add adapter informations as empty translation string.
            This is the reason why empty strings are not translated when using the
            gettext adapter. Instead they are erased from the translation table and
            provided by the <methodname>getAdapterInfo()</methodname> method. It will return
            the adapter informations for all added gettext files as array using the
            filename as key.
        </para>

        <programlisting language="php"><![CDATA[
// Getting the adapter informations
$translate = new Zend_Translate(
    array(
        'adapter' => 'gettext',
        'content' => '/path/to/english.mo',
        'locale'  => 'en'
    )
);
print_r($translate->getAdapterInfo());
]]></programlisting>
    </sect2>

    <sect2 id="zend.translate.sourcecreation.tmx">
        <title>Creating TMX source files</title>

        <para>
            TMX source files are a new industry standard.
            They have the advantage of being <acronym>XML</acronym> files and so they are
            readable by every editor and of course by humans.
            You can either create TMX files manually with a text editor,
            or you can use a special tool. But most tools currently available for
            creating TMX source files are not freely available.
        </para>

        <example id="zend.translate.sourcecreation.tmx.example">
            <title>Example TMX file</title>

            <programlisting language="xml"><![CDATA[
<?xml version="1.0" ?>
<!DOCTYPE tmx SYSTEM "tmx14.dtd">
<tmx version="1.4">
   <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
           adminlang="en-us" srclang="de-at" o-tmf="abc"
           creationtool="XYZTool" >
   </header>
   <body>
       <tu tuid='message1'>
           <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
           <tuv xml:lang="en"><seg>message1</seg></tuv>
       </tu>
       <tu tuid='message2'>
           <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
           <tuv xml:lang="en"><seg>message2</seg></tuv>
       </tu>
   </body>
</tmx>
]]></programlisting>

            <programlisting language="php"><![CDATA[
$translate = new Zend_Translate(
    array(
        'adapter' => 'tmx',
        'content' => 'path/to/mytranslation.tmx',
        'locale'  => 'en'
    )
);
]]></programlisting>
        </example>

        <para>
            TMX files can have several languages within the same file.
            All other included languages are added automatically,
            so you do not have to call <methodname>addLanguage()</methodname>.
        </para>

        <para>
            If you want to have only specified languages from the source translated you can
            set the option <property>defined_language</property> to <constant>TRUE</constant>.
            With this option you can add the wished languages explicitly with
            <methodname>addLanguage()</methodname>. The default value for this option is to add all
            languages.
        </para>

        <note>
            <title>Option useId</title>

            <para>
                When you set the <emphasis>useId</emphasis> option to <constant>FALSE</constant>
                then the <emphasis>srclang</emphasis> header will be used to define the language
                which sets the message.
            </para>

            <para>
                In our example the message key would <emphasis>message1</emphasis> per default.
                When this option is set to <constant>FALSE</constant> the message key
                <emphasis>Nachricht1</emphasis> would be used.
            </para>

            <para>
                Note that the <emphasis>tuv</emphasis> entry which is related to the
                <emphasis>srclang</emphasis> entry must be the first
                <emphasis>tuv</emphasis> entry which is set like shown in the above example.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.translate.sourcecreation.csv">
        <title>Creating CSV source files</title>

        <para>
            CSV source files are small and human readable.
            If your customers want to translate their own,
            you will probably use the CSV adapter.
        </para>

        <example id="zend.translate.sourcecreation.csv.example">
            <title>Example CSV file</title>

            <programlisting language="txt"><![CDATA[
#Example csv file
message1;Nachricht1
message2;Nachricht2
]]></programlisting>

            <programlisting language="php"><![CDATA[
$translate = new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => '/path/to/mytranslation.csv',
        'locale'  => 'de'
    )
);
$translate->addTranslation(
    array(
        'content' => 'path/to/other.csv',
        'locale' => 'fr'
    )
);
]]></programlisting>
        </example>

        <para>
            There are three different options for the CSV adapter.
            You can set <property>delimiter</property>, <property>limit</property> and
            <property>enclosure</property>.
        </para>

        <para>
            The default delimiter for CSV string is '<emphasis>;</emphasis>', but
            with the option <property>delimiter</property>
            you can decide to use another one.
        </para>

        <para>
            The default limit for a line within a CSV file is '<emphasis>0</emphasis>'. This means
            that the end of a CSV line is searched automatically. If you set
            <property>limit</property> to any value, then the CSV file will be
            read faster, but any line exceeding this limit will be truncated.
        </para>

        <para>
            The default enclosure to use for CSV files is '<emphasis>"</emphasis>'. You can
            set a different one using the option <property>enclosure</property>.
        </para>

        <example id="zend.translate.sourcecreation.csv.example2">
            <title>Second CSV file example</title>

            <programlisting language="txt"><![CDATA[
# Example CSV file
"message,1",Nachricht1
message2,"Nachricht,2"
"message3,",Nachricht3
]]></programlisting>

            <programlisting language="php"><![CDATA[
$translate = new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => '/path/to/mytranslation.csv',
        'locale'  => 'de',
        'delimiter' => ','
    )
);

$translate->addTranslation(
    array(
        'content' => '/path/to/other.csv',
        'locale' => 'fr'
    )
);
]]></programlisting>
        </example>

        <note>
            <para>
                When you are using non-ASCII characters within your CSV file, like umlauts or UTF-8
                chars, then you should always use enclosure. Omitting the enclosure can lead to
                missing characters in your translation.
            </para>
        </note>
    </sect2>

    <sect2 id="zend.translate.sourcecreation.ini">
        <title>Creating INI source files</title>

        <para>
            <acronym>INI</acronym> source files are human readable but normally not very small as
            they also include other data beside translations. If you have data which shall be
            editable by your customers you can use the <acronym>INI</acronym> adapter.
        </para>

        <example id="zend.translate.sourcecreation.ini.example">
            <title>Example INI file</title>

            <programlisting language="txt"><![CDATA[
[Test]
;TestPage Comment
Message_1="Nachricht 1 (de)"
Message_2="Nachricht 2 (de)"
Message_3="Nachricht :3 (de)"
]]></programlisting>

            <programlisting language="php"><![CDATA[
$translate = new Zend_Translate(
    array(
        'adapter' => 'ini',
        'content' => '/path/to/mytranslation.ini',
        'locale'  => 'de'
    )
);
$translate->addTranslation(
    array(
        'content' => '/path/to/other.ini',
        'locale' => 'it'
    )
);
]]></programlisting>
        </example>

        <para>
            <acronym>INI</acronym> files have several restrictions. If a value in the
            <acronym>INI</acronym> file contains any non-alphanumeric characters it needs to be
            enclosed in double-quotes (<emphasis>"</emphasis>). There are also reserved words which
            must not be used as keys for <acronym>INI</acronym> files. These include:
            <constant>NULL</constant>, yes, no, <constant>TRUE</constant>, and
            <constant>FALSE</constant>. Values <constant>NULL</constant>, <emphasis>no</emphasis>
            and <constant>FALSE</constant> results in <emphasis>""</emphasis>,
            <emphasis>yes</emphasis> and <constant>TRUE</constant> results in '1'. Characters
            <emphasis>{}|&amp;~![()"</emphasis> must not be used anywhere in the key and have a
            special meaning in the value. Do not use them as it will produce unexpected behaviour.
        </para>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->