0

I'm new to angular-translate (I'm not a God in Angular itself though). Say I have a set of translation JSON files already available, in English and Italian. Those contain the translations I should use by default.

i18n/locale-en_GB.json

{
    "market.marketplace.title": "Marketplace",
     "market.marketplace.descr": "Here is a description",
     "market.available.items": "Available items"
}

i18n/locale-it_IT.json

{
    "market.marketplace.title": "Marketplace",
     "market.marketplace.descr": "Ecco una descrizione",
     "market.available.items": "Oggetti disponibili"
}

If that was all, I would of course simply use the static file loader:

.conf

...

$translateProvider.useStaticFilesLoader({
   prefix: 'i18n/locale-', // Template paths to translation files
   suffix: '.json'
});

...

The problem is that I also have to take into account the results of a REST Call that I have to run at the very beginning (say translation service configuration time), which could override some of the default translations. I tried to use a custom loader:

.conf

...

//$translateProvider.useStaticFilesLoader....
$translateProvider.useLoader('customLoader', {});

...

.factory

    .factory('customLoader', function ($q, $http, MyRESTService, translationService) {

        return function (options) {
            var deferred = $q.defer();
            MyRESTService.getLanguageMaps(
                function success(response) {
                    var langMap = response;
                    /* langMap would be something like:
                    var langMap = [
                        {
                            "en_GB": {
                                "market.marketplace.title": "NEWENGLISHTITLE",
                                "market.marketplace.descr": "NEWENGLISHDESCR"
                            }
                        },
                        {
                           "it_IT": {
                                "market.marketplace.title": "NEWITALIANTITLE",
                                "market.marketplace.descr": "NEWITALIANDESCR"
                           }
                        }
                   ];
                   */
                   deferred.resolve(langMap); 
                },
                function failure(err) {
                    deferred.reject(err);
                }
            );
            return deferred.promise;
        };

    })

But I can't seem to find a way to load my "default" translations (from static files) first, and then merge with results from my REST API. Furthermore, the only way I could find to make this customLoader work was to specify only one JSON as translation map, i.e. I can't make it use the first object as English map and second object as Italian map.

E.g.

/* If resulting configMap layout is like this, translations are displayed but only second JSON Object is used */
var configMap = [
    {                   
        "market.marketplace.title": "ENGLISHTITLE",
        "market.marketplace.descr": "ENGLISHDESCR"
    },
    {
        "market.marketplace.title": "ITALIANTITLE",
        "market.marketplace.descr": "ITALIANDESCR"              
    }
]

/* This way does not work */
var configMap = [
    {"en_GB", 
        {                   
           "market.marketplace.title": "ENGLISHTITLE",
           "market.marketplace.descr": "ENGLISHDESCR"
        },
    },
    {"it_IT", 
       {
           "market.marketplace.title": "ITALIANTITLE",
           "market.marketplace.descr": "ITALIANDESCR"               
       }
    }
]

I can decide how results will be given, as I still have to implement the REST call.

I hope I made myself clear enough! Can somebody help here?

  • Doesn't https://github.com/angular-translate/angular-translate/issues/1125 and knallis explanation help you? – Yosh Oct 06 '15 at 13:42
  • Didn't see that post.. the plunker knallis proposes http://plnkr.co/edit/F9bsLhIOKCNpkPR6Xbrc?p=info can actually be used to do what I want (provided that I still need to load my initial table from .json files, but this can be easily done via $http object). Strange fact: he didn't point me to this direction when I decided to ask in the angular-translate forum: https://github.com/angular-translate/angular-translate/issues/1240 (yes I did used different words but the point was still the same) – Francesco Lilli Oct 07 '15 at 14:28

0 Answers0