|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Internationalization</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Internationalization</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>The Internationalization utility supports the management of localized resources such as strings and formatting patterns.</p> |
|
|
32 |
</div> |
|
|
33 |
|
|
|
34 |
<h2 id="scenarios">Usage Scenarios for the Internationalization Utility</h2> |
|
|
35 |
|
|
|
36 |
<p>The YUI Internationalization utility supports externalization, that is, |
|
|
37 |
separating data that needs to change for different languages or markets |
|
|
38 |
from the code of a software product, so that the same code can be used |
|
|
39 |
worldwide.</p> |
|
|
40 |
|
|
|
41 |
<p>Depending on the kind of software you create with YUI, you will interact |
|
|
42 |
with the Internationalization utility in different ways.</p> |
|
|
43 |
|
|
|
44 |
<h3 id="monolingualApps">Monolingual Applications</h3> |
|
|
45 |
|
|
|
46 |
<p>Many applications using YUI are not internationalized themselves; they |
|
|
47 |
use one user interface language to target one market. |
|
|
48 |
However, such applications still want language-sensitive modules that |
|
|
49 |
they rely on to be internationalized and localized for their language. |
|
|
50 |
For example, an application using Chinese to target Hong Kong wants dates |
|
|
51 |
to be displayed in a Chinese format appropriate for Hong Kong, and so |
|
|
52 |
relies on the DataType utility to provide such formats.</p> |
|
|
53 |
|
|
|
54 |
<p>If the modules that such an application uses support the language of |
|
|
55 |
the application, the problem is solved by simply |
|
|
56 |
<a href="#prefLang">requesting preferred languages</a>. Otherwise, the |
|
|
57 |
application may be able to fill the gap by |
|
|
58 |
<a href="#appResources">providing resources to modules</a>.</p> |
|
|
59 |
|
|
|
60 |
<h3 id="multilingualApps">Multilingual Applications</h3> |
|
|
61 |
|
|
|
62 |
<p>An application that's intended for users in different markets or |
|
|
63 |
using different languages has to be internationalized.<p> |
|
|
64 |
|
|
|
65 |
<p>Primarily, this means developing its code in the form of |
|
|
66 |
<a href="#intlModules">internationalized modules</a>, determining |
|
|
67 |
the preferred user interface language(s), |
|
|
68 |
<a href="#prefLang">requesting preferred languages</a>, and possibly |
|
|
69 |
<a href="#appResources">providing resources to modules</a>.</p> |
|
|
70 |
|
|
|
71 |
<p>Optionally, an application can provide a user interface element |
|
|
72 |
that lets the user <a href="#switchingLangs">switch languages</a> |
|
|
73 |
on the fly.</p> |
|
|
74 |
|
|
|
75 |
<h3 id="intlModules">Internationalized Modules</h3> |
|
|
76 |
|
|
|
77 |
<p>A module whose functionality is sensitive to different markets and |
|
|
78 |
languages and that's intended for use by multilingual applications |
|
|
79 |
or by different monolingual applications |
|
|
80 |
has to be <a href="#modules">internationalized</a>.</p> |
|
|
81 |
|
|
|
82 |
<h2 id="getting-started">Getting Started</h2> |
|
|
83 |
|
|
|
84 |
<p> |
|
|
85 |
To include the source files for Internationalization and its dependencies, first load |
|
|
86 |
the YUI seed file if you haven't already loaded it. |
|
|
87 |
</p> |
|
|
88 |
|
|
|
89 |
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre> |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
<p> |
|
|
93 |
Next, create a new YUI instance for your application and populate it with the |
|
|
94 |
modules you need by specifying them as arguments to the <code>YUI().use()</code> method. |
|
|
95 |
YUI will automatically load any dependencies required by the modules you |
|
|
96 |
specify. |
|
|
97 |
</p> |
|
|
98 |
|
|
|
99 |
<pre class="code prettyprint"><script> |
|
|
100 |
// Create a new YUI instance and populate it with the required modules. |
|
|
101 |
YUI().use('intl', function (Y) { |
|
|
102 |
// Internationalization is available and ready for use. Add implementation |
|
|
103 |
// code here. |
|
|
104 |
}); |
|
|
105 |
</script></pre> |
|
|
106 |
|
|
|
107 |
|
|
|
108 |
<p> |
|
|
109 |
For more information on creating YUI instances and on the |
|
|
110 |
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the |
|
|
111 |
documentation for the <a href="../yui/index.html">YUI Global Object</a>. |
|
|
112 |
</p> |
|
|
113 |
|
|
|
114 |
|
|
|
115 |
<h2 id="using">Using the Internationalization Utility</h2> |
|
|
116 |
|
|
|
117 |
<h3 id="bcp47">Using BCP 47 Language Tags</h3> |
|
|
118 |
|
|
|
119 |
<p>BCP 47 language tags are the identifiers for languages used on the internet. |
|
|
120 |
BCP stands for IETF Best Current Practice, and BCP 47 is currently the combination |
|
|
121 |
of <a href="http://tools.ietf.org/html/rfc5646">RFC 5646</a> and |
|
|
122 |
<a href="http://tools.ietf.org/html/rfc4647">RFC 4647</a>. |
|
|
123 |
These tags allow the description of languages in varying levels of detail, from |
|
|
124 |
"Chinese" ("zh") to "Chinese written in traditional characters as used in Taiwan" |
|
|
125 |
("zh-Hant-TW") and more. Typical components ("subtags") are ISO 639 language codes, |
|
|
126 |
ISO 15924 script codes, and ISO 3166 country codes. Subtags are separated by |
|
|
127 |
hyphens.</p> |
|
|
128 |
|
|
|
129 |
<p>Here are the language tags for some popular languages:</p> |
|
|
130 |
|
|
|
131 |
<table> |
|
|
132 |
<tr><th>Language Tag<th>Description |
|
|
133 |
<tr><td>zh-Hans-CN<td>Chinese, simplified characters, China |
|
|
134 |
<tr><td>es<td>Spanish |
|
|
135 |
<tr><td>en<td>English |
|
|
136 |
<tr><td>hi-IN<td>Hindi, India |
|
|
137 |
<tr><td>ar<td>Arabic |
|
|
138 |
<tr><td>en-US<td>English, U.S.A. |
|
|
139 |
<tr><td>id-ID<td>Indonesian, Indonesia |
|
|
140 |
<tr><td>pt-BR<td>Portuguese, Brazil |
|
|
141 |
<tr><td>ru-RU<td>Russian, Russia |
|
|
142 |
<tr><td>fr<td>French |
|
|
143 |
<tr><td>ja-JP<td>Japanese, Japan |
|
|
144 |
<tr><td>es-MX<td>Spanish, Mexico |
|
|
145 |
</table> |
|
|
146 |
|
|
|
147 |
<p>BCP 47 also defines a "Lookup" algorithm, which is commonly used to determine |
|
|
148 |
the best language for a user interface. Its input is an ordered list of |
|
|
149 |
languages that the user prefers, and the list of languages that the software |
|
|
150 |
supports. When looking for a language, the algorithm uses a fallback that |
|
|
151 |
successively simplifies a language tag. For example, when looking for |
|
|
152 |
a requested "zh-Hans-CN", it also checks whether "zh-Hans" or "zh" are |
|
|
153 |
available.</p> |
|
|
154 |
|
|
|
155 |
<p>The Internationalization utility provides the Lookup algorithm as the |
|
|
156 |
<code>Intl.lookupBestLang</code> method, and the YUI loader uses it to determine |
|
|
157 |
the best language based on an application's request and a module's language |
|
|
158 |
support.</p> |
|
|
159 |
|
|
|
160 |
<p>When requesting a language, it's generally a good idea to be specific and |
|
|
161 |
include the country, because in some cases the differences between countries |
|
|
162 |
are significant. For example, "3/5/10" means "March 5, 2010" in U.S. English, |
|
|
163 |
but "3 May 2010" in British English.</p> |
|
|
164 |
|
|
|
165 |
<p>When providing language support, on the other hand, you should also support |
|
|
166 |
the less specific variant without country ("en", "es", "zh-Hans", etc.), so that |
|
|
167 |
the fallback finds something when a request includes a country that you don't |
|
|
168 |
support. Where the usage in different countries using the same language diverges |
|
|
169 |
siginificantly, try to be neutral, e.g., by formatting dates in ISO notation |
|
|
170 |
as 2010-03-05.</p> |
|
|
171 |
|
|
|
172 |
<h3 id="applications">Internationalizing Applications</h3> |
|
|
173 |
|
|
|
174 |
<h4 id="prefLang">Requesting Preferred Languages</h4> |
|
|
175 |
|
|
|
176 |
<p>When creating a YUI instance, you can specify a list of preferred languages.</p> |
|
|
177 |
|
|
|
178 |
<p>For a monolingual application, this list starts with the user interface |
|
|
179 |
language of the application, but it may include other languages that |
|
|
180 |
users are likely to understand, in case a module doesn't support the |
|
|
181 |
preferred language. For example, an application in Arabic for Morocco might |
|
|
182 |
specify French as a second choice since French is widely spoken in Morocco.</p> |
|
|
183 |
|
|
|
184 |
<p>A multilingual application might maintain user language preferences as part of |
|
|
185 |
the application, derive the preference list from the <code>Accept-Language</code> |
|
|
186 |
header provided by the browser, or determine the list in some other fashion.</p> |
|
|
187 |
|
|
|
188 |
<p>The preference list is specified as the <code>lang</code> property of the YUI |
|
|
189 |
instance's config object. The YUI instance uses it to select the best available |
|
|
190 |
language for each module and load the necessary resource bundles.</p> |
|
|
191 |
|
|
|
192 |
<pre class="code prettyprint">// Create new YUI instance, specify preferred languages, |
|
|
193 |
// and populate it with the required modules |
|
|
194 |
YUI({lang:"ar-MA,fr-FR"}).use('datatype-date', function(Y) { |
|
|
195 |
|
|
|
196 |
// DataType available, and hopefully localized into one of the preferred languages |
|
|
197 |
|
|
|
198 |
});</pre> |
|
|
199 |
|
|
|
200 |
|
|
|
201 |
<h4 id="appResources">Providing Resources to Modules</h4> |
|
|
202 |
|
|
|
203 |
<p>In some cases, a module is internationalized, but doesn't have a resource |
|
|
204 |
bundle for the desired language. It may however have specified the contents |
|
|
205 |
of the resource bundle needed. In such a case, the application can register |
|
|
206 |
a resource bundle for its language with the Internationalization utility and |
|
|
207 |
set the language of that module.</p> |
|
|
208 |
|
|
|
209 |
<p>For example, date formatting in the DataType utility has support for a |
|
|
210 |
large number of languages built in, but Punjabi is not one of them. If |
|
|
211 |
you need date formatting for Punjabi, you can provide a resource bundle for this |
|
|
212 |
language (see the <a href="../datatype/index.html#addDateFormat">DataType</a> |
|
|
213 |
documentation for information on the contents of the resource bundle):</p> |
|
|
214 |
|
|
|
215 |
<pre class="code prettyprint">YUI().use("intl", "datatype-date-format", function(Y) { |
|
|
216 |
// provide data for Punjabi in India |
|
|
217 |
Y.Intl.add("datatype-date-format", "pa-IN", { |
|
|
218 |
"a":["ਐਤ.","ਸੋਮ.","ਮੰਗਲ.","ਬੁਧ.","ਵੀਰ.","ਸ਼ੁਕਰ.","ਸ਼ਨੀ."], |
|
|
219 |
"A":["ਐਤਵਾਰ","ਸੋਮਵਾਰ","ਮੰਗਲਵਾਰ","ਬੁਧਵਾਰ","ਵੀਰਵਾਰ","ਸ਼ੁੱਕਰਵਾਰ","ਸ਼ਨੀਚਰਵਾਰ"], |
|
|
220 |
"b":["ਜਨਵਰੀ","ਫ਼ਰਵਰੀ","ਮਾਰਚ","ਅਪ੍ਰੈਲ","ਮਈ","ਜੂਨ","ਜੁਲਾਈ","ਅਗਸਤ","ਸਤੰਬਰ","ਅਕਤੂਬਰ","ਨਵੰਬਰ","ਦਸੰਬਰ"], |
|
|
221 |
"B":["ਜਨਵਰੀ","ਫ਼ਰਵਰੀ","ਮਾਰਚ","ਅਪ੍ਰੈਲ","ਮਈ","ਜੂਨ","ਜੁਲਾਈ","ਅਗਸਤ","ਸਤੰਬਰ","ਅਕਤੂਬਰ","ਨਵੰਬਰ","ਦਸੰਬਰ"], |
|
|
222 |
"c":"%a, %Y %b %d %l:%M:%S %p %Z", |
|
|
223 |
"p":["ਸਵੇਰੇ","ਸ਼ਾਮ"], |
|
|
224 |
"P":["ਸਵੇਰੇ","ਸ਼ਾਮ"], |
|
|
225 |
"x":"%d/%m/%Y", |
|
|
226 |
"X":"%l:%M:%S %p" |
|
|
227 |
}); |
|
|
228 |
// switch to Punjabi |
|
|
229 |
Y.Intl.setLang("datatype-date-format", "pa-IN"); |
|
|
230 |
// now dates are formatted in Punjabi |
|
|
231 |
alert(Y.DataType.Date.format(new Date(), {format:"%A %x %X"})); |
|
|
232 |
});</pre> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
<h4 id="switchingLangs">Switching Languages</h4> |
|
|
236 |
|
|
|
237 |
<p>Some applications let the user change the user interface language on the fly. |
|
|
238 |
The Internationalization utility offers some low-level support for this:</p> |
|
|
239 |
|
|
|
240 |
<ul> |
|
|
241 |
<li>Applications that want to make the languages offered reflect the actually |
|
|
242 |
available languages in one or more modules can obtain the necessary |
|
|
243 |
information from <code>Intl.getAvailableLangs</code>. |
|
|
244 |
<li>Once a new language has been selected, the application can load the |
|
|
245 |
required resource bundles and call <code>Intl.setLang</code> to |
|
|
246 |
switch localizable modules to the new language. |
|
|
247 |
<li>Modules that have language sensitive behavior, whether relying on |
|
|
248 |
their own resource bundles or on other modules', can listen to |
|
|
249 |
<code>intl:langChange</code> events to find out about language changes. |
|
|
250 |
</ul> |
|
|
251 |
|
|
|
252 |
<p>The <a href="../datatype/datatype-dateformat-lang.html">Formatting |
|
|
253 |
Dates Using Language Resource Bundles</a> example shows how to use these interfaces.</p> |
|
|
254 |
|
|
|
255 |
<h3 id="modules">Internationalizing Modules</h3> |
|
|
256 |
|
|
|
257 |
<h4 id="externalizing">Externalizing Resources</h4> |
|
|
258 |
|
|
|
259 |
<p>Externalization means moving all language-sensitive data into external data files, |
|
|
260 |
also known as "resource bundles". Most of this data will be user interface strings |
|
|
261 |
that have to be translated, but there may also be patterns strings, font names, or |
|
|
262 |
other items. Resource bundles store this data as simple key-value pairs.</p> |
|
|
263 |
|
|
|
264 |
<p>The first resource bundle you always have to provide for an internationalized module |
|
|
265 |
is the root bundle, identified by the empty language tag "" and using the bundle name |
|
|
266 |
<code>lang/<i>module</i></code>. |
|
|
267 |
|
|
|
268 |
This is the bundle that will be used when an application requests a language that your module does |
|
|
269 |
not support. Additional languages are identified by their BCP 47 language tags, and their resource |
|
|
270 |
bundles use the names <code>lang/<i>module</i>_<i>language</i></code>.</p> |
|
|
271 |
|
|
|
272 |
<p>If you've used resource bundles in Java or other internationalization libraries, |
|
|
273 |
you may be familiar with the fallback mechanisms in their ResourceBundle classes. |
|
|
274 |
These do not exist in YUI, so that the loader doesn't have to load multiple bundles. |
|
|
275 |
As a consequence, each YUI resource bundle must provide the complete set of key-value |
|
|
276 |
pairs that the module needs.</p> |
|
|
277 |
|
|
|
278 |
<p>YUI currently supports two source formats for resource bundles: JSON-style |
|
|
279 |
JavaScript source files, and Yahoo Resource Bundle format.</p> |
|
|
280 |
|
|
|
281 |
<p>In JSON-style format, a resource bundle is a simple object whose properties |
|
|
282 |
represent the bundle's key-value pairs. Source files use the JavaScript suffix |
|
|
283 |
".js" and can contain comments, so that you can provide localizers with the |
|
|
284 |
information they need for correct localization. |
|
|
285 |
Here is a family of JSON files providing the same set of strings in English, |
|
|
286 |
German, and simplified Chinese:</p> |
|
|
287 |
|
|
|
288 |
<table> |
|
|
289 |
<tr> |
|
|
290 |
<th></th> |
|
|
291 |
<th>English <span style="text-transform:none">(root)</span></th> |
|
|
292 |
<th>German<th>Simplified Chinese</th> |
|
|
293 |
</tr> |
|
|
294 |
<tr> |
|
|
295 |
<th>File</th> |
|
|
296 |
<td>greetings.js</td> |
|
|
297 |
<td>greetings_de.js</td> |
|
|
298 |
<td>greetings_zh-Hans.js</td> |
|
|
299 |
</tr> |
|
|
300 |
<tr> |
|
|
301 |
<th>Contents</th> |
|
|
302 |
<td> |
|
|
303 |
<pre class="code prettyprint">{ |
|
|
304 |
HELLO: "Hello!", |
|
|
305 |
GOODBYE: "Goodbye!" |
|
|
306 |
}</pre> |
|
|
307 |
|
|
|
308 |
</td> |
|
|
309 |
<td> |
|
|
310 |
<pre class="code prettyprint">{ |
|
|
311 |
HELLO: "Hallo!", |
|
|
312 |
GOODBYE: "Tschüß!" |
|
|
313 |
}</pre> |
|
|
314 |
|
|
|
315 |
</td> |
|
|
316 |
<td> |
|
|
317 |
<pre class="code prettyprint">{ |
|
|
318 |
HELLO: "你好!", |
|
|
319 |
GOODBYE: "再见!" |
|
|
320 |
}</pre> |
|
|
321 |
|
|
|
322 |
</td> |
|
|
323 |
</table> |
|
|
324 |
|
|
|
325 |
<p>The <a href="#yrb">Yahoo Resource Bundles format</a> is a simple |
|
|
326 |
text format for resource bundles that Yahoo open-sourced in 2009. |
|
|
327 |
It uses the file name suffix ".pres". |
|
|
328 |
Here are the same resource bundles as above in YRB format:</p> |
|
|
329 |
|
|
|
330 |
<table> |
|
|
331 |
<tr> |
|
|
332 |
<th></th> |
|
|
333 |
<th>English <span style="text-transform:none">(root)</span></th> |
|
|
334 |
<th>German</th> |
|
|
335 |
<th>Simplified Chinese</th> |
|
|
336 |
</tr> |
|
|
337 |
<tr> |
|
|
338 |
<th>File</th> |
|
|
339 |
<td>greetings.pres</td> |
|
|
340 |
<td>greetings_de.pres</td> |
|
|
341 |
<td>greetings_zh-Hans.pres</td> |
|
|
342 |
</tr> |
|
|
343 |
<tr> |
|
|
344 |
<th>Contents</th> |
|
|
345 |
<td> |
|
|
346 |
<pre class="code prettyprint">HELLO = Hello! |
|
|
347 |
GOODBYE = Goodbye!</pre> |
|
|
348 |
|
|
|
349 |
</td> |
|
|
350 |
<td> |
|
|
351 |
<pre class="code prettyprint">HELLO = Hallo! |
|
|
352 |
GOODBYE = Tschüß!</pre> |
|
|
353 |
|
|
|
354 |
</td> |
|
|
355 |
<td> |
|
|
356 |
<pre class="code prettyprint">HELLO = 你好! |
|
|
357 |
GOODBYE = 再见!</pre> |
|
|
358 |
|
|
|
359 |
</td> |
|
|
360 |
</tr> |
|
|
361 |
</table> |
|
|
362 |
|
|
|
363 |
<h4 id="packaging">Packaging Resources</h4> |
|
|
364 |
|
|
|
365 |
<p>The YUI loader expects resource bundles in a specific format. If you use <a href="http://yui.github.com/shifter/">Shifter</a> |
|
|
366 |
to build your module, resource bundles in JSON or YRB format will be automatically |
|
|
367 |
converted into the format expected by the loader. All you have to do is provide the source |
|
|
368 |
files in the <code>src/<i>module</i>/lang/</code> directory and add the <code>lang</code> keys |
|
|
369 |
to the JSON file under <code>src/<i>module</i>/meta/</code>. |
|
|
370 |
|
|
|
371 |
<p>If you use some other build process, you have to produce JavaScript files in the |
|
|
372 |
following format:</p> |
|
|
373 |
|
|
|
374 |
<pre class="code prettyprint">YUI.add("lang/greetings_zh-Hans", function(Y) { |
|
|
375 |
|
|
|
376 |
Y.Intl.add( |
|
|
377 |
|
|
|
378 |
"greetings", // associated module |
|
|
379 |
"zh-Hans", // BCP 47 language tag |
|
|
380 |
|
|
|
381 |
// key-value pairs for this module and language |
|
|
382 |
{ |
|
|
383 |
HELLO: "你好!", |
|
|
384 |
GOODBYE: "再见!" |
|
|
385 |
} |
|
|
386 |
); |
|
|
387 |
}, "3.10.3");</pre> |
|
|
388 |
|
|
|
389 |
|
|
|
390 |
<h4 id="specifying">Specifying Available Languages</h4> |
|
|
391 |
|
|
|
392 |
<p>The YUI loader also needs to be told that your module uses resource bundles, |
|
|
393 |
and for which languages it has resource bundles available. You provide this |
|
|
394 |
information as the <code>lang</code> property of the module meta data:</p> |
|
|
395 |
|
|
|
396 |
<pre class="code prettyprint">modules: { |
|
|
397 |
"greetings": { |
|
|
398 |
lang: ["de", "zh-Hans"] |
|
|
399 |
} |
|
|
400 |
}</pre> |
|
|
401 |
|
|
|
402 |
|
|
|
403 |
<h4 id="obtaining">Obtaining Resources</h4> |
|
|
404 |
|
|
|
405 |
<p>To access its resources, a module simply calls <code>Intl.get</code> with its |
|
|
406 |
module name. When instantiating YUI, the application will have requested its |
|
|
407 |
user interface language, so <code>Intl.get</code> will return the appropriate |
|
|
408 |
localized resource bundle.</p> |
|
|
409 |
|
|
|
410 |
<pre class="code prettyprint">function Greetings() { |
|
|
411 |
// Get localized strings in the current language |
|
|
412 |
this.resources = Y.Intl.get("greetings"); |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
Greetings.prototype = { |
|
|
416 |
|
|
|
417 |
hello: function() { |
|
|
418 |
return this.resources.HELLO; |
|
|
419 |
}, |
|
|
420 |
|
|
|
421 |
goodbye: function() { |
|
|
422 |
return this.resources.GOODBYE; |
|
|
423 |
} |
|
|
424 |
}</pre> |
|
|
425 |
|
|
|
426 |
|
|
|
427 |
<h3 id="yrb">Yahoo Resource Bundle Format</h3> |
|
|
428 |
|
|
|
429 |
<p>The Yahoo Resource Bundle (YRB) format is a simple text format for |
|
|
430 |
resource bundles. It's similar to Java properties files, but based |
|
|
431 |
on UTF-8 and with additional heredoc support.</p> |
|
|
432 |
|
|
|
433 |
<ul> |
|
|
434 |
<li>Files are encoded in UTF-8. The first line may be prefixed with a byte order mark (BOM).</li> |
|
|
435 |
<li>Lines whose first non-whitespace character is “#” are comment lines and are ignored.</li> |
|
|
436 |
<li>Lines that contain only whitespace characters and are not part of a heredoc string are ignored.</li> |
|
|
437 |
<li>Key-value definitions come in two forms: |
|
|
438 |
<ul> |
|
|
439 |
<li>The simple form has a key string, followed by “=”, followed by the value, all on one line. |
|
|
440 |
The tokens may or may not be surrounded by whitespace characters. Leading and trailing |
|
|
441 |
whitespace is trimmed from both key and value. The value cannot start with "<<<"; |
|
|
442 |
for values starting with this character sequence, use the heredoc form. |
|
|
443 |
</li> |
|
|
444 |
<li>The heredoc form starts with a key string, followed by “=”, followed by “<<<”, |
|
|
445 |
followed by an identifier, all on one line. |
|
|
446 |
The tokens may or may not be surrounded by whitespace characters |
|
|
447 |
Leading and trailing whitespace is trimmed from both key and identifier. |
|
|
448 |
The heredoc form ends with a termination line that contains only the identifier, |
|
|
449 |
possibly followed by a semicolon. |
|
|
450 |
The lines between these two lines, except comment lines, form the heredoc string. |
|
|
451 |
The line break before the termination line is removed, all other line breaks are preserved. |
|
|
452 |
</li> |
|
|
453 |
</ul> |
|
|
454 |
</li> |
|
|
455 |
<li>Lines that are not comment lines, whitespace lines, or part of a key-value definition are illegal.</li> |
|
|
456 |
<li>The following escape sequences are recognized in values: |
|
|
457 |
<ul> |
|
|
458 |
<li>“\\” stands for “\”.</li> |
|
|
459 |
<li>“\n” stands for the newline character, U+000A.</li> |
|
|
460 |
<li>“\t” stands for the horizontal tab character, U+0009.</li> |
|
|
461 |
<li>“\ ” stands for the space character, U+0020. This is only needed if the value of a key-value |
|
|
462 |
pair starts or ends with a space character.</li> |
|
|
463 |
<li>“\#” stands for the number sign character, U+0023. This is only needed if a line within a |
|
|
464 |
heredoc string starts with this character.</li> |
|
|
465 |
</ul> |
|
|
466 |
</li> |
|
|
467 |
<li>A sequence of “\” followed by a character not listed above is illegal. |
|
|
468 |
A “\” immediately preceding the end of the file is illegal.</li> |
|
|
469 |
<li>Only the characters horizontal tab, U+0009, and space, U+0020, are considered whitespace.</li> |
|
|
470 |
</ul> |
|
|
471 |
</div> |
|
|
472 |
</div> |
|
|
473 |
</div> |
|
|
474 |
|
|
|
475 |
<div class="yui3-u-1-4"> |
|
|
476 |
<div class="sidebar"> |
|
|
477 |
|
|
|
478 |
<div id="toc" class="sidebox"> |
|
|
479 |
<div class="hd"> |
|
|
480 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
481 |
</div> |
|
|
482 |
|
|
|
483 |
<div class="bd"> |
|
|
484 |
<ul class="toc"> |
|
|
485 |
<li> |
|
|
486 |
<a href="#scenarios">Usage Scenarios for the Internationalization Utility</a> |
|
|
487 |
<ul class="toc"> |
|
|
488 |
<li> |
|
|
489 |
<a href="#monolingualApps">Monolingual Applications</a> |
|
|
490 |
</li> |
|
|
491 |
<li> |
|
|
492 |
<a href="#multilingualApps">Multilingual Applications</a> |
|
|
493 |
</li> |
|
|
494 |
<li> |
|
|
495 |
<a href="#intlModules">Internationalized Modules</a> |
|
|
496 |
</li> |
|
|
497 |
</ul> |
|
|
498 |
</li> |
|
|
499 |
<li> |
|
|
500 |
<a href="#getting-started">Getting Started</a> |
|
|
501 |
</li> |
|
|
502 |
<li> |
|
|
503 |
<a href="#using">Using the Internationalization Utility</a> |
|
|
504 |
<ul class="toc"> |
|
|
505 |
<li> |
|
|
506 |
<a href="#bcp47">Using BCP 47 Language Tags</a> |
|
|
507 |
</li> |
|
|
508 |
<li> |
|
|
509 |
<a href="#applications">Internationalizing Applications</a> |
|
|
510 |
<ul class="toc"> |
|
|
511 |
<li> |
|
|
512 |
<a href="#prefLang">Requesting Preferred Languages</a> |
|
|
513 |
</li> |
|
|
514 |
<li> |
|
|
515 |
<a href="#appResources">Providing Resources to Modules</a> |
|
|
516 |
</li> |
|
|
517 |
<li> |
|
|
518 |
<a href="#switchingLangs">Switching Languages</a> |
|
|
519 |
</li> |
|
|
520 |
</ul> |
|
|
521 |
</li> |
|
|
522 |
<li> |
|
|
523 |
<a href="#modules">Internationalizing Modules</a> |
|
|
524 |
<ul class="toc"> |
|
|
525 |
<li> |
|
|
526 |
<a href="#externalizing">Externalizing Resources</a> |
|
|
527 |
</li> |
|
|
528 |
<li> |
|
|
529 |
<a href="#packaging">Packaging Resources</a> |
|
|
530 |
</li> |
|
|
531 |
<li> |
|
|
532 |
<a href="#specifying">Specifying Available Languages</a> |
|
|
533 |
</li> |
|
|
534 |
<li> |
|
|
535 |
<a href="#obtaining">Obtaining Resources</a> |
|
|
536 |
</li> |
|
|
537 |
</ul> |
|
|
538 |
</li> |
|
|
539 |
<li> |
|
|
540 |
<a href="#yrb">Yahoo Resource Bundle Format</a> |
|
|
541 |
</li> |
|
|
542 |
</ul> |
|
|
543 |
</li> |
|
|
544 |
</ul> |
|
|
545 |
</div> |
|
|
546 |
</div> |
|
|
547 |
|
|
|
548 |
|
|
|
549 |
|
|
|
550 |
<div class="sidebox"> |
|
|
551 |
<div class="hd"> |
|
|
552 |
<h2 class="no-toc">Examples</h2> |
|
|
553 |
</div> |
|
|
554 |
|
|
|
555 |
<div class="bd"> |
|
|
556 |
<ul class="examples"> |
|
|
557 |
|
|
|
558 |
|
|
|
559 |
<li data-description="How to create components which use language resource bundles."> |
|
|
560 |
<a href="intl-basic.html">Language Resource Bundles</a> |
|
|
561 |
</li> |
|
|
562 |
|
|
|
563 |
|
|
|
564 |
|
|
|
565 |
|
|
|
566 |
</ul> |
|
|
567 |
</div> |
|
|
568 |
</div> |
|
|
569 |
|
|
|
570 |
|
|
|
571 |
|
|
|
572 |
<div class="sidebox"> |
|
|
573 |
<div class="hd"> |
|
|
574 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
575 |
</div> |
|
|
576 |
|
|
|
577 |
<div class="bd"> |
|
|
578 |
<ul class="examples"> |
|
|
579 |
|
|
|
580 |
|
|
|
581 |
|
|
|
582 |
|
|
|
583 |
<li data-description="Formatting dates into strings using pre-packaged language resource bundles."> |
|
|
584 |
<a href="../datatype/datatype-dateformat-lang.html">Formatting Dates Using Language Resource Bundles</a> |
|
|
585 |
</li> |
|
|
586 |
|
|
|
587 |
|
|
|
588 |
</ul> |
|
|
589 |
</div> |
|
|
590 |
</div> |
|
|
591 |
|
|
|
592 |
</div> |
|
|
593 |
</div> |
|
|
594 |
</div> |
|
|
595 |
</div> |
|
|
596 |
|
|
|
597 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
598 |
<script>prettyPrint();</script> |
|
|
599 |
|
|
|
600 |
<script> |
|
|
601 |
YUI.Env.Tests = { |
|
|
602 |
examples: [], |
|
|
603 |
project: '../assets', |
|
|
604 |
assets: '../assets/intl', |
|
|
605 |
name: 'intl', |
|
|
606 |
title: 'Internationalization', |
|
|
607 |
newWindow: '', |
|
|
608 |
auto: false |
|
|
609 |
}; |
|
|
610 |
YUI.Env.Tests.examples.push('intl-basic'); |
|
|
611 |
YUI.Env.Tests.examples.push('datatype-dateformat-lang'); |
|
|
612 |
|
|
|
613 |
</script> |
|
|
614 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
615 |
|
|
|
616 |
|
|
|
617 |
|
|
|
618 |
</body> |
|
|
619 |
</html> |