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?