translation.js -> translations.js
kept confusing my why i can't find this file! this is in line with the rest of our code
This commit is contained in:
75
src/translations.js
Normal file
75
src/translations.js
Normal file
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
translate,
|
||||
getTranslations,
|
||||
listLanguages
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
cloudron = require('./cloudron.js'),
|
||||
debug = require('debug')('box:translation'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
paths = require('./paths.js'),
|
||||
safe = require('safetydance');
|
||||
|
||||
const TRANSLATION_FOLDER = path.join(paths.DASHBOARD_DIR, 'translation');
|
||||
|
||||
// to be used together with getTranslations() => { translations, fallback }
|
||||
function translate(input, assets) {
|
||||
assert.strictEqual(typeof input, 'string');
|
||||
assert.strictEqual(typeof assets, 'object');
|
||||
assert.strictEqual(typeof assets.translations, 'object');
|
||||
assert.strictEqual(typeof assets.fallback, 'object');
|
||||
|
||||
const { translations, fallback } = assets;
|
||||
|
||||
const tokens = input.match(/{{(.*?)}}/gm);
|
||||
if (!tokens) return input;
|
||||
|
||||
let output = input;
|
||||
for (const token of tokens) {
|
||||
const key = token.slice(2).slice(0, -2).trim();
|
||||
let value = key.split('.').reduce(function (acc, cur) {
|
||||
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;
|
||||
}, fallback);
|
||||
|
||||
if (value === null) value = token;
|
||||
|
||||
output = output.replace(token, value);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
async function getTranslations() {
|
||||
const fallback = safe.JSON.parse(fs.readFileSync(path.join(TRANSLATION_FOLDER, 'en.json'), 'utf8'));
|
||||
if (!fallback) debug(`getTranslations: Fallback language en not found. ${safe.error.message}`);
|
||||
|
||||
const lang = await cloudron.getLanguage();
|
||||
|
||||
const translations = safe.JSON.parse(fs.readFileSync(path.join(TRANSLATION_FOLDER, lang + '.json'), 'utf8'));
|
||||
if (!translations) debug(`getTranslations: Requested language ${lang} not found. ${safe.error.message}`);
|
||||
|
||||
return { translations: translations || {}, fallback: fallback || {} };
|
||||
}
|
||||
|
||||
async function listLanguages() {
|
||||
const [error, result] = await safe(fs.promises.readdir(TRANSLATION_FOLDER));
|
||||
if (error) {
|
||||
debug('listLanguages: Failed to list translations. %o', error);
|
||||
return [ 'en' ]; // we always return english to avoid dashboard breakage
|
||||
}
|
||||
|
||||
const jsonFiles = result.filter(function (file) { return path.extname(file) === '.json'; });
|
||||
const languages = jsonFiles.map(function (file) { return path.basename(file, '.json'); });
|
||||
return languages;
|
||||
}
|
||||
Reference in New Issue
Block a user