pad.libre-service.eu-etherpad/src/node/hooks/i18n.js

127 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-11-23 19:24:19 +01:00
const languages = require('languages4translatewiki');
const fs = require('fs');
const path = require('path');
const _ = require('underscore');
const npm = require('npm');
const plugins = require('ep_etherpad-lite/static/js/pluginfw/plugin_defs.js').plugins;
const semver = require('semver');
const existsSync = require('../utils/path_exists');
const settings = require('../utils/Settings')
;
2012-11-10 14:12:17 +01:00
2012-12-23 22:43:32 +01:00
// returns all existing messages merged together and grouped by langcode
// {es: {"foo": "string"}, en:...}
function getAllLocales() {
2020-11-23 19:24:19 +01:00
const locales2paths = {};
// Puts the paths of all locale files contained in a given directory
2012-12-23 22:43:32 +01:00
// into `locales2paths` (files from various dirs are grouped by lang code)
// (only json files with valid language code as name)
function extractLangs(dir) {
2020-11-23 19:24:19 +01:00
if (!existsSync(dir)) return;
let stat = fs.lstatSync(dir);
if (!stat.isDirectory() || stat.isSymbolicLink()) return;
2020-11-23 19:24:19 +01:00
fs.readdirSync(dir).forEach((file) => {
file = path.resolve(dir, file);
stat = fs.lstatSync(file);
if (stat.isDirectory() || stat.isSymbolicLink()) return;
2020-11-23 19:24:19 +01:00
const ext = path.extname(file);
const locale = path.basename(file, ext).toLowerCase();
2012-12-17 18:18:07 +01:00
if ((ext == '.json') && languages.isValid(locale)) {
2020-11-23 19:24:19 +01:00
if (!locales2paths[locale]) locales2paths[locale] = [];
locales2paths[locale].push(file);
2012-12-17 18:18:07 +01:00
}
});
}
2020-11-23 19:24:19 +01:00
// add core supported languages first
extractLangs(`${npm.root}/ep_etherpad-lite/locales`);
2020-11-23 19:24:19 +01:00
// add plugins languages (if any)
for (const pluginName in plugins) extractLangs(path.join(npm.root, pluginName, 'locales'));
2012-12-17 18:18:07 +01:00
// Build a locale index (merge all locale data other than user-supplied overrides)
2020-11-23 19:24:19 +01:00
const locales = {};
_.each(locales2paths, (files, langcode) => {
locales[langcode] = {};
2012-12-17 18:18:07 +01:00
2020-11-23 19:24:19 +01:00
files.forEach((file) => {
let fileContents;
try {
2020-11-23 19:24:19 +01:00
fileContents = JSON.parse(fs.readFileSync(file, 'utf8'));
} catch (err) {
console.error(`failed to read JSON file ${file}: ${err}`);
throw err;
}
2012-12-23 22:43:32 +01:00
_.extend(locales[langcode], fileContents);
2012-12-17 18:18:07 +01:00
});
});
// Add custom strings from settings.json
// Since this is user-supplied, we'll do some extra sanity checks
const wrongFormatErr = Error(
2020-11-23 19:24:19 +01:00
'customLocaleStrings in wrong format. See documentation ' +
'for Customization for Administrators, under Localization.');
if (settings.customLocaleStrings) {
2020-11-23 19:24:19 +01:00
if (typeof settings.customLocaleStrings !== 'object') throw wrongFormatErr;
_.each(settings.customLocaleStrings, (overrides, langcode) => {
if (typeof overrides !== 'object') throw wrongFormatErr;
_.each(overrides, (localeString, key) => {
if (typeof localeString !== 'string') throw wrongFormatErr;
locales[langcode][key] = localeString;
});
});
}
return locales;
2012-12-17 18:18:07 +01:00
}
// returns a hash of all available languages availables with nativeName and direction
// e.g. { es: {nativeName: "español", direction: "ltr"}, ... }
function getAvailableLangs(locales) {
2020-11-23 19:24:19 +01:00
const result = {};
_.each(_.keys(locales), (langcode) => {
result[langcode] = languages.getLanguageInfo(langcode);
2012-12-17 18:18:07 +01:00
});
return result;
}
// returns locale index that will be served in /locales.json
2020-11-23 19:24:19 +01:00
const generateLocaleIndex = function (locales) {
const result = _.clone(locales); // keep English strings
_.each(_.keys(locales), (langcode) => {
if (langcode != 'en') result[langcode] = `locales/${langcode}.json`;
2012-12-17 18:18:07 +01:00
});
return JSON.stringify(result);
2020-11-23 19:24:19 +01:00
};
2012-12-04 12:12:58 +01:00
2012-11-10 14:12:17 +01:00
2020-11-23 19:24:19 +01:00
exports.expressCreateServer = function (n, args, cb) {
// regenerate locales on server restart
const locales = getAllLocales();
const localeIndex = generateLocaleIndex(locales);
exports.availableLangs = getAvailableLangs(locales);
2012-12-17 18:18:07 +01:00
2020-11-23 19:24:19 +01:00
args.app.get('/locales/:locale', (req, res) => {
// works with /locale/en and /locale/en.json requests
const locale = req.params.locale.split('.')[0];
2012-12-17 18:18:07 +01:00
if (exports.availableLangs.hasOwnProperty(locale)) {
2012-12-19 19:49:25 +01:00
res.setHeader('Content-Type', 'application/json; charset=utf-8');
2020-11-23 19:24:19 +01:00
res.send(`{"${locale}":${JSON.stringify(locales[locale])}}`);
2012-12-17 18:18:07 +01:00
} else {
res.status(404).send('Language not available');
2012-12-17 18:18:07 +01:00
}
2020-11-23 19:24:19 +01:00
});
2020-11-23 19:24:19 +01:00
args.app.get('/locales.json', (req, res) => {
2012-12-19 19:49:25 +01:00
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.send(localeIndex);
2020-11-23 19:24:19 +01:00
});
return cb();
2020-11-23 19:24:19 +01:00
};