Skip to content

Instantly share code, notes, and snippets.

@zippy1978
Last active December 17, 2015 04:28
Show Gist options
  • Select an option

  • Save zippy1978/5550224 to your computer and use it in GitHub Desktop.

Select an option

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
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;
}
});
@JFonS
Copy link

JFonS commented Jul 31, 2013

Missing closing bracket at line 11

@sichacvah
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment