i18next / ng-i18next

translation for AngularJS using i18next
https://github.com/i18next/ng-i18next
MIT License
161 stars 54 forks source link

configure customLoad function #18

Closed pschulten closed 11 years ago

pschulten commented 11 years ago

I'd like to use a customLoad function. But one can not inject a services or $http in the config phase.

bugwelle commented 11 years ago

Hello, loading the translation files is done by i18next. ng-i18next is only the wrapper to use i18next with AngularJS.

I don't know if this works, but you could try something like this:

  1. config i18next ($i18nextProvider.options = {...})
  2. load your translation files with your own service
  3. pass your translation to : $i18next.resStore

Please look at this: https://github.com/archer96/ng-i18next/blob/master/test/unit/i18nextDirectiveSpec.js#L16-L49

I hope that solves your problem :)

Regards, Andre

pschulten commented 11 years ago

Thank you. Here's what I'm doing: Tell i18next to not hit the server:

angular.module('jm.i18next').config(function ($i18nextProvider) {
        $i18nextProvider.options = {
                resStore: {} //defining a resStore prevents i18next to fetch stuff from the server
            };
    });

and then later use my service:

Translation.load(lang).then(function(transl){
                $i18next.options = {resStore: transl, lng:lang};
                $rootScope.$broadcast('i18nextLanguageChange');

            });
pschulten commented 11 years ago

I turned out, that defining an empty resStore was a bad idea. When I reload the page or directly browse a certain route angular somehow stops and the page content isn't shown. Workaround one was to remove the call to $digest in the init function of ng_i18next.js

function init(options) {

    window.i18n.init(options, function (localize) {

        if (!$rootScope.$$phase) {
            //wtf? $rootScope.$digest();
        }

        t = localize;

        $rootScope.$broadcast('i18nextLanguageChange');

    });

}

which I don't like. Workaround two was trigger the normal i18next life-cycle with:

angular.module('jm.i18next').config(function ($i18nextProvider) {
    $i18nextProvider.options = {
            resGetPath: 'empty.json' //load translation manually
        };
});