2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const { promisify } = require('util');
|
2021-01-26 18:33:40 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const log = require('loglevel');
|
2021-01-26 18:33:40 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const readFile = promisify(fs.readFile);
|
2021-01-26 18:33:40 +01:00
|
|
|
|
|
|
|
function getLocalePath(code) {
|
|
|
|
return path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'app',
|
|
|
|
'_locales',
|
|
|
|
code,
|
|
|
|
'messages.json',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-26 18:33:40 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 20:10:51 +02:00
|
|
|
// eslint-disable-next-line consistent-return
|
2021-01-26 18:33:40 +01:00
|
|
|
async function getLocale(code) {
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const localeFilePath = getLocalePath(code);
|
|
|
|
const fileContents = await readFile(localeFilePath, 'utf8');
|
|
|
|
return JSON.parse(fileContents);
|
2021-01-26 18:33:40 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ENOENT') {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error('Locale file not found');
|
2021-01-26 18:33:40 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(`Error opening your locale ("${code}") file: `, e);
|
2021-01-26 18:33:40 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
process.exit(1);
|
2021-01-26 18:33:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareLocalesForMissingItems({ base, subject }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Object.keys(base).filter((key) => !subject[key]);
|
2021-01-26 18:33:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 08:19:41 +01:00
|
|
|
function compareLocalesForMissingDescriptions({ englishLocale, targetLocale }) {
|
|
|
|
return Object.keys(englishLocale).filter(
|
|
|
|
(key) =>
|
|
|
|
targetLocale[key] !== undefined &&
|
|
|
|
englishLocale[key].description !== undefined &&
|
|
|
|
englishLocale[key].description !== targetLocale[key].description,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-03 08:19:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 18:33:40 +01:00
|
|
|
module.exports = {
|
2021-02-03 08:19:41 +01:00
|
|
|
compareLocalesForMissingDescriptions,
|
2021-01-26 18:33:40 +01:00
|
|
|
compareLocalesForMissingItems,
|
|
|
|
getLocale,
|
|
|
|
getLocalePath,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|