|
1 <!DOCTYPE html> |
|
2 <html lang="en"> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Example: Language Resource Bundles</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 |
|
24 <h1>Example: Language Resource Bundles</h1> |
|
25 <div class="yui3-g"> |
|
26 <div class="yui3-u-3-4"> |
|
27 <div id="main"> |
|
28 <div class="content"><style scoped> |
|
29 #out .word { |
|
30 margin:0.3em 0.3em 0.3em 1em; |
|
31 } |
|
32 |
|
33 #out .speaking { |
|
34 font-size:108%; |
|
35 color:#00aa00; |
|
36 margin-top:1em; |
|
37 margin-bottom:1em; |
|
38 } |
|
39 </style> |
|
40 |
|
41 <div class="intro"> |
|
42 <p>This example shows how you can define language resource bundles for your custom module implementations; it also illustrates how YUI Loader can load the correct bundle based on the language you've chosen for your YUI instance.</p> |
|
43 </div> |
|
44 |
|
45 <div class="example"> |
|
46 <div id="out"></div> |
|
47 |
|
48 <script> |
|
49 (function() { |
|
50 |
|
51 var say = function(msg, node, cls) { |
|
52 node.append('<p class="' + cls + '">' + msg + '</p>'); |
|
53 }; |
|
54 |
|
55 var appMetaData = { |
|
56 myapp: { |
|
57 base: '../assets/intl/', |
|
58 modules : { |
|
59 "translator" : { |
|
60 path: 'translator/translator.js', |
|
61 lang: ["en", "fr", "es"] |
|
62 } |
|
63 } |
|
64 } |
|
65 }; |
|
66 |
|
67 YUI({ |
|
68 lang:"en", |
|
69 groups: appMetaData |
|
70 }).use("node-base", "translator", function(Y) { |
|
71 var translator = new Y.Translator(), |
|
72 out = Y.one("#out"); |
|
73 |
|
74 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
75 say(translator.hi(), out, "word"); |
|
76 say(translator.bye(), out, "word"); |
|
77 }); |
|
78 |
|
79 YUI({ |
|
80 lang:"fr", |
|
81 groups: appMetaData |
|
82 }).use("node-base", "translator", function(Y) { |
|
83 var translator = new Y.Translator(), |
|
84 out = Y.one("#out"); |
|
85 |
|
86 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
87 say(translator.hi(), out, "word"); |
|
88 say(translator.bye(), out, "word"); |
|
89 }); |
|
90 |
|
91 YUI({ |
|
92 lang:"es", |
|
93 groups: appMetaData |
|
94 }).use("node-base", "translator", function(Y) { |
|
95 var translator = new Y.Translator(), |
|
96 out = Y.one("#out"); |
|
97 |
|
98 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
99 say(translator.hi(), out, "word"); |
|
100 say(translator.bye(), out, "word"); |
|
101 }); |
|
102 |
|
103 }()); |
|
104 </script> |
|
105 </div> |
|
106 |
|
107 <h3>Defining your Custom Module</h3> |
|
108 |
|
109 <p>We use Loader's groups support to add a custom module called "translator" under the group "myapp". The "lang" property in the module's metadata specifies which set of languages it supports.</p> |
|
110 |
|
111 <pre class="code prettyprint">var appMetaData = { |
|
112 myapp: { |
|
113 base: '../assets/intl', |
|
114 modules : { |
|
115 "translator" : { |
|
116 path: 'translator/translator.js', |
|
117 lang: ["en", "fr", "es"] |
|
118 } |
|
119 } |
|
120 } |
|
121 }; |
|
122 |
|
123 YUI({ |
|
124 lang:'fr', |
|
125 groups: appMetaData |
|
126 }).use(...);</pre> |
|
127 |
|
128 <p>NOTE: Since this example is hosted on a page with other YUI instances, we don't want to pollute their configuration, so we just pass our <code>groups: appMetaData</code> configuration property to each YUI instance we create as shown above.</p> |
|
129 <p>If you own all YUI instances on the page, you can use the global <code>YUI_Config</code> variable to define a global configuration for all YUI instances on the page, to avoid passing the same meta-data to all your instances as shown below:</p> |
|
130 <pre class="code prettyprint">var YUI_Config = { |
|
131 groups: { |
|
132 myapp: { |
|
133 base: '../assets/intl', |
|
134 modules : { |
|
135 "translator" : { |
|
136 path: 'translator/translator.js', |
|
137 lang: ["en", "fr", "es"] |
|
138 } |
|
139 } |
|
140 } |
|
141 } |
|
142 }; |
|
143 |
|
144 YUI({ |
|
145 lang:'fr' |
|
146 }).use(...);</pre> |
|
147 |
|
148 |
|
149 <h3>What Language Resource Bundles Look Like</h3> |
|
150 |
|
151 <p>The language resource bundles for any module follows the pattern below:</p> |
|
152 |
|
153 <pre class="code prettyprint">YUI.add("lang/translator_fr", function(Y) { |
|
154 |
|
155 Y.Intl.add( |
|
156 |
|
157 "translator", // Associated Module |
|
158 "fr", // BCP 47 Language Tag |
|
159 |
|
160 { // Translated String Key/Value Pairs |
|
161 hello:"Bonjour", |
|
162 goodbye: "Au revoir" |
|
163 } |
|
164 |
|
165 ); |
|
166 |
|
167 }, "3.10.3");</pre> |
|
168 |
|
169 |
|
170 <p>The <code>"lang/[for-module]_[lang]"</code> passed to <code>YUI.add</code> is the default module name used for language resource bundles, and the <code>Y.Intl.add</code> method is used to register the string name/value pair hash for a given module and language combination. |
|
171 |
|
172 <h3>Generating Language Resource Bundles</h3> |
|
173 |
|
174 <p><a href="http://yui.github.com/shifter">Shifter</a> will handle the creation of the boiler plate code shown above, from the raw language files found in the module's <code>src/[module]/lang</code> subdirectory. The raw files under the <code>lang</code> directory contain just the string name/value pairs for each language.</p> |
|
175 |
|
176 <p>Provide the raw string name/value pairs in the <code>src/[component]/lang</code> subdirectory in your component's source area:</p> |
|
177 |
|
178 <pre class="code prettyprint">// Contents of the raw src/[component]/lang/[component]_fr.js file |
|
179 { |
|
180 hello:"Bonjour", |
|
181 goodbye: "Au revoir" |
|
182 }</pre> |
|
183 |
|
184 |
|
185 <p>And whenever you build your component code, the language resource bundles will be built and deployed too.</p> |
|
186 |
|
187 <p>You can checkout the <a href="http://github.com/yui/yui3"></a>YUI 3 Source Code</code> and see the source code and build configuration files for the "console" and "datatype-date-format" modules to see a concrete example of this.</p> |
|
188 |
|
189 <h3>Accessing Localized Resources In Your Class</h3> |
|
190 |
|
191 <p>The Translator class implementation gets access to the localized strings by using <code>Y.Intl.get</code>, passing in the module name whose strings we need access to:</p> |
|
192 |
|
193 <pre class="code prettyprint">function Translator() { |
|
194 // Get localized strings in the current language |
|
195 this._strs = Y.Intl.get("translator"); |
|
196 } |
|
197 |
|
198 Translator.prototype = { |
|
199 |
|
200 hi : function() { |
|
201 return this._strs.hello; |
|
202 }, |
|
203 |
|
204 bye : function() { |
|
205 return this._strs.goodbye; |
|
206 } |
|
207 |
|
208 ... |
|
209 }</pre> |
|
210 |
|
211 |
|
212 |
|
213 <h3>Specifying the Language for an Instance</h3> |
|
214 |
|
215 <p>We specify the language to use for each instance, using the "lang" configuration property for the instance.</p> |
|
216 |
|
217 <h4> An English instance</h4> |
|
218 |
|
219 <pre class="code prettyprint">YUI({ |
|
220 lang:"en", |
|
221 ... |
|
222 }).use("node-base", "translator", function(Y) { |
|
223 var translator = new Y.Translator(), |
|
224 out = Y.one("#out"); |
|
225 |
|
226 say("Speaking in: " + Y.Intl.getLang("translator"), out); |
|
227 say(translator.hi(), out); |
|
228 say(translator.bye(), out); |
|
229 });</pre> |
|
230 |
|
231 |
|
232 <h4>A French YUI Instance</h4> |
|
233 |
|
234 <pre class="code prettyprint">YUI({ |
|
235 lang:"fr", |
|
236 ... |
|
237 }).use("node-base", "translator", function(Y) { |
|
238 ... |
|
239 });</pre> |
|
240 |
|
241 |
|
242 <h4>A Spanish YUI Instance</h4> |
|
243 |
|
244 <pre class="code prettyprint">YUI({ |
|
245 lang:"es", |
|
246 ... |
|
247 }).use("node-base", "translator", function(Y) { |
|
248 ... |
|
249 });</pre> |
|
250 |
|
251 |
|
252 <h3>Modules Shipping With Language Resource Bundles</h3> |
|
253 |
|
254 <p>As mentioned above, the <code>datatype</code> module (specifically the <code>datatype-date-format</code> module) and <code>console</code> are shipped with language resource bundles. Datatype ships with over 50 different languages supported, and Console ships with en and es language resource bundles, mainly as a demonstration of how language resource bundles are defined and used for Widget development.</p> |
|
255 |
|
256 <h2>Complete Example Source</h2> |
|
257 <pre class="code prettyprint"><div id="out"></div> |
|
258 |
|
259 <script> |
|
260 (function() { |
|
261 |
|
262 var say = function(msg, node, cls) { |
|
263 node.append('<p class="' + cls + '">' + msg + '</p>'); |
|
264 }; |
|
265 |
|
266 var appMetaData = { |
|
267 myapp: { |
|
268 base: '../assets/intl/', |
|
269 modules : { |
|
270 "translator" : { |
|
271 path: 'translator/translator.js', |
|
272 lang: ["en", "fr", "es"] |
|
273 } |
|
274 } |
|
275 } |
|
276 }; |
|
277 |
|
278 YUI({ |
|
279 lang:"en", |
|
280 groups: appMetaData |
|
281 }).use("node-base", "translator", function(Y) { |
|
282 var translator = new Y.Translator(), |
|
283 out = Y.one("#out"); |
|
284 |
|
285 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
286 say(translator.hi(), out, "word"); |
|
287 say(translator.bye(), out, "word"); |
|
288 }); |
|
289 |
|
290 YUI({ |
|
291 lang:"fr", |
|
292 groups: appMetaData |
|
293 }).use("node-base", "translator", function(Y) { |
|
294 var translator = new Y.Translator(), |
|
295 out = Y.one("#out"); |
|
296 |
|
297 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
298 say(translator.hi(), out, "word"); |
|
299 say(translator.bye(), out, "word"); |
|
300 }); |
|
301 |
|
302 YUI({ |
|
303 lang:"es", |
|
304 groups: appMetaData |
|
305 }).use("node-base", "translator", function(Y) { |
|
306 var translator = new Y.Translator(), |
|
307 out = Y.one("#out"); |
|
308 |
|
309 say("Speaking in: " + Y.Intl.getLang("translator"), out, "speaking"); |
|
310 say(translator.hi(), out, "word"); |
|
311 say(translator.bye(), out, "word"); |
|
312 }); |
|
313 |
|
314 }()); |
|
315 </script></pre> |
|
316 |
|
317 </div> |
|
318 </div> |
|
319 </div> |
|
320 |
|
321 <div class="yui3-u-1-4"> |
|
322 <div class="sidebar"> |
|
323 |
|
324 |
|
325 |
|
326 <div class="sidebox"> |
|
327 <div class="hd"> |
|
328 <h2 class="no-toc">Examples</h2> |
|
329 </div> |
|
330 |
|
331 <div class="bd"> |
|
332 <ul class="examples"> |
|
333 |
|
334 |
|
335 <li data-description="How to create components which use language resource bundles."> |
|
336 <a href="intl-basic.html">Language Resource Bundles</a> |
|
337 </li> |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 </ul> |
|
343 </div> |
|
344 </div> |
|
345 |
|
346 |
|
347 |
|
348 <div class="sidebox"> |
|
349 <div class="hd"> |
|
350 <h2 class="no-toc">Examples That Use This Component</h2> |
|
351 </div> |
|
352 |
|
353 <div class="bd"> |
|
354 <ul class="examples"> |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 <li data-description="Formatting dates into strings using pre-packaged language resource bundles."> |
|
360 <a href="../datatype/datatype-dateformat-lang.html">Formatting Dates Using Language Resource Bundles</a> |
|
361 </li> |
|
362 |
|
363 |
|
364 </ul> |
|
365 </div> |
|
366 </div> |
|
367 |
|
368 </div> |
|
369 </div> |
|
370 </div> |
|
371 </div> |
|
372 |
|
373 <script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
374 <script>prettyPrint();</script> |
|
375 |
|
376 <script> |
|
377 YUI.Env.Tests = { |
|
378 examples: [], |
|
379 project: '../assets', |
|
380 assets: '../assets/intl', |
|
381 name: 'intl-basic', |
|
382 title: 'Language Resource Bundles', |
|
383 newWindow: '', |
|
384 auto: false |
|
385 }; |
|
386 YUI.Env.Tests.examples.push('intl-basic'); |
|
387 YUI.Env.Tests.examples.push('datatype-dateformat-lang'); |
|
388 |
|
389 </script> |
|
390 <script src="../assets/yui/test-runner.js"></script> |
|
391 |
|
392 |
|
393 |
|
394 </body> |
|
395 </html> |