'How to switch languages with the i18next plugin?
I am using Backbone.js in my application and the i18next plugin for my language switch function on my application. When I pass a value to the lng option in the init function call, then it translates my page correctly.
Now I want to do this dynamically via a language selector. I have a <select> of four languages and I want to pass the value of the selected language to the lng option of the init function.
Here is my code:
HTML
<div class="col-xs-6>
<select class="form-control language-selector">
<option value="de">Deutsch</option>
<option value="en">English</option>
<option value="fr">Français</option>
<option value="it">Italiano</option>
</select>
</div>
JavaScript
i18next.init({
debug: true,
languages: ['de','en','fr','it'],
lng: 'de',
fallbackLng: false,
load: 'current',
resources: resBundle
}, function(err, t){
});
'change .language-selector': function(e){
e.preventDefault();
i18next.setLng($(e.target).val(), (err, t) => {
console.log(arguments);
this.render();
});
}
Solution 1:[1]
$(document).ready(function () {
i18n.init({
"lng": 'en',
"resStore": resources,
"fallbackLng" : 'en'
}, function (t) {
$(document).i18n();
});
'change .language-selector': function(e){
e.preventDefault();
i18n.init({
lng: $(e.target).val()}, (err, t) => {
console.log(arguments);
$(document).i18n();
});
}
}
I dunno backbone.js. Working solution in normal JavaScript is here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
