YUI 3.x Home -

YUI Library Examples: Internationalization: Language Resource Bundles

Internationalization: Language Resource Bundles

This example shows how you can define language resource bundles for your custom module implementations, and have loader load them based on the language chosen for your YUI instance.

Defining your Custom Module

We use Loader's newly added groups support to add a custom module called "translator" under the group "myapp". We set this configuration using YUI_config, so it's applied to all our YUI instances. The "lang" property in the module's meta-data specifies which set of languages it supports.

  1. YUI_config = {
  2. filter:"raw",
  3. groups: {
  4. myapp: {
  5. base: 'assets/',
  6. modules : {
  7. "translator" : {
  8. lang: ["en", "fr", "es"]
  9. }
  10. }
  11. }
  12. }
  13. };
    YUI_config = {
        filter:"raw",
        groups: {
            myapp: {
                base: 'assets/',
                modules : {
                    "translator" : {
                        lang: ["en", "fr", "es"]
                    }
                }
            }
        }
    };

What Language Resource Bundles Look Like

The language resource bundles for any module follows the pattern below:

  1. YUI.add("lang/translator_fr", function(Y) {
  2.  
  3. Y.Intl.add(
  4.  
  5. "translator", // Associated Module
  6. "fr", // BCP 47 Language Tag
  7.  
  8. { // Translated String Key/Value Pairs
  9. hello:"Bonjour",
  10. goodbye: "Au revoir"
  11. }
  12.  
  13. );
  14.  
  15. }, "3.1.0");
YUI.add("lang/translator_fr", function(Y) {
 
    Y.Intl.add(
 
        "translator",     // Associated Module 
        "fr",             // BCP 47 Language Tag
 
        {                 // Translated String Key/Value Pairs 
            hello:"Bonjour",   
            goodbye: "Au revoir"
        }
 
    );
 
}, "3.1.0");

The "lang/[for-module]_[lang]" passed to YUI.add is the default module name used for language resource bundles, and the Y.Intl.add method is used to register the string name/value pair hash for a given module and language combination.

Generating Language Resource Bundles

YUI Builder will handle the creation of the boiler plate code shown above, from the raw language files found in the module's src/[module]/lang subdirectory. The raw files under the lang directory contain just the string name/value pairs for each language.

As the component developer, the only thing you need to do is specify the component.lang property in the build properties file:

  1. # In your component's build properties file (src/[module]/build.properties for example):
  2. component.lang=en,fr,es
# In your component's build properties file (src/[module]/build.properties for example):
component.lang=en,fr,es

Provide the raw string name/value pairs in the src/[component]/lang subdirectory in your component's source area:

  1. // Contents of the raw src/[component]/lang/[component]_fr.js file
  2. {
  3. hello:"Bonjour",
  4. goodbye: "Au revoir"
  5. }
// Contents of the raw src/[component]/lang/[component]_fr.js file
{
    hello:"Bonjour",   
    goodbye: "Au revoir"
}

And whenever you build your component code, the language resource bundles will be built and deployed too.

You can checkout the YUI 3 Source 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.

Accessing Localized Resources In Your Class

The Translator class implementation gets access to the localized strings by using Y.Intl.get, passing in the module name whose strings we need access to:

  1.  
  2. function Translator() {
  3. // Get localized strings in the current language
  4. this._strs = Y.Intl.get("translator");
  5. }
  6.  
  7. Translator.prototype = {
  8.  
  9. hi : function() {
  10. return this._strs.hello;
  11. },
  12.  
  13. bye : function() {
  14. return this._strs.goodbye;
  15. }
  16.  
  17. ...
  18. }
  19.  
 
    function Translator() {
        // Get localized strings in the current language
        this._strs = Y.Intl.get("translator");
    }
 
    Translator.prototype = {
 
        hi : function() {
            return this._strs.hello;
        },
 
        bye : function() {
            return this._strs.goodbye;
        }
 
        ...
    }
 

Specifying the Language for an Instance

We specify the language to use for each instance, using the "lang" configuration property for the instance.

An English instance
  1. YUI({
  2. lang:"en"
  3. }).use("node-base", "translator", function(Y) {
  4. var translator = new Y.Translator(),
  5. out = Y.one("#out");
  6.  
  7. say("Speaking in: " + Y.Intl.getLang("translator"), out);
  8. say(translator.hi(), out);
  9. say(translator.bye(), out);
  10. });
    YUI({
        lang:"en"
    }).use("node-base", "translator", function(Y) {
        var translator = new Y.Translator(),
            out = Y.one("#out");
 
        say("Speaking in: " + Y.Intl.getLang("translator"), out);
        say(translator.hi(), out);
        say(translator.bye(), out);
    });
A French YUI Instance
  1. YUI({
  2. lang:"fr"
  3. }).use("node-base", "translator", function(Y) {
  4. ...
  5. });
    YUI({
        lang:"fr"
    }).use("node-base", "translator", function(Y) {
        ...
    });
A Spanish YUI Instance
  1. YUI({
  2. lang:"es"
  3. }).use("node-base", "translator", function(Y) {
  4. ...
  5. });
    YUI({
        lang:"es"
    }).use("node-base", "translator", function(Y) {
        ...
    });

Modules Shipping With Language Resource Bundles in 3.1.0

As mentioned above, the datatype module (specifically the datatype-date-format module) and console are shipped in the 3.1.0 release 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.

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings