signal-desktop/app/locale.js
Scott Nonnenberg 64fe9dbfb2
Clean logs on start - and eslint/mocha with code coverage (#1945)
* Clean logs on startup; install server-side testing/linting

* Add eslint config, make all of app/ conform to its demands

* Add Node.js testing and linting to CI

* Lock project to Node.js 7.9.0, used by Electron 1.7.10

* New eslint error: trailing commas in function argumensts

Node 7.9.0 doesn't like trailing commas, but Electron does

* Move electron to devDependency, tell eslint it's built-in
2018-01-08 13:19:25 -08:00

68 lines
1.4 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const { app } = require('electron');
const _ = require('lodash');
const logging = require('./logging');
function normalizeLocaleName(locale) {
if (/^en-/.test(locale)) {
return 'en';
}
return locale;
}
function getLocaleMessages(locale) {
const onDiskLocale = locale.replace('-', '_');
const targetFile = path.join(
__dirname,
'..',
'_locales',
onDiskLocale,
'messages.json'
);
return JSON.parse(fs.readFileSync(targetFile, 'utf-8'));
}
function load() {
const logger = logging.getLogger();
const english = getLocaleMessages('en');
let appLocale = app.getLocale();
if (process.env.NODE_ENV === 'test') {
appLocale = 'en';
}
// 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(appLocale);
let messages;
try {
messages = getLocaleMessages(localeName);
// We start with english, then overwrite that with anything present in locale
messages = _.merge(english, messages);
} catch (e) {
logger.error(`Problem loading messages for locale ${localeName} ${e.stack}`);
logger.error('Falling back to en locale');
localeName = 'en';
messages = english;
}
return {
name: localeName,
messages,
};
}
module.exports = {
load,
};