Refer to the following steps to render different messages in your app according to the device's language settings:
Open the command-line tool and create a new PhoneGap project called
globalization
.$ cordova create ~/the/path/to/your/source/globalization com.gnstudio.pg.globalization Globalization
Add the Globalization API plugin using the command line.
$ cordova plugins add https: //git-wip-us.apache.org/repos/asf/cordova-plugin-globalization.git
Using the command-line tool, add the platform you want to use for this test (Android, Blackberry, iOS, or Windows Phone 8).
$ cordova platforms add android
Go to the
www/js
folder, create thelibs
folder and the subfolders needed to store therequire.js
file and the i18n plugin.|-libs |---require |-----plugins
Download and save the minified version of Require.js available at http://requirejs.org/docs/download.html#requirejs in the
js/libs/require
folder and the i18n plugin available at http://requirejs.org/docs/download.html#i18n in thejs/libs/require/plugins
folder.Go to
www/js
and create a new folder namednls
(the plugin will search for a folder named in this way to load language files). In this folder create a subfolder to store the data for each supported locale and a root folder to be used in case the user locale is not yet implemented (i.e.,root
).|-nls |---en-us |---es-es |---fr-fr |---it-it |---root
Create a file named
connection.js
in thenls/root
folder containing all the default messages to render in case the app is having connection issues. The syntax required by the i18l plugin is very similar to the one used to define a new Require.js module except that the name of the module is not required.// root ;define({ 'cannotConnect': 'The app can\'t connect to internet.', 'connectionSlow': 'The connection is slow be patient.', 'connectionError':'Please try again later.' });
Create a language file for each supported locale using the same name and the same syntax but with different content so that you can easily get the differences when testing your app. Save the file in the language-specific folder (for example,
fr-fr
).Go to the
nls
folder and create another JavaScript file namedconnection.js
in order to specify the supported locales.;define({ 'root': true, 'fr-fr': true, 'en_us': true, 'es-es': true, 'it-it': true });
Go to the
js
folder, create a new JavaScript file namedmain.js
, and define a new Require.js module specifying a dependency to theconnection.js
language files.;define('main', ['i18n!nls/connection'], (function(connection){ // All the methods and variables will go here }));
Create in the
main
module a function namedinit
and define in its body a listener for thedeviceready
event.var init = function(){ document.addEventListener('deviceready', onDeviceReady); };
Return the
init
function at the end of the module in order to be able to call it from other modules.return{ init: init }
Once the
deviceready
event is triggered and the PhoneGap framework is loaded, it's possible to access the Globalization API and discover the device current locale using thegetLocaleName
method.var onDeviceReady = function(evt){ var g = navigator.globalization; g.getLocaleName(onLocaleName, onGlobalizationError); };
Define the handler
onLocaleName
and use the values returned by the i18n plugin to display all of them on the screen.var onLocaleName = function(locale){ console.log('onLocaleName', locale.value); var deviceready = document.querySelector('#deviceready'); var element = document.createElement('span'); element.innerHTML += 'The cannotConnect message is:<i>' + connection.cannotConnect + '</i><br>'; element.innerHTML += 'The connectionSlow message is:<i>' + connection.connectionSlow + '</i><br>'; element.innerHTML += 'The connectionError message is:<i>' + connection.connectionError + '</i><br>'; deviceready.appendChild(element); };
Define the
onGlobalizationError
handler and notify the user that an issue has occurred.var onGlobalizationError = function(error) { var message = 'code: ' + error.code + '\n'; message += 'message: ' + error.message; navigator.notification.alert(message, null); };
In the
js
folder create a new JavaScript file namedapp.js
and use it to configure the paths forRequire.js
to load themain
module and call theinit
method.require.config({ paths: { i18n: 'libs/require/plugins/i18n' }, }); require(['main'], function(main){ main.init(); });
Open the
index.html
file and add ascript
tag in the head section to loadRequire.js
and theapp.js
file.<script data-main="js/app" src="js/libs/require/require.js"></script>
Open the command-line tool, go to the
project
folder, and build and run the app on a real device or an emulator.$ cordova build $ cordova run