From 744ee63514c25747b5ec9c07b40e996d839c32e3 Mon Sep 17 00:00:00 2001 From: Pedro Beschorner Marin Date: Sat, 5 Jun 2021 14:16:43 -0300 Subject: [PATCH] Fix locale fetching in v2.3 --- lib/api.js | 3 ++ lib/locales.js | 89 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/lib/api.js b/lib/api.js index 4e8b666..4877440 100644 --- a/lib/api.js +++ b/lib/api.js @@ -34,6 +34,8 @@ const getSecret = (options) => options.secret || config.api.secret; const getMeetingID = (options) => options.room || config.url.meeting.name; +const getVersion = (options) => options.version || config.url.version; + const getPassword = (role, options) => { return options.password && options.password[role] ? options.password[role] : config.api.password[role]; @@ -122,5 +124,6 @@ module.exports = { end, getHost, getMeetingID, + getVersion, getJoinURL, }; diff --git a/lib/locales.js b/lib/locales.js index 4e22d4d..7dba985 100644 --- a/lib/locales.js +++ b/lib/locales.js @@ -1,19 +1,78 @@ const axios = require('axios'); const conf = require('./conf'); const logger = require('./logger'); +const api = require('./api'); + +const DEFAULT_LANGUAGE = 'en'; const { config } = conf; +const fetchLocale = async (client) => { + const { lang } = config.browser; + logger.info(`Fetching ${lang} locale`); + const url = `${client}/locale?locale=${lang}`; + + return axios.get(url); +}; + +const fetchLocaleJSON = async (client, locale) => { + const json = `${client}/locales/${locale}.json`; + let data = {}; + await axios.get(json).then(response => { + if (response.status === 200) data = response.data; + else logger.error(`Could not fetch ${json} data`); + }).catch(error => { + logger.error(error); + }); + + return data; +}; + +const fetchFallbackLocale = async (client) => { + logger.debug(`Fetching ${DEFAULT_LANGUAGE} fallback locale`); + const fallback = await fetchLocaleJSON(client, DEFAULT_LANGUAGE); + + return fallback; +}; + +const fetchRegionLocale = async (client, locale) => { + let region = {}; + + if (locale) { + logger.debug(`Fetching ${locale} region locale`); + region = await fetchLocaleJSON(client, locale); + } + + return region; +}; + +const fetchNormalizedLocale = async (client, locale) => { + let normalized = {}; + + if (locale) { + logger.debug(`Fetching ${locale} normalized locale`); + normalized = await fetchLocaleJSON(client, locale); + } + + return normalized; +}; + +const mergeLocales = (...locales) => { + let data = {}; + for (const index in locales) { + data = Object.assign(data, locales[index]); + } + + return data; +}; + 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 host = api.getHost(options); const client = `${host}/${config.url.basename}`; - const url = `${client}/locale?locale=${lang}`; - let locale; - await axios.get(url).then(async response => { + let locale = {}; + await fetchLocale(client).then(async response => { + const version = api.getVersion(options); switch (version) { case '2.2': const { messages } = response.data; @@ -24,13 +83,15 @@ module.exports = { } 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); - }); + const { + normalizedLocale, + regionDefaultLocale, + } = response.data; + + const fallback = await fetchFallbackLocale(client); + const region = await fetchRegionLocale(client, regionDefaultLocale); + const normalized = await fetchNormalizedLocale(client, normalizedLocale); + locale = mergeLocales(fallback, region, normalized); break; default: logger.error(`Invalid BigBlueButton's server version: ${version}`);