Skip to content

Instantly share code, notes, and snippets.

@rattanchauhan
Last active November 27, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattanchauhan/c82c7fb7cae7c2cdfdc421daf19d23cb to your computer and use it in GitHub Desktop.
Save rattanchauhan/c82c7fb7cae7c2cdfdc421daf19d23cb to your computer and use it in GitHub Desktop.
Extjs I18n (Keys & Translations stored in application itself)
Ext.define('App.common.I18n', {
singleton: true,
alternateClassName: ['I18n'],
requires: [
'App.locale.locale-de',
'App.locale.locale-en',
'App.locale.locale-fr',
'App.locale.locale-it'
],
initLocale: function () {
var pageParameters = Ext.urlDecode(window.location.search.substring(1));
// check in url
if (pageParameters.lang && (pageParameters.lang === 'en' || pageParameters.lang === 'de' || pageParameters.lang === 'fr' || pageParameters.lang === 'it')) {
localStorage.setItem('userLanguage', pageParameters.lang);
this.locale = pageParameters.lang;
} else {
// check in localStorage
var storedLocale = localStorage.getItem('userLanguage');
this.locale = storedLocale ? storedLocale : 'en';
}
},
get: function (key) {
if (!this.locale) {
this.initLocale();
}
switch (this.locale) {
case 'en': {
return EN.Labels[key] ? EN.Labels[key] : key;
}
case 'de': {
return DE.Labels[key] ? DE.Labels[key] : key;
}
case 'fr': {
return FR.Labels[key] ? FR.Labels[key] : key;
}
case 'it': {
return IT.Labels[key] ? IT.Labels[key] : key;
}
default: return EN.Labels[key] ? EN.Labels[key] : key;
}
},
getYesNo: function (value) {
return value === true ? this.get('Common.Label.Yes') : this.get('Common.Label.No');
},
getYesNoInternet: function (value) {
return value === true ? this.get('Common.Label.YesInternet') : this.get('Common.Label.NoInternet');
},
getOnOff: function (value) {
return value === true ? this.get('Common.Label.On') : this.get('Common.Label.Off');
},
getOnOffNotPossible: function (value) {
if (!value) {
return value;
}
if (value === 'ON') {
return this.get('Common.Label.On');
} else if (value === 'OFF') {
return this.get('Common.Label.Off');
}
return this.get('Common.Label.NotPossible');
},
locale: null
});
Ext.define('App.locale.locale-en', {
singleton: true,
alternateClassName: ['EN'],
constructor: function (config) {
this.initConfig(config);
},
Labels: {
'Common.Label.Yes': 'Yes',
'Common.Label.No': 'No',
'Common.Label.Off': 'Off',
'Common.Label.On': 'On',
'Common.Label.NotPossible': 'Not possible',
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment