Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="body" >
2
 
3
<h1 id="6201">Internationalization (I18N) and Localization (L10N)</h1>
4
<p id="790625" class="block-content">Many web application built with PHP will not have internationalization in mind when it was first written. It may be that it was not intended for use in languages and cultures. Internationalization is an important aspect due to the increase adoption of the Internet in many non-English speaking countries. The process of internationalization and localization will contain difficulties. Below are some general guidelines to internationalize an existing application.</p>
5
 
6
<h2 id="6203">Separate culture/locale sensitive data</h2>
7
 
8
<p id="790626" class="block-content">Identify and separate data that varies with culture. The most obvious are text/string/message. Other type of data should also be considered. The following list categorize some examples of culture sensitive data
9
</p>
10
 
11
<ul id="u1" class="block-content">
12
    <li> Strings, Messages, Text, in relatively small units (e.g. phrases, sentences, paragraphs, but not the full text of a book).</li>
13
    <li> Labels on buttons.</li>
14
    <li> Help files, large units of text, static text.</li>
15
    <li> Sounds.</li>
16
    <li> Colors.</li>
17
    <li> Graphics,Icons.</li>
18
    <li> Dates, Times.</li>
19
    <li> Numbers, Currency, Measurements.</li>
20
    <li> Phone numbers.</li>
21
    <li> Honorific and personal titles.</li>
22
    <li> Postal address.</li>
23
    <li> Page layout.</li>
24
</ul>
25
 
26
<p id="790627" class="block-content">If possible all manner of text should be isolated and store in a persistence format. These text include, application error messages, hard coded strings in PHP files, emails, static HTML text, and text on form elements (e.g. buttons).</p>
27
 
28
<h2 id="6204">Configuration</h2>
29
<p id="790628" class="block-content">To enable the localization features in PRADO, you need to add a few configuration options in your <a href="?page=Configurations.AppConfig">application configuration</a>.
30
First you need to include the <tt>System.I18N.*</tt> namespace to your paths.
31
</p>
32
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_790203">
33
<paths>
34
    <using namespace="System.I18N.*" />
35
</paths>
36
</com:TTextHighlighter>
37
 
38
<p id="790629" class="block-content">Then, if you wish to translate some text in your application, you need to add one translation message data source.</p>
39
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_790204">
40
<module id="globalization" class="TGlobalization">
41
    <translation type="XLIFF"
42
        source="MyApp.messages"
43
        marker="@@"
44
        autosave="true" cache="true" />
45
</module>
46
</com:TTextHighlighter>
47
 
48
<p id="790630" class="block-content">Where <tt>source</tt> in <tt>translation</tt> is the dot path to a directory
49
where you are going to store your translate message catalogue. The <tt>autosave</tt>
50
attribute if enabled, saves untranslated messages back into the message catalogue.
51
With <tt>cache</tt> enabled, translated messages are saved in the application <tt>runtime/i18n</tt> directory.
52
The <tt>marker</tt> value is used to surround any untranslated text.
53
</p>
54
 
55
<p id="790631" class="block-content">With the configuration complete, we can now start to localize your application. If you have <tt>autosave</tt> enabled, after running your application with some localization activity (i.e. translating some text), you will see a directory and a <tt>messages.xml</tt> created within your <tt>source</tt> directory.</p>
56
 
57
<h2 id="6205">What to do with <tt>messages.xml</tt>?</h2>
58
<p id="790632" class="block-content">The translation message catalogue file, if using <tt>type="XLIFF"</tt>, is a standardized translation message interchange XML format. You can edit the XML file using any UTF-8 aware editor. The format of the XML is something like the following.</p>
59
 
60
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_790205">
61
<?xml version="1.0"?>
62
<xliff version="1.0">
63
    <file original="I18N Example IndexPage"
64
          source-language="EN"
65
          datatype="plaintext"
66
          date="2005-01-24T11:07:53Z">
67
        <body>
68
 
69
<trans-unit id="1">
70
<source>Hello world.</source>
71
<target>Hi World!!!</target>
72
</trans-unit>
73
 
74
        </body>
75
    </file>
76
</xliff>
77
</com:TTextHighlighter>
78
 
79
Each translation message is wrapped within a <tt>trans-unit</tt> tag, where <tt>source</tt> is the original message, and <tt>target</tt> is the translated message. Editors such as <a href="http://www.heartsome.net/EN/xlfedit.html">Heartsome XLIFF Translation Editor</a> can help in editing these XML files.
80
 
