Fix localization includes and remove default locale messages

This commit is contained in:
Tim Mickel 2016-01-13 14:51:47 -05:00
parent ba6846f049
commit 7dec89ddb3

View file

@ -1,14 +1,15 @@
import Cookie from './Cookie'; import Cookie from './Cookie';
import Intl from 'intl';
import IntlMessageFormat from 'intl-messageformat'; import IntlMessageFormat from 'intl-messageformat';
window.Intl = Intl;
let currentLocale; let currentLocale;
let root = ''; let root = '';
let localizationMessages = {}; let localizationMessages = {};
let defaultLocalizationMessages = {};
// Configuration // Configuration
const defaultLocale = window.Settings.defaultLocale; const defaultLocale = window.Settings.defaultLocale;
const defaultLocaleShort = window.Settings.defaultLocaleShort;
const supportedLocales = window.Settings.supportedLocales; const supportedLocales = window.Settings.supportedLocales;
const sampleKeyPrefix = 'key_'; const sampleKeyPrefix = 'key_';
@ -48,6 +49,7 @@ export default class Localization {
// Call this when the app is initialized // Call this when the app is initialized
static includeLocales () { static includeLocales () {
var localizationCookie = Cookie.get('localization'); var localizationCookie = Cookie.get('localization');
if (localizationCookie === null) { if (localizationCookie === null) {
currentLocale = this.determineLocaleFromBrowser(); currentLocale = this.determineLocaleFromBrowser();
} else { } else {
@ -55,25 +57,11 @@ export default class Localization {
} }
var topLevel = currentLocale.split('-')[0]; var topLevel = currentLocale.split('-')[0];
// Intl locale-data
document.write('<script src="' + root +
'jssource/external/Intl/locale-data/jsonp/' + topLevel + '.js"><\/script>');
// Always load default locale
document.write('<script src="' + root +
'jssource/external/Intl/locale-data/jsonp/' + defaultLocale + '.js"><\/script>');
document.write('<script src="' + root +
'jssource/external/intl-messageformat/locale-data/' + defaultLocaleShort + '.js"><\/script>');
// Get messages synchronously // Get messages synchronously
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', root + 'localizations/' + topLevel + '.json', false); xhr.open('GET', root + 'localizations/' + topLevel + '.json', false);
xhr.send(null); xhr.send(null);
localizationMessages = JSON.parse(xhr.responseText); localizationMessages = JSON.parse(xhr.responseText);
xhr = new XMLHttpRequest();
xhr.open('GET', this.root + 'localizations/' + this.defaultLocale + '.json', false);
xhr.send(null);
defaultLocalizationMessages = JSON.parse(xhr.responseText);
} }
// Translate a particular message given the message key and info // Translate a particular message given the message key and info
@ -82,9 +70,6 @@ export default class Localization {
if (key in localizationMessages) { if (key in localizationMessages) {
message = new IntlMessageFormat(localizationMessages[key], currentLocale); message = new IntlMessageFormat(localizationMessages[key], currentLocale);
return message.format(formatting); return message.format(formatting);
} else if (key in defaultLocalizationMessages) {
message = new IntlMessageFormat(defaultLocalizationMessages[key], defaultLocale);
return message.format(formatting);
} }
return 'String missing: ' + key; return 'String missing: ' + key;
} }