Hello World Internationalized Sample App
The Internationalized Hello World sample app:
- Launches a tab view when the app's icon is clicked.
- Invokes a Yahoo! Mail API from the launched view.
- Displays strings appropriate to the user's language preference..
Try It Out
See Quick Start to learn how to get the full source for this example, install it, and run it in Yahoo! Mail.
Changing your Language Preference
To try out this app's language features, you'll need to be able to switch your Yahoo! accounts language preference. To do that from within Yahoo! Mail:
- Click on the "Hi, {your username}" menu in the upper left hand side of the browser
- Select "Account Info"
- Scroll down to "Account Settings" and click "Set language, site, time zone"
- Switch your "Regional Site and Language" to, e.g. "Yahoo! France"
config.xml
This config.xml tells Yahoo! Mail to load your
app's views/main in a tab when the app's icon is
clicked. It also provides internationalized name and description tags
for different languages.
<?xml version="1.0"?> <openmail_app_config version="0"> <name>Hello World (i18n)</name> <description>Hello World, internationalized version</description> <data/> <events> <click> <action> <launch> <view>main</view> <target_zone>tab</target_zone> </launch> </action> </click> </events> <intl_values> <intl_strings intl="en-AU"> <name>Hello World</name> <description>Hello World, internationalized Version (en-AU)</description> <developer_name>Yahoo!</developer_name> <legal> <terms_of_service url="http://developer.yahoo.com/terms/"/> <privacy_policy url="http://info.yahoo.com/privacy/us/yahoo/devel/details.html"/> <landing_page url="http://developer.yahoo.com/"/> </legal> </intl_strings> <intl_strings intl="fr-FR"> <name>Bonjour monde</name> <description>Bonjour monde, version internationalisée (fr-FR)</description> </intl_strings> </intl_values> </openmail_app_config>
<?xml version="1.0"?> <openmail_app_config version="0"> <name>Hello World (i18n)</name> <description>Hello World, internationalized version</description> <data/> <events> <click> <action> <launch> <view>main</view> <target_zone>tab</target_zone> </launch> </action> </click> </events> <intl_values> <intl_strings intl="en-AU"> <name>Hello World</name> <description>Hello World, internationalized Version (en-AU)</description> <developer_name>Yahoo!</developer_name> <legal> <terms_of_service url="http://developer.yahoo.com/terms/"/> <privacy_policy url="http://info.yahoo.com/privacy/us/yahoo/devel/details.html"/> <landing_page url="http://developer.yahoo.com/"/> </legal> </intl_strings> <intl_strings intl="fr-FR"> <name>Bonjour monde</name> <description>Bonjour monde, version internationalisée (fr-FR)</description> </intl_strings> </intl_values> </openmail_app_config>
views/main
main demonstrates how to pull in the Yahoo! Mail
APIs and then use one of the API calls,
Mail.compose.
When the "Compose" button is clicked, the form invokes Mail.compose,
which will open the mail composer with some fields prepopulated from the
form based on the user's language preference.
fetchStrings fetches language-specific json based on the
user's language, then invokes layoutDOM to apply those
strings to the DOM, using both straight string substitution and template
Y.lang.sub
template substitution.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Hello World (i18n)</title> <script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script> <script src="http://mail.yimg.com/nq/om/api/1.1.19/om-min.js"></script> </head> <body> <p id="dailyFlood"></p> <p><textarea id="tabody" cols="60" rows="10"></textarea></p> <p> <button id="demobtn"></button> <input type="checkbox" value="rich" name="rich" id="richtext"/> <span id="checkboxTxt"></span> </p> <script type="text/javascript"> YUI().use("node-base", "io-base", "json", function(Y) { //Holds language-specific strings var STRINGS = {}; function init() { //compose button handler function demoCompose() { var bodyTxt = document.getElementById("tabody").value; var richText = document.getElementById("richtext").checked; var mailBlob = { to: STRINGS.sampleToAddress, cc: STRINGS.sampleCcAddress, bcc: STRINGS.sampleBccAddress, subject: STRINGS.sampleSubject, html: richText, body: bodyTxt }; openmail.Mail.compose(mailBlob); } //Plug values from STRINGS into DOM components. //Hook up button click handler. function layoutDOM() { var messageCount = Math.floor(Math.random()*20000), target = document.getElementById("dailyFlood"); target.innerHTML = Y.Lang.sub(STRINGS.dailyFloodInfo, {date : new Date(), number: messageCount}); target = document.getElementById("tabody"); target.innerHTML = STRINGS.textAreaPrompt; target = document.getElementById("demobtn"); target.innerHTML = STRINGS.composeButton; target = document.getElementById("checkboxTxt"); target.innerHTML = STRINGS.richTextButton; Y.on("click", demoCompose, "#demobtn"); } function onFetchStringsSuccess (transId, response, args) { if (response.responseText) { try { STRINGS = Y.JSON.parse(response.responseText); layoutDOM(); } catch (x) { alert("Hello World's initialization failed.. try again later"); openmail.Application.closeView(); } }else{ alert("Hello World failed to load resources.. try again later"); openmail.Application.closeView(); } } function onFetchStringsFailure(transId, response, args) { alert("Hello World failed to load resources, try again later"); openmail.Application.closeView(); } function fetchStrings(params) { //Default string resource bundle var url = "/yahoo/mail/assets/resources.json"; //user has language preference, use it for string definitions if(params.user.lang[0]){ url = "/yahoo/mail/assets/resources_" + params.user.lang[0] + ".json"; } var cfg = { on: { success: onFetchStringsSuccess, failure: onFetchStringsFailure } }; //Fetch the strings from url Y.io(url, cfg); } //getParameters will callback to fetchStrings asynchrnously, passing the //user's language preference as part of the callback parameters. openmail.Application.getParameters(fetchStrings); } Y.on("domready", init); }); </script> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Hello World (i18n)</title> <script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script> <script src="http://mail.yimg.com/nq/om/api/1.1.19/om-min.js"></script> </head> <body> <p id="dailyFlood"></p> <p><textarea id="tabody" cols="60" rows="10"></textarea></p> <p> <button id="demobtn"></button> <input type="checkbox" value="rich" name="rich" id="richtext"/> <span id="checkboxTxt"></span> </p> <script type="text/javascript"> YUI().use("node-base", "io-base", "json", function(Y) { //Holds language-specific strings var STRINGS = {}; function init() { //compose button handler function demoCompose() { var bodyTxt = document.getElementById("tabody").value; var richText = document.getElementById("richtext").checked; var mailBlob = { to: STRINGS.sampleToAddress, cc: STRINGS.sampleCcAddress, bcc: STRINGS.sampleBccAddress, subject: STRINGS.sampleSubject, html: richText, body: bodyTxt }; openmail.Mail.compose(mailBlob); } //Plug values from STRINGS into DOM components. //Hook up button click handler. function layoutDOM() { var messageCount = Math.floor(Math.random()*20000), target = document.getElementById("dailyFlood"); target.innerHTML = Y.Lang.sub(STRINGS.dailyFloodInfo, {date : new Date(), number: messageCount}); target = document.getElementById("tabody"); target.innerHTML = STRINGS.textAreaPrompt; target = document.getElementById("demobtn"); target.innerHTML = STRINGS.composeButton; target = document.getElementById("checkboxTxt"); target.innerHTML = STRINGS.richTextButton; Y.on("click", demoCompose, "#demobtn"); } function onFetchStringsSuccess (transId, response, args) { if (response.responseText) { try { STRINGS = Y.JSON.parse(response.responseText); layoutDOM(); } catch (x) { alert("Hello World's initialization failed.. try again later"); openmail.Application.closeView(); } }else{ alert("Hello World failed to load resources.. try again later"); openmail.Application.closeView(); } } function onFetchStringsFailure(transId, response, args) { alert("Hello World failed to load resources, try again later"); openmail.Application.closeView(); } function fetchStrings(params) { //Default string resource bundle var url = "/yahoo/mail/assets/resources.json"; //user has language preference, use it for string definitions if(params.user.lang[0]){ url = "/yahoo/mail/assets/resources_" + params.user.lang[0] + ".json"; } var cfg = { on: { success: onFetchStringsSuccess, failure: onFetchStringsFailure } }; //Fetch the strings from url Y.io(url, cfg); } //getParameters will callback to fetchStrings asynchrnously, passing the //user's language preference as part of the callback parameters. openmail.Application.getParameters(fetchStrings); } Y.on("domready", init); }); </script> </body> </html>
resources.json
This resources.json file provides default strings for
when there are no strings specific to the user's language preference..
{ "dailyFloodInfo": "It's {date}. You have {number} new messages.", "textAreaPrompt": "Enter some text and press the \"Compose\" button.", "composeButton": "Compose", "richTextButton": "Rich text", "sampleToAddress": "hello@to.com", "sampleCcAddress": "hello@cc.com", "sampleBccAddress": "hello@bcc.com", "sampleSubject": "hello world" }
{
"dailyFloodInfo": "It's {date}. You have {number} new messages.",
"textAreaPrompt": "Enter some text and press the \"Compose\" button.",
"composeButton": "Compose",
"richTextButton": "Rich text",
"sampleToAddress": "hello@to.com",
"sampleCcAddress": "hello@cc.com",
"sampleBccAddress": "hello@bcc.com",
"sampleSubject": "hello world"
}
resources_fr-FR.json
This resources_fr-FR.json file provides the French version of the strings.
{ "dailyFloodInfo": "Il est {date}. Vous avez {number} messages non lus.", "textAreaPrompt": "Entrez votre message et poussez \"Écrire\".", "composeButton": "Écrire", "richTextButton": "Texte riche", "sampleToAddress": "allo@à.fr", "sampleCcAddress": "allo@cc.fr", "sampleBccAddress": "allo@bcc.fr", "sampleSubject": "Bonjour le monde!" }
{
"dailyFloodInfo": "Il est {date}. Vous avez {number} messages non lus.",
"textAreaPrompt": "Entrez votre message et poussez \"Écrire\".",
"composeButton": "Écrire",
"richTextButton": "Texte riche",
"sampleToAddress": "allo@à.fr",
"sampleCcAddress": "allo@cc.fr",
"sampleBccAddress": "allo@bcc.fr",
"sampleSubject": "Bonjour le monde!"
}
resources_en-AU.json
This resources_fr-FR.json file provides the Australian English version of the strings.
{ "dailyFloodInfo": "tada! It’s {date}.You have {number} new messages.", "textAreaPrompt": "Enter some text and press the ”Compose” button.", "composeButton": "Compose", "richTextButton": "Rich text", "sampleToAddress": "hello@to.au", "sampleCcAddress": "hello@cc.au", "sampleBccAddress": "hello@bcc.au", "sampleSubject": "Hello World" }
{
"dailyFloodInfo": "tada! It’s {date}.You have {number} new messages.",
"textAreaPrompt": "Enter some text and press the ”Compose” button.",
"composeButton": "Compose",
"richTextButton": "Rich text",
"sampleToAddress": "hello@to.au",
"sampleCcAddress": "hello@cc.au",
"sampleBccAddress": "hello@bcc.au",
"sampleSubject": "Hello World"
}