81
 
82
<h2 id="6206">Setting and Changing Culture</h2>
83
<p id="790633" class="block-content">Once globalization is enabled, you can access the globalization settings, such as, <tt>Culture</tt>, <tt>Charset</tt>, etc, using </p>
84
<com:TTextHighlighter CssClass="source block-content" id="code_790206">
85
$globalization = $this->getApplication()->getGlobalization();
86
echo $globalization->Culture;
87
$globalization->Charset= "GB-2312"; //change the charset
88
</com:TTextHighlighter>
89
 
90
<p id="790634" class="block-content">You also change the way the culture is determined by changing the <tt>class</tt> attribute in the module configuration. For example, to set the culture that depends on the browser settings, you can use the <tt>TGlobalizationAutoDetect</tt> class.
91
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_790207">
92
<module id="globalization" class="TGlobalizationAutoDetect">
93
   ...
94
</module>
95
</com:TTextHighlighter>
96
 
97
<p id="790635" class="block-content">You may also provide your own globalization class to change how the application culture is set.
98
Lastly, you can change the globalization settings on page by page basis using <a href="?page=Configurations.Templates1#tct">template control tags</a>. For example, changing the <tt>Culture</tt> to "zh".</p>
99
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790208">
100
&lt;%@ Application.Globalization.Culture="zh" %&gt;
101
</com:TTextHighlighter>
102
 
103
<h2 id="6207">Localizing your PRADO application</h2>
104
There are two areas in your application that may need message or string localization, in PHP code and in the templates. To localize strings within PHP, use the <tt>localize</tt> function detailed below. To localize text in the template, use the <a href="#ttranslate">TTranslate</a> component.
105
<h2 id="6208">Using <tt>localize</tt> function to translate text within PHP</h2>
106
 
107
<p id="790636" class="block-content">The <tt>localize</tt> function searches for a translated string that matches original from your translation source. First, you need to locate all the hard coded text in PHP that are displayed or sent to the end user. The following example localizes the text of the <tt>$sender</tt> (assuming, say, the sender is a button). The original code before localization is as follows.
108
<com:TTextHighlighter CssClass="source block-content" id="code_790209">
109
function clickMe($sender,$param)
110
{
111
  $sender->Text="Hello, world!";
112
}
113
</com:TTextHighlighter>
114
 
115
<p id="790637" class="block-content">The hard coded message "Hello, world!" is to be localized using the <tt>localize</tt> function. </p>
116
<com:TTextHighlighter CssClass="source block-content" id="code_790210">
117
function clickMe($sender,$param)
118
{
119
  $sender->Text=Prado::localize("Hello, world!");
120
}
121
</com:TTextHighlighter>
122
 
123
<h2 id="6209">Compound Messages</h2>
124
 
125
<p id="790638" class="block-content">Compound messages can contain variable data. For example, in the message "There are 12 users online.", the integer 12 may change depending on some data in your application. This is difficult to translate because the position of the variable data may be difference for different languages. In addition, different languages have their own rules for plurals (if any) and/or quantifiers. The following example can not be easily translated, because the sentence structure is fixed by hard coding the variable data within message.</p>
126
<com:TTextHighlighter CssClass="source block-content" id="code_790211">
127
$num_users = 12;
128
$message = "There are " . $num_users . " users online.";
129
</com:TTextHighlighter>
130
 
131
This problem can be solved using the <tt>localize</tt> function with string substitution. For example, the <tt>$message</tt> string above can be constructed as follows.
132
<com:TTextHighlighter CssClass="source block-content" id="code_790212">
133
$num_users = 12;
134
$message = Prado::localize("There are {num_users} users online.", array('num_users'=>$num_users));
135
</com:TTextHighlighter>
136
<p id="790639" class="block-content">Where the second parameter in <tt>localize</tt> takes an associative array with the key as the substitution to find in the text and replaced it with the associated value.
137
The <tt>localize</tt> function does not solve the problem of localizing languages that have plural forms, the solution is to use <a href="#choice-format">TChoiceFormat</a>.</p>
138
 
139
<p id="790640" class="block-content">The following sample demonstrates the basics of localization in PRADO.</p>
140
<com:RunBar PagePath="Advanced.Samples.I18N.Home" />
141
 
