2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2017-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-06-09 19:37:01 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2017-08-17 17:52:59 +00:00
|
|
|
const _ = require('lodash');
|
2020-02-13 18:14:08 +00:00
|
|
|
const { setup } = require('../js/modules/i18n');
|
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
|
|
|
}
|
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
function load({ appLocale, logger } = {}) {
|
|
|
|
if (!appLocale) {
|
|
|
|
throw new TypeError('`appLocale` is required');
|
|
|
|
}
|
2017-10-03 18:08:06 +00:00
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
if (!logger || !logger.error) {
|
|
|
|
throw new TypeError('`logger.error` is required');
|
2017-10-03 18:08:06 +00:00
|
|
|
}
|
2017-08-17 17:52:59 +00:00
|
|
|
|
2018-03-02 20:59:39 +00:00
|
|
|
const 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
|
2017-10-03 18:08:06 +00:00
|
|
|
let localeName = normalizeLocaleName(appLocale);
|
2017-07-20 17:18:50 +00:00
|
|
|
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) {
|
2018-05-02 01:54:43 +00:00
|
|
|
logger.error(
|
|
|
|
`Problem loading messages for locale ${localeName} ${e.stack}`
|
|
|
|
);
|
2017-09-25 22:00:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-13 18:14:08 +00:00
|
|
|
const i18n = setup(appLocale, messages);
|
|
|
|
|
2017-07-20 17:18:50 +00:00
|
|
|
return {
|
2020-02-13 18:14:08 +00:00
|
|
|
i18n,
|
2017-07-20 17:18:50 +00:00
|
|
|
name: localeName,
|
2018-01-08 21:19:25 +00:00
|
|
|
messages,
|
2017-07-20 17:18:50 +00:00
|
|
|
};
|
2017-06-22 01:13:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:37:01 +00:00
|
|
|
module.exports = {
|
2018-01-08 21:19:25 +00:00
|
|
|
load,
|
2017-07-20 17:18:50 +00:00
|
|
|
};
|