Last active
December 17, 2015 04:28
-
-
Save zippy1978/5550224 to your computer and use it in GitHub Desktop.
Here is a singleton Sencha Touch class to handle internationalization (i18n). For more information on how to use it, please read : http://zippy1978.tumblr.com/post/36131938659/internationalization-i18n-with-sencha-touch#comment-889307386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Ext.define('MyApp.util.I18n', { | |
| singleton : true, | |
| config : { | |
| defaultLanguage : 'en', | |
| translations : { | |
| 'en' : { | |
| 'signIn' : 'Sign in', | |
| 'name' : 'Name', | |
| 'hello' : 'Hello {0} !', | |
| 'enOnly' : 'English only !' | |
| }, | |
| 'fr' : { | |
| 'signIn' : 'Identifiez-vous', | |
| 'name' : 'Nom', | |
| 'hello' : 'Bonjour {0} !' | |
| } | |
| } | |
| }, | |
| constructor: function(config) { | |
| this.initConfig(config); | |
| this.callParent([config]); | |
| }, | |
| translate: function (key) { | |
| // Get browser language | |
| var browserLanguage = window.navigator.userLanguage || window.navigator.language; | |
| // Is it defined ? if not : use default | |
| var language = this.getTranslations()[browserLanguage] === undefined ? this.getDefaultLanguage() : browserLanguage; | |
| // Translate | |
| var translation = "[" + key + "]"; | |
| if (this.getTranslations()[language][key] === undefined) { | |
| // Key not found in language : tries default one | |
| if (this.getTranslations()[this.getDefaultLanguage()][key] !== undefined) { | |
| translation = this.getTranslations()[this.getDefaultLanguage()][key]; | |
| } | |
| } else { | |
| // Key found | |
| translation = this.getTranslations()[language][key]; | |
| } | |
| // If there is more than one argument : format string | |
| if (arguments.length > 1) { | |
| var tokenCount = arguments.length - 2; | |
| for( var token = 0; token <= tokenCount; token++ ) | |
| { | |
| translation = translation.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] ); | |
| } | |
| } | |
| return translation; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Missing closing bracket at line 11