142
<h1 id="6202">I18N Components</h1>
143
<a name="ttranslate"></a>
144
<h2 id="6210">TTranslate</h2>
145
<p id="790641" class="block-content">Messages and strings can be localized in PHP or in templates.
146
To translate a message or string in the template, use <tt>TTranslate</tt>.</p>
147
 
148
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790213">
149
&lt;com:TTranslate&gt;Hello World&lt;/com:TTranslate&gt;
150
&lt;com:TTranslate Text="Goodbye" /&gt;
151
</com:TTextHighlighter>
152
 
153
<p id="790642" class="block-content"><tt>TTranslate</tt> can also perform string substitution.
154
The <tt>Parameters</tt> property can be use to add name values pairs for substitution. Substrings in the translation enclosed with "{" and "}" are consider as the
155
 parameter names during substitution lookup. The following example will substitute the substring "{time}" with the value of the parameter attribute "<tt>Parameters.time=&lt;%= time() %&gt;</tt>".
156
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790214">
157
&lt;com:TTranslate Parameters.time=&lt;%= time() %&gt; &gt;
158
The time is {time}.
159
&lt;/com:TTranslate&gt;
160
</com:TTextHighlighter>
161
 
162
<p id="790643" class="block-content">A short for <tt>TTranslate</tt> is also provided using the following syntax.</p>
163
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790215">
164
&lt;%[string]%&gt;
165
</com:TTextHighlighter>
166
<p id="790644" class="block-content">where string will be translated to different languages according to the end-user's language preference. This syntax can be used with attribute values as well.</p>
167
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790216">
168
&lt;com:TLabel Text="&lt;%[ Hello World! ]%&gt;" /&gt;
169
</com:TTextHighlighter>
170
 
171
<h2 id="6211">TDateFormat</h2>
172
<p id="790645" class="block-content">Formatting localized date and time is straight forward.</p>
173
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790217">
174
&lt;com:TDateFormat Value="12/01/2005" /&gt;
175
</com:TTextHighlighter>
176
 
177
<p id="790646" class="block-content">The <tt>Pattern</tt> property accepts 4 predefined localized date patterns and 4 predefined localized time patterns.</p>
178
<ul id="u2" class="block-content">
179
	<li><tt>fulldate</tt></li>
180
	<li><tt>longdate</tt></li>
181
	<li><tt>mediumdate</tt></li>
182
	<li><tt>shortdate</tt></li>
183
	<li><tt>fulltime</tt></li>
184
	<li><tt>longtime</tt></li>
185
	<li><tt>mediumtime</tt></li>
186
	<li><tt>shorttime</tt></li>
187
</ul>
188
<p id="p1" class="block-content">
189
The predefined can be used in any combination. If using a combined predefined pattern,
190
the first pattern must be the date, followed by a space, and lastly the time pattern.
191
For example, full date pattern with short time pattern. The actual ordering of the
192
date-time and the actual pattern will be determine automatically from locale data specified
193
by the <tt>Culture</tt> property.</p>
194
 
195
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790218">
196
&lt;com:TDateFormat Pattern="fulldate shorttime" /&gt;
197
</com:TTextHighlighter>
198
 
199
<p id="790647" class="block-content">You can also specify a custom pattern using the following sub-patterns.
200
The date/time format is specified by means of a string time pattern. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following:
201
<com:TTextHighlighter Language="text" CssClass="source block-content" id="code_790219">
202
 Symbol   Meaning                 Presentation        Example
203
 ------   -------                 ------------        -------
204
 G        era designator          (Text)              AD
205
 y        year                    (Number)            1996
206
 M        month in year           (Text &amp; Number)     July &amp; 07
207
 d        day in month            (Number)            10
208
 h        hour in am/pm (1~12)    (Number)            12
209
 H        hour in day (0~23)      (Number)            0
210
 m        minute in hour          (Number)            30
211
 s        second in minute        (Number)            55
212
 E        day of week             (Text)              Tuesday
213
 D        day in year             (Number)            189
214
 F        day of week in month    (Number)            2 (2nd Wed in July)
215
 w        week in year            (Number)            27
216
 W        week in month           (Number)            2
217
 a        am/pm marker            (Text)              PM
218
 k        hour in day (1~24)      (Number)            24
219
 K        hour in am/pm (0~11)    (Number)            0
220
 z        time zone               (Time)              Pacific Standard Time
221
 '        escape for text         (Delimiter)         'Date='
222
 ''       single quote            (Literal)           'o''clock'
223
</com:TTextHighlighter>
224
</p>
225
 
