How to load language keys from separate js file?

327 Views Asked by At

This is my app.js

app.js
    $translateProvider.registerAvailableLanguageKeys(['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT'], {  
                                'en-*':'en_US',
                                'es-*':'es_ES', 
                                'pt-*':'pt_PT',
                                'fr-*':'fr_FR',
                                'de-*':'de_DE',
                                'ja-*':'ja_JP',
                                'it-*':'it_IT',
                                 '*':'en_US'
                                })    
                    }
                    ]

keys (['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT'] and

'en-*':'en_US',
'es-*':'es_ES', 
'pt-*':'pt_PT',
'fr-*':'fr_FR',
'de-*':'de_DE',
'ja-*':'ja_JP',
'it-*':'it_IT',
 '*':'en_US'

How can i get these keys from a global object from outside javascript file since im repeating these keys in other places.

My javascript file has these two arrays.

keys.js

var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT']
var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*']

How can i use this javascript file to get iterate key value pair in app.js and other places instead of repeating it everywhere.

1

There are 1 best solutions below

0
On

If your goal is to create an object with key-value pairs from the two arrays you could do the following:

var obj = {};
for (var i = 0; i < commonKeys.length; i++) {
    obj[commonKeys[i]] = keys[i];
}
console.log(obj);

Or, if you are ok with using a third-party library, you could include underscore.js and use the _.object function like this:

var obj = _.object(commonKeys, keys);
console.log(obj);

In both cases the output of the js console would be

Object {en-*: "en_US", es-*: "es_ES", pt-*: "pt_PT", fr-*: "fr_FR", de-*: "de_DE"…}