I'm developing a one page application using backbone, for the i18n i would like to use i18next but I'm experiencing big trouble to load and initialize i18next properly using requirejs. I'm using the jQuery plugin and the data attributes in html tags to perform the translations, so i would like to initialize i18next once, in the application bootstrap and perform the translations on the document ready event.
My requirejs initialization looks as follows:
require.config({
baseUrl : './',
paths :
{
/** Vendor **/
jquery : 'js/vendor/jquery/jquery.min',
underscore : 'js/vendor/underscore/underscore-optamd-min',
backbone : 'js/vendor/backbone/backbone-optamd-min',
bootstrap : 'js/vendor/bootstrap/amd/bootstrap',
text : 'js/vendor/require/text',
order : 'js/vendor/require/order',
json : 'js/vendor/json/json2',
i18n : 'js/vendor/i18next/i18next.min',
/** General Folder Routes **/
vendor: 'js/vendor',
libs : 'js/libs',
utils : 'js/utils',
/** App Folder Routes **/
templates : 'templates/private',
app : 'app/private',
modules : 'app/private/modules',
/** Else... **/
vm : 'js/utils/vm',
buffer : 'js/utils/buffer',
basemodel : 'js/utils/baseModel',
basecollection : 'js/utils/baseCollection',
highcharts : 'js/vendor/highcharts/js/highcharts',
fullcalendar : 'js/libs/fullcalendar/fullcalendar.min',
chosen : 'js/libs/chosen/chosen.jquery.min'
},
shim: {
'i18n': {
deps: ['jquery'],
exports: 'i18n'
}
},
packages : [
"layout",
{
name : "layoutView",
main : "../app/private/modules/layout/layoutView"
}
],
urlArgs : "v=" + '2' + '&bust=' + (new Date()).getTime()
});
require([ 'jquery', 'underscore', 'backbone', 'json', 'i18n', 'utils/plugins', 'vm', 'app/app', 'app/router', 'buffer'], function($, _, Backbone, JsonJS, i18n, Plugins, Vm, App, Router, Buffer) {
Router.initialize();
});
and my app main app is as follows:
define([ 'jquery',
'underscore',
'backbone',
'vm',
'i18n',
'layoutView'
], function($, _, Backbone, Vm, i18n, LayoutView) {
var App = Backbone.View.extend({
el: 'body',
initialize: function ()
{
i18n.init({ lng: "es", fallbackLng: "en", useDataAttrOptions: true, debug: true }).done(function(){
var layoutView = Vm.create('layoutView', LayoutView);main app f
$(document).ready(function(){
$('.i18n').i18n();
});
});
}
});
return new App;
});
using this configuration i18next sometimes loads fine and works flawlessly and sometimes it tries to load itself before jQuery, so the deferred object in lines 529 - 534 in the init method of i18next is undefined:
// jQuery deferred
var deferred;
if ($ && $.Deferred) {
deferred = $.Deferred();
}
I've tried to initialize i18next before and after Router.initialize(), doesn't work. Tried to load the translation files syncronous and asyncronous, nothing works: I like i18next and I would like to use it in my project, but this issue is driving me mad.
Any clue or pointing finger in the right direction is really appreciated.
Thanks in advance.
Diego.