226
<p id="790648" class="block-content">The count of pattern letters determine the format.</p>
227
 
228
<p id="790649" class="block-content">(Text): 4 letters uses full form, less than 4, use short or abbreviated form
229
if it exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")</p>
230
 
231
<p id="790650" class="block-content">(Number): the minimum number of digits. Shorter numbers are zero-padded
232
 to this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is
233
 handled specially; that is, if the count of 'y' is 2, the Year will be
234
 truncated to 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".)
235
 Unlike other fields, fractional seconds are padded on the right with zero.</p>
236
 
237
<p id="790651" class="block-content">(Text and Number): 3 or over, use text, otherwise use number. (e.g.,
238
"M" produces "1", "MM" produces "01", "MMM" produces "Jan", and "MMMM"
239
produces "January".)</p>
240
 
241
<p id="790652" class="block-content">Any characters in the pattern that are not in the ranges of ['a'..'z']
242
and ['A'..'Z'] will be treated as quoted text. For instance, characters
243
like ':', '.', ' ', and '@' will appear in the resulting time text
244
even they are not embraced within single quotes.</p>
245
 
246
<p id="790653" class="block-content">Examples using the US locale:
247
 
248
<com:TTextHighlighter Language="text" CssClass="source block-content" id="code_790220">
249
Format Pattern                         Result
250
--------------                         -------
251
"yyyy.MM.dd G 'at' HH:mm:ss"      -&gt;&gt;  1996.07.10 AD at 15:08:56
252
"EEE, MMM d, ''yy"                -&gt;&gt;  Wed, Jul 10, '96
253
"h:mm a"                          -&gt;&gt;  12:08 PM
254
"hh 'o''clock' a, z"              -&gt;&gt;  12 o'clock PM, Pacific Daylight Time
255
"K:mm a"                          -&gt;&gt;  0:00 PM
256
"yyyy.MMMM.dd G hh:mm a"          -&gt;&gt;  1996.July.10 AD 12:08 PM
257
</com:TTextHighlighter>
258
</p>
259
 
260
<p id="790654" class="block-content">If the <tt>Value</tt> property is not specified, the current date and time is used.</p>
261
 
262
<h2 id="6212">TNumberFormat</h2>
263
<p id="790655" class="block-content">PRADO's Internationalization framework provide localized currency formatting and number formatting. Please note that the <tt>TNumberFormat</tt> component provides formatting only, it does not perform current conversion or exchange.</p>
264
 
265
<p id="790656" class="block-content">Numbers can be formatted as currency, percentage, decimal or scientific
266
numbers by specifying the <tt>Type</tt> attribute. The valid types are:</p>
267
<ul id="u3" class="block-content">
268
	<li><tt>currency</tt></li>
269
	<li><tt>percentage</tt></li>
270
	<li><tt>decimal</tt></li>
271
	<li><tt>scientific</tt></li>
272
</ul>
273
 
274
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790221">
275
&lt;com:TNumberFormat Type="currency" Value="100" /&gt;
276
</com:TTextHighlighter>
277
 
278
<p id="790657" class="block-content"><tt>Culture</tt> and <tt>Currency</tt> properties may be specified to format locale specific numbers. </p>
279
 
280
<p id="790658" class="block-content">If someone from US want to see sales figures from a store in
281
Germany (say using the EURO currency), formatted using the german
282
 currency, you would need to use the attribute <tt>Culture="de_DE"</tt> to get
283
the currency right, e.g. 100,00$. The decimal and grouping separator is
284
then also from the <tt>de_DE</tt> locale. This may lead to some confusion because
285
people from US uses the "," (comma) as thousand separator. Therefore a <tt>Currency</tt>
286
attribute is available, so that the output from the following example results in $100.00
287
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790222">
288
&lt;com:TNumberFormat Type="currency"
289
          Culture="en_US" Currency="EUR" Value="100" /&gt;
290
</com:TTextHighlighter>
291
</p>
292
 
293
<p id="790659" class="block-content">The <tt>Pattern</tt> property determines the number of digits, thousand grouping
294
positions, the number of decimal points and the decimal position. The actual characters that
295
are used to represent the decimal points and thousand points are culture specific
296
and will change automatically according to the <tt>Culture</tt> property. The valid
297
<tt>Pattern</tt> characters are:</p>
298
<ul id="u6" class="block-content">
299
	<li><tt># (hash)</tt> - represents the optional digits</li>
