2017-06-09 19:37:01 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2017-06-22 01:13:36 +00:00
|
|
|
const app = require('electron').app;
|
2017-08-17 17:52:59 +00:00
|
|
|
const _ = require('lodash');
|
2017-06-09 19:37:01 +00:00
|
|
|
|
2017-09-25 22:00:19 +00:00
|
|
|
const logger = require('./logging').getLogger();
|
|
|
|
|
2017-06-09 19:37:01 +00:00
|
|
|
function normalizeLocaleName(locale) {
|
|
|
|
if (/^en-/.test(locale)) {
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLocaleMessages(locale) {
|
|
|
|
const onDiskLocale = locale.replace('-', '_');
|
2017-06-22 00:31:20 +00:00
|
|
|
|
|
|
|
const targetFile = path.join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'_locales',
|
|
|
|
onDiskLocale,
|
|
|
|
'messages.json'
|
|
|
|
);
|
2017-06-09 19:37:01 +00:00
|
|
|
|
2017-06-22 01:28:07 +00:00
|
|
|
return JSON.parse(fs.readFileSync(targetFile, 'utf-8'));
|
2017-06-09 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:18:50 +00:00
|
|
|
function load() {
|
2017-08-17 17:52:59 +00:00
|
|
|
var english = getLocaleMessages('en');
|
|
|
|
|
2017-07-20 17:18:50 +00:00
|
|
|
// Load locale - if we can't load messages for the current locale, we
|
|
|
|
// default to 'en'
|
|
|
|
//
|
|
|
|
// possible locales:
|
|
|
|
// https://github.com/electron/electron/blob/master/docs/api/locales.md
|
|
|
|
let localeName = normalizeLocaleName(app.getLocale());
|
|
|
|
let messages;
|
|
|
|
|
|
|
|
try {
|
|
|
|
messages = getLocaleMessages(localeName);
|
2017-08-17 17:52:59 +00:00
|
|
|
|
|
|
|
// We start with english, then overwrite that with anything present in locale
|
|
|
|
messages = _.merge(english, messages);
|
2017-07-20 17:18:50 +00:00
|
|
|
} catch (e) {
|
2017-09-25 22:00:19 +00:00
|
|
|
logger.error('Problem loading messages for locale ' + localeName + ' ' + e.stack);
|
|
|
|
logger.error('Falling back to en locale');
|
2017-07-20 17:18:50 +00:00
|
|
|
|
|
|
|
localeName = 'en';
|
2017-08-17 17:52:59 +00:00
|
|
|
messages = english;
|
2017-07-20 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: localeName,
|
|
|
|
messages
|
|
|
|
};
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:37:01 +00:00
|
|
|
module.exports = {
|
2017-07-20 17:18:50 +00:00
|
|
|
load: load
|
|
|
|
};
|