lint: src/node/hooks/i18n.js

This commit is contained in:
John McLear 2021-01-21 21:06:52 +00:00 committed by Richard Hansen
parent 2dec36bfd7
commit 6054f6d93f

View file

@ -1,24 +1,23 @@
'use strict';
const languages = require('languages4translatewiki'); const languages = require('languages4translatewiki');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const _ = require('underscore'); const _ = require('underscore');
const npm = require('npm'); const npm = require('npm');
const plugins = require('ep_etherpad-lite/static/js/pluginfw/plugin_defs.js').plugins; const plugins = require('../../static/js/pluginfw/plugin_defs.js').plugins;
const semver = require('semver');
const existsSync = require('../utils/path_exists'); const existsSync = require('../utils/path_exists');
const settings = require('../utils/Settings') const settings = require('../utils/Settings');
;
// returns all existing messages merged together and grouped by langcode // returns all existing messages merged together and grouped by langcode
// {es: {"foo": "string"}, en:...} // {es: {"foo": "string"}, en:...}
function getAllLocales() { const getAllLocales = () => {
const locales2paths = {}; const locales2paths = {};
// Puts the paths of all locale files contained in a given directory // Puts the paths of all locale files contained in a given directory
// into `locales2paths` (files from various dirs are grouped by lang code) // into `locales2paths` (files from various dirs are grouped by lang code)
// (only json files with valid language code as name) // (only json files with valid language code as name)
function extractLangs(dir) { const extractLangs = (dir) => {
if (!existsSync(dir)) return; if (!existsSync(dir)) return;
let stat = fs.lstatSync(dir); let stat = fs.lstatSync(dir);
if (!stat.isDirectory() || stat.isSymbolicLink()) return; if (!stat.isDirectory() || stat.isSymbolicLink()) return;
@ -31,12 +30,12 @@ function getAllLocales() {
const ext = path.extname(file); const ext = path.extname(file);
const locale = path.basename(file, ext).toLowerCase(); const locale = path.basename(file, ext).toLowerCase();
if ((ext == '.json') && languages.isValid(locale)) { if ((ext === '.json') && languages.isValid(locale)) {
if (!locales2paths[locale]) locales2paths[locale] = []; if (!locales2paths[locale]) locales2paths[locale] = [];
locales2paths[locale].push(file); locales2paths[locale].push(file);
} }
}); });
} };
// add core supported languages first // add core supported languages first
extractLangs(`${npm.root}/ep_etherpad-lite/locales`); extractLangs(`${npm.root}/ep_etherpad-lite/locales`);
@ -78,29 +77,29 @@ function getAllLocales() {
} }
return locales; return locales;
} };
// returns a hash of all available languages availables with nativeName and direction // returns a hash of all available languages availables with nativeName and direction
// e.g. { es: {nativeName: "español", direction: "ltr"}, ... } // e.g. { es: {nativeName: "español", direction: "ltr"}, ... }
function getAvailableLangs(locales) { const getAvailableLangs = (locales) => {
const result = {}; const result = {};
_.each(_.keys(locales), (langcode) => { _.each(_.keys(locales), (langcode) => {
result[langcode] = languages.getLanguageInfo(langcode); result[langcode] = languages.getLanguageInfo(langcode);
}); });
return result; return result;
} };
// returns locale index that will be served in /locales.json // returns locale index that will be served in /locales.json
const generateLocaleIndex = function (locales) { const generateLocaleIndex = (locales) => {
const result = _.clone(locales); // keep English strings const result = _.clone(locales); // keep English strings
_.each(_.keys(locales), (langcode) => { _.each(_.keys(locales), (langcode) => {
if (langcode != 'en') result[langcode] = `locales/${langcode}.json`; if (langcode !== 'en') result[langcode] = `locales/${langcode}.json`;
}); });
return JSON.stringify(result); return JSON.stringify(result);
}; };
exports.expressCreateServer = function (n, args, cb) { exports.expressCreateServer = (n, args, cb) => {
// regenerate locales on server restart // regenerate locales on server restart
const locales = getAllLocales(); const locales = getAllLocales();
const localeIndex = generateLocaleIndex(locales); const localeIndex = generateLocaleIndex(locales);