Files
cloudron-box/src/translations.js

76 lines
2.6 KiB
JavaScript
Raw Normal View History

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'),
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'),
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-26 17:26:12 +02:00
const fallbackData = fs.readFileSync(path.join(paths.TRANSLATIONS_DIR, 'en.json'), 'utf8');
if (!fallbackData) debug(`getTranslations: Fallback language en not found. ${safe.error.message}`);
const fallback = safe.JSON.parse(fallbackData) || {};
2020-11-19 23:38:59 +01:00
const lang = await cloudron.getLanguage();
2020-11-19 23:38:59 +01:00
2024-08-26 17:26:12 +02:00
const translationData = safe.fs.readFileSync(path.join(paths.TRANSLATIONS_DIR, `${lang}.json`), 'utf8');
if (!translationData) debug(`getTranslations: Requested language ${lang} not found. ${safe.error.message}`);
const translations = safe.JSON.parse(translationData) || {};
2020-11-19 23:38:59 +01:00
2024-08-26 17:26:12 +02:00
return { translations, 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) {
debug(`listLanguages: Failed to list translations. %${error.message}`);
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'; });
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
}