Answer the question
In order to leave comments, you need to log in
How to correctly implement a template change?
Hello!
I'm learning backbone. I am using requirejs-i18n.
There is a default config:
require.config
config:
i18n:
locale: 'ru-ru'
Answer the question
In order to leave comments, you need to log in
Templates can be the same for all languages. Just all the text should be output through variables. And these variables, the current locale, are forwarded to the template engine along with the data from the views. I always use a template manager in backbone applications, which is responsible, among other things, for forwarding end-to-end data to templates.
And as I already understood, you need to somehow add an event about changing the locale (language) and so that all the parts that are multilingual react to it and load the language?For simplicity, you can simply reload the entire page. Changing the language on the fly is a rather rare action, you don’t have to bother too much.
//объявляем глобальную переменную - можно где нибудь в index.html или где удобно с языком по-умолчанию:
SYSTEM = {
lang: ru
}
var template = Handlebars.compile(templateTpl);
//Наряду с переменными передаваемыми в шаблон, передаём объект с переводами
var lang: = {
ru: {
name: "Имя"
},
en: {
name: "Name"
}
}
var data = {
myVar1: '',
myVar2: ''
}
var templateData = $.extend({}, {lang: lang[SYSTEM.lang]}, data);
this.$el.append(template(templateData));
<div>
<span>{{lang.name}}</span>
<span>{{myVar1}}</span>
<span>{{myVar2}}</span>
</div>
SYSTEM.lang = 'en';
Backbone.history.navigate(window.location, {trigger:true});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question