Answer the question
In order to leave comments, you need to log in
Ember.js + i18n: how to arrange language change without reloading the page?
Hello, I used the only (it seems) Ember extension that implements internationalization:
github.com/zendesk/ember-i18n The
question is - how can I change the application language without reloading the page?
Answer the question
In order to leave comments, you need to log in
As a result, it turned out something like this ...
TranslationsPrototype.
A prototype for working with localizations. If there is no string in the translation, the value from the default locale will be taken. Thus, if something is not translated in other languages, but is in the source code, we will not have an alias or an empty space - there will be an original. For me it's English.
/**
* Прототип обьекта, содержащего локализации
* Назначение просто - вернуть обьект с переведенными строками
* @type {*}
*/
var translationsPrototype = Em.Object.extend({
defaultLocale:'en',
currentTranslation:{},
locales:_locales,
getLocale:function (localeName) {
this.currentTranslation = this.locales[localeName];
for (prop in this.locales[this.defaultLocale]) {
if (typeof this.currentTranslation[prop] == 'undefined') {
this.currentTranslation[prop] = this.locales[this.defaultLocale][prop];
}
}
return this.currentTranslation;
}
});
/**
* Перевод программы
* @type {Object}
* @private
*/
_locales = {
en:{
'str_one':'Hello',
},
ru:{
'str_one':'Привет',
},
}
var Translation = translationsPrototype.create();
var App= Em.Application.create({
version:'1.0b',
language:'en',
debug:0,
init:function () { //constructor overwrite
Ember.Namespace.NAMESPACES.push(this);
Ember.Namespace.PROCESSED = false;
// вся эта мутня с переводами нужна для живой смены языка в рамках подходов ember
var translation = Em.Object.extend();
this.T = translation.create();
this._setLocale(this.get('language'));
},
_setLocale:function (localeName) {
this.T.setProperties(Translation.getLocale(localeName));
return this;
}
});
T = App.T; // алиас для глобального контекста
App.addObserver('language', function (object, property) {
object._setLocale(object.language);
});
<script type="text/x-handlebars">
{{T.str_one}}!
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question