From e0c156d4aaf66de53e4b4c735150e9bd497b2243 Mon Sep 17 00:00:00 2001 From: Pedro Beschorner Marin Date: Sun, 30 May 2021 17:05:07 -0300 Subject: [PATCH] Create locales module --- lib/locales.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/run.js | 3 ++- lib/util.js | 37 ------------------------------------- 3 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 lib/locales.js diff --git a/lib/locales.js b/lib/locales.js new file mode 100644 index 0000000..4e22d4d --- /dev/null +++ b/lib/locales.js @@ -0,0 +1,44 @@ +const axios = require('axios'); +const conf = require('./conf'); +const logger = require('./logger'); + +const { config } = conf; + +module.exports = { + get: async (options) => { + const { lang } = config.browser; + logger.info(`Fetching ${lang} locale`); + const host = options.host || config.url.host; + const version = options.version || config.url.version; + const client = `${host}/${config.url.basename}`; + const url = `${client}/locale?locale=${lang}`; + let locale; + await axios.get(url).then(async response => { + switch (version) { + case '2.2': + const { messages } = response.data; + if (messages) { + locale = messages; + } else { + logger.error(`Missing locale messages`); + } + break; + case '2.3': + const { normalizedLocale } = response.data; + const json = `${client}/locales/${normalizedLocale}.json`; + await axios.get(json).then(response => { + locale = response.data; + }).catch(error => { + logger.error(error); + }); + break; + default: + logger.error(`Invalid BigBlueButton's server version: ${version}`); + } + }).catch(error => { + logger.error(error); + }); + + return locale; + }, +}; diff --git a/lib/run.js b/lib/run.js index 4df1335..41dbf87 100644 --- a/lib/run.js +++ b/lib/run.js @@ -2,6 +2,7 @@ const util = require('./util'); const pool = require('./pool'); const conf = require('./conf'); const logger = require('./logger'); +const locales = require('./locales'); const { api, bot, url, misc } = conf.config; @@ -37,7 +38,7 @@ const run = async (actions, options = {}) => { if (!success) return null; // Fetch the UI labels from locale - const locale = await util.locale(options); + const locale = await locales.get(options); let browsers = []; for (let i = 0; i < pool.population; i++) { diff --git a/lib/util.js b/lib/util.js index 5c035fe..30f2153 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,3 @@ -const axios = require('axios'); const faker = require('faker'); const conf = require('./conf'); const logger = require('./logger'); @@ -203,40 +202,4 @@ module.exports = { return hidden; }, - locale: async (options) => { - const { lang } = config.browser; - logger.info(`Fetching ${lang} locale`); - const host = options.host || config.url.host; - const version = options.version || config.url.version; - const client = `${host}/${config.url.basename}`; - const url = `${client}/locale?locale=${lang}`; - let locale; - await axios.get(url).then(async response => { - switch (version) { - case '2.2': - const { messages } = response.data; - if (messages) { - locale = messages; - } else { - logger.error(`Missing locale messages`); - } - break; - case '2.3': - const { normalizedLocale } = response.data; - const json = `${client}/locales/${normalizedLocale}.json`; - await axios.get(json).then(response => { - locale = response.data; - }).catch(error => { - logger.error(error); - }); - break; - default: - logger.error(`Invalid BigBlueButton's server version: ${version}`); - } - }).catch(error => { - logger.error(error); - }); - - return locale; - }, };