300
	<li><tt>0 (zero)</tt> - represents the mandatory digits, zero left filled</li>
301
	<li><tt>. (full stop)</tt> - the position of the decimal point (only 1 decimal point is allowed)</li>
302
	<li><tt>, (comma)</tt> - thousand point separation (up to 2 commas are allowed)</li>
303
</ul>
304
<p id="p2" class="block-content">
305
For example, consider the <tt>Value="1234567.12345"</tt> and
306
with <tt>Culture="en_US"</tt> (which uses "," for thousand point separator and "." for decimal separators).
307
</p>
308
<com:TTextHighlighter Language="text" CssClass="source block-content" id="code_790223">
309
Pattern                      Output
310
-------                      ------
311
##,###.00               -&gt;&gt;  1,234,567.12
312
##,###.##               -&gt;&gt;  1,234,567.12345
313
##,##.0000              -&gt;&gt;  1,23,45,67.1235
314
##,###,##.0             -&gt;&gt;  12,345,67.1
315
000,000,000.0           -&gt;&gt;  001,234,567.1
316
</com:TTextHighlighter>
317
</p>
318
 
319
<h2 id="6213">TTranslateParameter</h2>
320
<p id="790660" class="block-content">Compound messages, i.e., string substitution, can be accomplished with <tt>TTranslateParameter</tt>.
321
In the following example, the strings "{greeting}" and "{name}" will be replace
322
with the values of "Hello" and "World", respectively.The substitution string must be enclose with "{" and "}". The parameters can be further translated by using <tt>TTranslate</tt>.
323
 
324
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790224">
325
&lt;com:TTranslate&gt;
326
  {greeting} {name}!
327
  &lt;com:TTranslateParameter Key="name">World&lt;/com:TTranslateParameter&gt;
328
  &lt;com:TTranslateParameter Key="greeting">Hello&lt;/com:TTranslateParameter&gt;
329
&lt;/com:TTranslate&gt;
330
</com:TTextHighlighter>
331
 
332
 
333
<a name="choice-format"></a>
334
<h2 id="6214">TChoiceFormat</h2>
335
 
336
<p id="790661" class="block-content">Using the <tt>localize</tt> function or <tt>TTranslate</tt> component to translate messages does not inform the translator the cardinality of the data required to determine the correct plural structure to use. It only informs them that there is a variable data, the data could be anything. Thus, the translator will be unable to determine with respect to the substitution data the correct plural, language structure or phrase to use . E.g. in English, to translate the sentence, "There are {number} of apples.", the resulting translation should be different depending on the <tt>number</tt> of apples.</p>
337
 
338
<p id="790662" class="block-content">The <tt>TChoiceFormat</tt> component performs message/string choice translation. The following example demonstrated a simple 2 choice message translation.</p>
339
 
340
<com:TTextHighlighter Language="prado" CssClass="source block-content" id="code_790225">
341
&lt;com:TChoiceFormat Value="1"/&gt;[1] One Apple. |[2] Two Apples&lt;/com:TChoiceFormat&gt;
342
</com:TTextHighlighter>
343
 
344
<p id="790663" class="block-content">In the above example, the <tt>Value</tt> "1" (one), thus the translated string
345
is "One Apple". If the <tt>Value</tt> was "2", then it will show "Two Apples".</p>
346
 
347
<p id="790664" class="block-content">The message/string choices are separated by the pipe "|" followed by a set notation of the form.</p>
348
<ul id="u7" class="block-content">
349
    <li><tt>[1,2]</tt> -- accepts values between 1 and 2, inclusive.</li>
350
    <li><tt>(1,2)</tt> -- accepts values between 1 and 2, excluding 1 and 2.</li>
351
    <li><tt>{1,2,3,4}</tt> -- only values defined in the set are accepted.</li>
352
    <li><tt>[-Inf,0)</tt> -- accepts value greater or equal to negative infinity
353
                        and strictly less than 0</li>
354
</ul>
355
 
356
<p id="790665" class="block-content">Any non-empty combinations of the delimiters of square and round brackets are acceptable.
357
The string chosen for display depends on the <tt>Value</tt> property. The <tt>Value</tt> is evaluated for each set until the <tt>Value</tt> is found to belong to a particular set.</p>
358
 
359
 
360
<div class="last-modified">$Id: I18N.page 1650 2007-01-24 06:55:32Z wei $</div></com:TContent>