Mashup Code Sample - Internationalization for Mashlets

 

Are you interested in Internationalizing you Mashlet?  Here's a quick way to do it using resource bundles.
The attached zip contains all the necessary files for an internationalized HelloWorld example.
 

 

Contents used for resource bundles:
1. mashlet-resource-bundles.js
2. presto-lang-es_MX.js (language file)
3. presto-lang-en_US.js (language file)

1. Place the mashlet-resource-bundles.js file at {PRESTO_TOMCAT}/webapps/static/mashlet/js/

Add the path to this file in mashltes.json as:

"resources": {

"js": [

{"script": "#{prestoResources}/mashlet/js/mashlet-resource-bundles.js"},

]

}

2. Place the language files anywhere on your webserver.

Language files should contain a JSON object with each property being a key for Resource Bundles.

Example of the Spanish and English file contents:
------------------------------------------------
Contents of Spanish file. (presto-lang-es_MX.js)
myBundles={
"title": "BIENVENIDO",
"hello": "!Hola Mundo!"
}

Contents of English file. (presto-lang-en_MUSjs)
myBundles={
"title": "WELCOME",
"hello": "Hello World!"
}

Using the Resource Bundles in Mashlets
To implement, follow this instructions:

1. Create a jsp and add this content to it. This code will grab the language setting of the browser.

 

<?php
@ page import="java.util.Locale" 
?>

 

<?php
@ page contentType="text/html;charset=UTF-8" language="java" 
?>

 

<?php
Locale lang = request.getLocale(); 
?>

 

....

2. Use the getResourceBundle() method in any label that needs to be translated.

Method signature: getResourceBundle(String map, String key)

Example:

From within the mashlet instance code, use the method to get the desired key.

var HelloWorld = Class.create(Ema.BoxMashlet, {

render: function() {

//This will print WELCOME and Hello World! in English or Spanish, depending on the browser's settings. Any other language will return undefined.
this.el.update("

"+ this.getResourceBundle("myBundles","title") +"

" + this.getResourceBundle("myBundles","hello"));

},

onRefresh: function() {
this.render();
}
});

 

0
Your rating: None