2020-11-19 23:38:59 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
translate,
|
|
|
|
|
getTranslations,
|
2023-08-10 16:20:33 +05:30
|
|
|
listLanguages
|
2020-11-19 23:38:59 +01:00
|
|
|
};
|
|
|
|
|
|
2021-08-19 11:00:35 -07:00
|
|
|
const assert = require('assert'),
|
2023-08-04 10:10:08 +05:30
|
|
|
cloudron = require('./cloudron.js'),
|
2020-11-19 23:38:59 +01:00
|
|
|
debug = require('debug')('box:translation'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
paths = require('./paths.js'),
|
2023-08-04 10:10:08 +05:30
|
|
|
safe = require('safetydance');
|
2020-11-19 23:38:59 +01:00
|
|
|
|
|
|
|
|
// to be used together with getTranslations() => { translations, fallback }
|
2024-07-05 12:41:51 +02:00
|
|
|
function translate(input, assets) {
|
2020-11-19 23:38:59 +01:00
|
|
|
assert.strictEqual(typeof input, 'string');
|
2024-07-05 12:41:51 +02:00
|
|
|
assert.strictEqual(typeof assets, 'object');
|
|
|
|
|
assert.strictEqual(typeof assets.translations, 'object');
|
|
|
|
|
assert.strictEqual(typeof assets.fallback, 'object');
|
|
|
|
|
|
|
|
|
|
const { translations, fallback } = assets;
|
2020-11-19 23:38:59 +01:00
|
|
|
|
2021-08-19 11:00:35 -07:00
|
|
|
const tokens = input.match(/{{(.*?)}}/gm);
|
2020-11-19 23:38:59 +01:00
|
|
|
if (!tokens) return input;
|
|
|
|
|
|
2021-08-19 11:00:35 -07:00
|
|
|
let output = input;
|
2024-07-05 11:41:07 +02:00
|
|
|
for (const token of tokens) {
|
2021-08-19 11:00:35 -07:00
|
|
|
const key = token.slice(2).slice(0, -2).trim();
|
|
|
|
|
let value = key.split('.').reduce(function (acc, cur) {
|
2020-11-19 23:38:59 +01:00
|
|
|
if (acc === null) return null;
|
|
|
|
|
return typeof acc[cur] !== 'undefined' ? acc[cur] : null;
|
|
|
|
|
}, translations);
|
|
|
|
|
|
|
|
|
|
// try fallback
|
|
|
|
|
if (value === null) value = key.split('.').reduce(function (acc, cur) {
|
|
|
|
|
if (acc === null) return null;
|
|
|
|
|
return typeof acc[cur] !== 'undefined' ? acc[cur] : null;
|
2024-07-05 12:41:51 +02:00
|
|
|
}, fallback);
|
2020-11-19 23:38:59 +01:00
|
|
|
|
|
|
|
|
if (value === null) value = token;
|
|
|
|
|
|
|
|
|
|
output = output.replace(token, value);
|
2024-07-05 11:41:07 +02:00
|
|
|
}
|
2020-11-19 23:38:59 +01:00
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 11:00:35 -07:00
|
|
|
async function getTranslations() {
|
2024-08-23 10:08:55 +02:00
|
|
|
const fallback = safe.JSON.parse(fs.readFileSync(path.join(paths.TRANSLATIONS_DIR, 'en.json'), 'utf8'));
|
2024-07-05 11:41:07 +02:00
|
|
|
if (!fallback) debug(`getTranslations: Fallback language en not found. ${safe.error.message}`);
|
2020-11-19 23:38:59 +01:00
|
|
|
|
2023-08-04 10:10:08 +05:30
|
|
|
const lang = await cloudron.getLanguage();
|
2020-11-19 23:38:59 +01:00
|
|
|
|
2024-08-23 10:08:55 +02:00
|
|
|
const translations = safe.JSON.parse(fs.readFileSync(path.join(paths.TRANSLATIONS_DIR, lang + '.json'), 'utf8'));
|
2024-07-05 11:41:07 +02:00
|
|
|
if (!translations) debug(`getTranslations: Requested language ${lang} not found. ${safe.error.message}`);
|
2020-11-19 23:38:59 +01:00
|
|
|
|
2024-07-05 11:41:07 +02:00
|
|
|
return { translations: translations || {}, fallback: fallback || {} };
|
2020-11-19 23:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 16:20:33 +05:30
|
|
|
async function listLanguages() {
|
2024-08-23 10:08:55 +02:00
|
|
|
const [error, result] = await safe(fs.promises.readdir(paths.TRANSLATIONS_DIR));
|
2021-08-19 11:00:35 -07:00
|
|
|
if (error) {
|
2023-08-10 16:20:33 +05:30
|
|
|
debug('listLanguages: Failed to list translations. %o', error);
|
2024-02-26 12:32:14 +01:00
|
|
|
return [ 'en' ]; // we always return english to avoid dashboard breakage
|
2021-08-19 11:00:35 -07:00
|
|
|
}
|
2020-11-19 23:38:59 +01:00
|
|
|
|
2021-08-19 11:00:35 -07:00
|
|
|
const jsonFiles = result.filter(function (file) { return path.extname(file) === '.json'; });
|
2024-02-26 12:32:14 +01:00
|
|
|
const languages = jsonFiles.map(function (file) { return path.basename(file, '.json'); });
|
2021-08-19 11:00:35 -07:00
|
|
|
return languages;
|
2020-11-19 23:38:59 +01:00
|
|
|
}
|