Refactor locale to only expose the data we care about

This commit is contained in:
David Balatero
2017-06-21 18:13:36 -07:00
committed by Scott Nonnenberg
parent 5e5ca80a6e
commit ceaff68d55
3 changed files with 50 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
const path = require('path');
const fs = require('fs');
const app = require('electron').app;
function normalizeLocaleName(locale) {
if (/^en-/.test(locale)) {
@@ -23,7 +24,25 @@ function getLocaleMessages(locale) {
return JSON.parse(fs.readFileSync(targetFile, 'utf-8'))
}
module.exports = {
normalizeLocaleName,
getLocaleMessages
// 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);
} catch (e) {
console.log('Problem loading messages for locale ', localeName, e.stack);
console.log('Falling back to en locale');
localeName = 'en';
messages = getLocaleMessages(localeName);
}
module.exports = {
name: localeName,
messages
}