[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <com:TContent ID="body" > 2 3 <h1 id="6201">Internationalization (I18N) and Localization (L10N)</h1> 4 <p>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>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> 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>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>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"> 33 <paths> 34 <using namespace="System.I18N.*" /> 35 </paths> 36 </com:TTextHighlighter> 37 38 <p>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"> 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>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>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>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"> 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>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"> 85 $globalization = $this->getApplication()->getGlobalization(); 86 echo $globalization->Culture; 87 $globalization->Charset= "GB-2312"; //change the charset 88 </com:TTextHighlighter> 89 90 <p>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"> 92 <module id="globalization" class="TGlobalizationAutoDetect"> 93 ... 94 </module> 95 </com:TTextHighlighter> 96 97 <p>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"> 100 <%@ Application.Globalization.Culture="zh" %> 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>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"> 109 function clickMe($sender,$param) 110 { 111 $sender->Text="Hello, world!"; 112 } 113 </com:TTextHighlighter> 114 115 <p>The hard coded message "Hello, world!" is to be localized using the <tt>localize</tt> function. </p> 116 <com:TTextHighlighter CssClass="source"> 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>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"> 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"> 133 $num_users = 12; 134 $message = Prado::localize("There are {num_users} users online.", array('num_users'=>$num_users)); 135 </com:TTextHighlighter> 136 <p>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>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>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"> 149 <com:TTranslate>Hello World</com:TTranslate> 150 <com:TTranslate Text="Goodbye" /> 151 </com:TTextHighlighter> 152 153 <p><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=<%= time() %></tt>". 156 <com:TTextHighlighter Language="prado" CssClass="source"> 157 <com:TTranslate Parameters.time=<%= time() %> > 158 The time is {time}. 159 </com:TTranslate> 160 </com:TTextHighlighter> 161 162 <p>A short for <tt>TTranslate</tt> is also provided using the following syntax.</p> 163 <com:TTextHighlighter Language="prado" CssClass="source"> 164 <%[string]%> 165 </com:TTextHighlighter> 166 <p>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"> 168 <com:TLabel Text="<%[ Hello World! ]%>" /> 169 </com:TTextHighlighter> 170 171 <h2 id="6211">TDateFormat</h2> 172 <p>Formatting localized date and time is straight forward.</p> 173 <com:TTextHighlighter Language="prado" CssClass="source"> 174 <com:TDateFormat Value="12/01/2005" /> 175 </com:TTextHighlighter> 176 177 <p>The <tt>Pattern</tt> property accepts 4 predefined localized date patterns and 4 predefined localized time patterns. 178 <ul> 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 The predefined can be used in any combination. If using a combined predefined pattern, 189 the first pattern must be the date, followed by a space, and lastly the time pattern. 190 For example, full date pattern with short time pattern. The actual ordering of the 191 date-time and the actual pattern will be determine automatically from locale data specified 192 by the <tt>Culture</tt> property.</p> 193 194 <com:TTextHighlighter Language="prado" CssClass="source"> 195 <com:TDateFormat Pattern="fulldate shorttime" /> 196 </com:TTextHighlighter> 197 198 <p>You can also specify a custom pattern using the following sub-patterns. 199 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: 200 <com:TTextHighlighter Language="text" CssClass="source"> 201 Symbol Meaning Presentation Example 202 ------ ------- ------------ ------- 203 G era designator (Text) AD 204 y year (Number) 1996 205 M month in year (Text & Number) July & 07 206 d day in month (Number) 10 207 h hour in am/pm (1~12) (Number) 12 208 H hour in day (0~23) (Number) 0 209 m minute in hour (Number) 30 210 s second in minute (Number) 55 211 E day of week (Text) Tuesday 212 D day in year (Number) 189 213 F day of week in month (Number) 2 (2nd Wed in July) 214 w week in year (Number) 27 215 W week in month (Number) 2 216 a am/pm marker (Text) PM 217 k hour in day (1~24) (Number) 24 218 K hour in am/pm (0~11) (Number) 0 219 z time zone (Time) Pacific Standard Time 220 ' escape for text (Delimiter) 'Date=' 221 '' single quote (Literal) 'o''clock' 222 </com:TTextHighlighter> 223 </p> 224 225 <p>The count of pattern letters determine the format.</p> 226 227 <p>(Text): 4 letters uses full form, less than 4, use short or abbreviated form 228 if it exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")</p> 229 230 <p>(Number): the minimum number of digits. Shorter numbers are zero-padded 231 to this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is 232 handled specially; that is, if the count of 'y' is 2, the Year will be 233 truncated to 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) 234 Unlike other fields, fractional seconds are padded on the right with zero.</p> 235 236 <p>(Text and Number): 3 or over, use text, otherwise use number. (e.g., 237 "M" produces "1", "MM" produces "01", "MMM" produces "Jan", and "MMMM" 238 produces "January".)</p> 239 240 <p>Any characters in the pattern that are not in the ranges of ['a'..'z'] 241 and ['A'..'Z'] will be treated as quoted text. For instance, characters 242 like ':', '.', ' ', and '@' will appear in the resulting time text 243 even they are not embraced within single quotes.</p> 244 245 <p>Examples using the US locale: 246 247 <com:TTextHighlighter Language="text" CssClass="source"> 248 Format Pattern Result 249 -------------- ------- 250 "yyyy.MM.dd G 'at' HH:mm:ss" ->> 1996.07.10 AD at 15:08:56 251 "EEE, MMM d, ''yy" ->> Wed, Jul 10, '96 252 "h:mm a" ->> 12:08 PM 253 "hh 'o''clock' a, z" ->> 12 o'clock PM, Pacific Daylight Time 254 "K:mm a" ->> 0:00 PM 255 "yyyy.MMMM.dd G hh:mm a" ->> 1996.July.10 AD 12:08 PM 256 </com:TTextHighlighter> 257 </p> 258 259 <p>If the <tt>Value</tt> property is not specified, the current date and time is used.</p> 260 261 <h2 id="6212">TNumberFormat</h2> 262 <p>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> 263 264 <p>Numbers can be formatted as currency, percentage, decimal or scientific 265 numbers by specifying the <tt>Type</tt> attribute. The valid types are: 266 <ul> 267 <li><tt>currency</tt></li> 268 <li><tt>percentage</tt></li> 269 <li><tt>decimal</tt></li> 270 <li><tt>scientific</tt></li> 271 </ul> 272 </p> 273 274 <com:TTextHighlighter Language="prado" CssClass="source"> 275 <com:TNumberFormat Type="currency" Value="100" /> 276 </com:TTextHighlighter> 277 278 <p><tt>Culture</tt> and <tt>Currency</tt> properties may be specified to format locale specific numbers. </p> 279 280 <p>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"> 288 <com:TNumberFormat Type="currency" 289 Culture="en_US" Currency="EUR" Value="100" /> 290 </com:TTextHighlighter> 291 </p> 292 293 <p>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: 298 <ul> 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 For example, consider the <tt>Value="1234567.12345"</tt> and 305 with <tt>Culture="en_US"</tt> (which uses "," for thousand point separator and "." for decimal separators). 306 <com:TTextHighlighter Language="text" CssClass="source"> 307 Pattern Output 308 ------- ------ 309 ##,###.00 ->> 1,234,567.12 310 ##,###.## ->> 1,234,567.12345 311 ##,##.0000 ->> 1,23,45,67.1235 312 ##,###,##.0 ->> 12,345,67.1 313 000,000,000.0 ->> 001,234,567.1 314 </com:TTextHighlighter> 315 </p> 316 317 <h2 id="6213">TTranslateParameter</h2> 318 <p>Compound messages, i.e., string substitution, can be accomplished with <tt>TTranslateParameter</tt>. 319 In the following example, the strings "{greeting}" and "{name}" will be replace 320 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>. 321 322 <com:TTextHighlighter Language="prado" CssClass="source"> 323 <com:TTranslate> 324 {greeting} {name}! 325 <com:TTranslateParameter Key="name">World</com:TTranslateParameter> 326 <com:TTranslateParameter Key="greeting">Hello</com:TTranslateParameter> 327 </com:TTranslate> 328 </com:TTextHighlighter> 329 330 331 <a name="choice-format"></a> 332 <h2 id="6214">TChoiceFormat</h2> 333 334 <p>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> 335 336 <p>The <tt>TChoiceFormat</tt> component performs message/string choice translation. The following example demonstrated a simple 2 choice message translation.</p> 337 338 <com:TTextHighlighter Language="prado" CssClass="source"> 339 <com:TChoiceFormat Value="1"/>[1] One Apple. |[2] Two Apples</com:TChoiceFormat> 340 </com:TTextHighlighter> 341 342 <p>In the above example, the <tt>Value</tt> "1" (one), thus the translated string 343 is "One Apple". If the <tt>Value</tt> was "2", then it will show "Two Apples".</p> 344 345 <p>The message/string choices are separated by the pipe "|" followed by a set notation of the form.</p> 346 <ul> 347 <li><tt>[1,2]</tt> -- accepts values between 1 and 2, inclusive.</li> 348 <li><tt>(1,2)</tt> -- accepts values between 1 and 2, excluding 1 and 2.</li> 349 <li><tt>{1,2,3,4}</tt> -- only values defined in the set are accepted.</li> 350 <li><tt>[-Inf,0)</tt> -- accepts value greater or equal to negative infinity 351 and strictly less than 0</li> 352 </ul> 353 354 <p>Any non-empty combinations of the delimiters of square and round brackets are acceptable. 355 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> 356 357 358 </com:TContent>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |