src/cm/media/js/lib/yui/yui_3.10.3/docs/intl/intl-basic.html
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     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: &#x27;..&#x2F;assets&#x2F;intl&#x27;,
       
   114         modules : {
       
   115             &quot;translator&quot; : {
       
   116                 path: &#x27;translator&#x2F;translator.js&#x27;,
       
   117                 lang: [&quot;en&quot;, &quot;fr&quot;, &quot;es&quot;]
       
   118             }
       
   119         }
       
   120     }
       
   121 };
       
   122 
       
   123 YUI({
       
   124     lang:&#x27;fr&#x27;,
       
   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: &#x27;..&#x2F;assets&#x2F;intl&#x27;,
       
   134             modules : {
       
   135                 &quot;translator&quot; : {
       
   136                     path: &#x27;translator&#x2F;translator.js&#x27;,
       
   137                     lang: [&quot;en&quot;, &quot;fr&quot;, &quot;es&quot;]
       
   138                 }
       
   139             }
       
   140         }
       
   141     }
       
   142 };
       
   143 
       
   144 YUI({
       
   145     lang:&#x27;fr&#x27;
       
   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(&quot;lang&#x2F;translator_fr&quot;, function(Y) {
       
   154 
       
   155     Y.Intl.add(
       
   156 
       
   157         &quot;translator&quot;,     &#x2F;&#x2F; Associated Module
       
   158         &quot;fr&quot;,             &#x2F;&#x2F; BCP 47 Language Tag
       
   159 
       
   160         {                 &#x2F;&#x2F; Translated String Key&#x2F;Value Pairs
       
   161             hello:&quot;Bonjour&quot;,
       
   162             goodbye: &quot;Au revoir&quot;
       
   163         }
       
   164 
       
   165     );
       
   166 
       
   167 }, &quot;3.10.3&quot;);</pre>
       
   168 
       
   169 
       
   170 <p>The <code>&quot;lang&#x2F;[for-module]_[lang]&quot;</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&#x2F;[module]&#x2F;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&#x2F;[component]&#x2F;lang</code> subdirectory in your component's source area:</p>
       
   177 
       
   178 <pre class="code prettyprint">&#x2F;&#x2F; Contents of the raw src&#x2F;[component]&#x2F;lang&#x2F;[component]_fr.js file
       
   179 {
       
   180     hello:&quot;Bonjour&quot;,
       
   181     goodbye: &quot;Au revoir&quot;
       
   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     &#x2F;&#x2F; Get localized strings in the current language
       
   195     this._strs = Y.Intl.get(&quot;translator&quot;);
       
   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:&quot;en&quot;,
       
   221     ...
       
   222 }).use(&quot;node-base&quot;, &quot;translator&quot;, function(Y) {
       
   223     var translator = new Y.Translator(),
       
   224         out = Y.one(&quot;#out&quot;);
       
   225 
       
   226     say(&quot;Speaking in: &quot; + Y.Intl.getLang(&quot;translator&quot;), 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:&quot;fr&quot;,
       
   236     ...
       
   237 }).use(&quot;node-base&quot;, &quot;translator&quot;, function(Y) {
       
   238     ...
       
   239 });</pre>
       
   240 
       
   241 
       
   242 <h4>A Spanish YUI Instance</h4>
       
   243 
       
   244 <pre class="code prettyprint">YUI({
       
   245     lang:&quot;es&quot;,
       
   246     ...
       
   247 }).use(&quot;node-base&quot;, &quot;translator&quot;, 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">&lt;div id=&quot;out&quot;&gt;&lt;&#x2F;div&gt;
       
   258 
       
   259 &lt;script&gt;
       
   260 (function() {
       
   261 
       
   262     var say = function(msg, node, cls) {
       
   263         node.append(&#x27;&lt;p class=&quot;&#x27; + cls + &#x27;&quot;&gt;&#x27; + msg + &#x27;&lt;&#x2F;p&gt;&#x27;);
       
   264     };
       
   265 
       
   266     var appMetaData = {
       
   267         myapp: {
       
   268             base: &#x27;..&#x2F;assets&#x2F;intl&#x2F;&#x27;,
       
   269             modules : {
       
   270                 &quot;translator&quot; : {
       
   271                     path: &#x27;translator&#x2F;translator.js&#x27;,
       
   272                     lang: [&quot;en&quot;, &quot;fr&quot;, &quot;es&quot;]
       
   273                 }
       
   274             }
       
   275         }
       
   276     };
       
   277 
       
   278     YUI({
       
   279         lang:&quot;en&quot;,
       
   280         groups: appMetaData
       
   281     }).use(&quot;node-base&quot;, &quot;translator&quot;, function(Y) {
       
   282         var translator = new Y.Translator(),
       
   283             out = Y.one(&quot;#out&quot;);
       
   284 
       
   285         say(&quot;Speaking in: &quot; + Y.Intl.getLang(&quot;translator&quot;), out, &quot;speaking&quot;);
       
   286         say(translator.hi(), out, &quot;word&quot;);
       
   287         say(translator.bye(), out, &quot;word&quot;);
       
   288     });
       
   289 
       
   290     YUI({
       
   291         lang:&quot;fr&quot;,
       
   292         groups: appMetaData
       
   293     }).use(&quot;node-base&quot;, &quot;translator&quot;, function(Y) {
       
   294         var translator = new Y.Translator(),
       
   295             out = Y.one(&quot;#out&quot;);
       
   296 
       
   297         say(&quot;Speaking in: &quot; + Y.Intl.getLang(&quot;translator&quot;), out, &quot;speaking&quot;);
       
   298         say(translator.hi(), out, &quot;word&quot;);
       
   299         say(translator.bye(), out, &quot;word&quot;);
       
   300     });
       
   301 
       
   302     YUI({
       
   303         lang:&quot;es&quot;,
       
   304         groups: appMetaData
       
   305     }).use(&quot;node-base&quot;, &quot;translator&quot;, function(Y) {
       
   306         var translator = new Y.Translator(),
       
   307             out = Y.one(&quot;#out&quot;);
       
   308 
       
   309         say(&quot;Speaking in: &quot; + Y.Intl.getLang(&quot;translator&quot;), out, &quot;speaking&quot;);
       
   310         say(translator.hi(), out, &quot;word&quot;);
       
   311         say(translator.bye(), out, &quot;word&quot;);
       
   312     });
       
   313 
       
   314 }());
       
   315 &lt;&#x2F;script&gt;</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>