Skip to content

Commit f70d8b9

Browse files
If injected is undefined, load from Meteor.settings
When running under Cordova, inject-initial currently fails, so this is a workaround so that the client has access to the localization information
1 parent 128e310 commit f70d8b9

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ msgfmt.init('en', {
9090
});
9191
```
9292

93+
## Cordova
94+
95+
There's an issue with the inject-initial package under Cordova which causes information to not be properly hooked to the client. To counter this, you may define the locales of the application in the settings file, under the public element.
96+
```json
97+
{
98+
"public": {
99+
...,
100+
"localization": {
101+
"native": "en",
102+
"locales": ["en", "fr"]
103+
}
104+
}
105+
}
106+
```
107+
93108
### Debug logging
94109

95110
`Logger.setLevel('msgfmt', 'trace');`

msgfmt:core/lib/mfPkg/messageformat-client.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,26 @@ mfPkg.resetStorage = function() {
395395
/* code below involves loading the actual module at long time */
396396

397397
var injected = Injected.obj('msgfmt');
398-
mfPkg.timestamps = injected ?
399-
injected.locales : {
400-
'fr': new Date(), 'en': new Date()
401-
};
402-
403-
mfPkg.native = injected ? injected.native : 'fr' ;
404-
mfPkg.sendPolicy = injected ? injected.sendPolicy : 'all';
398+
if (injected) {
399+
mfPkg.native = injected.native;
400+
mfPkg.sendPolicy = injected.sendPolicy;
401+
mfPkg.timestamps = injected.locales;
402+
} else {
403+
log.debug('Injected object was undefined, this is most likely a Cordova session');
404+
mfPkg.timestamps = {};
405+
var time = (new Date()).getTime();
406+
if (Meteor.settings && Meteor.settings.public && Meteor.settings.public.localization) {
407+
var localization = Meteor.settings.public.localization;
408+
mfPkg.native = localization.native;
409+
_.each(localization.locales, function(locale) {
410+
mfPkg.timestamps[locale] = time;
411+
});
412+
} else {
413+
log.warn('Cordova builds have issues with the inject-initial package, make sure to define settings keys public.localization.native && public.localization.locales');
414+
mfPkg.native = injected.native;
415+
mfPkg.timestamps[mfPkg.native] = time;
416+
}
417+
}
405418

406419
if (mfPkg.timestamps) {
407420
(function() {
@@ -437,14 +450,14 @@ if (msgfmt.setBodyDir) {
437450
var locale = mfPkg.useLocalStorage && amplify.store('mfLocale');
438451
if (locale) {
439452
log.debug('Found stored locale "' + locale + '"');
440-
mfPkg.setLocale(locale, true /* dontStore */);
453+
mfPkg.setLocale(locale, true /* dontStore */);
441454
} else if (locale = Session.get('locale')) {
442455
log.debug('Found session locale "' + locale + '"');
443456
mfPkg.setLocale(locale);
444457
} else if (injected && injected.headerLocale) {
445458
locale = injected.headerLocale
446459
log.debug('Setting locale from header: ' + locale);
447-
mfPkg.setLocale(locale);
460+
mfPkg.setLocale(locale);
448461
} else {
449462
mfPkg.setLocale(msgfmt.native);
450463
}

0 commit comments

Comments
 (0)