Skip to content

Commit

Permalink
Fix locale fetching in v2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobmarin committed Jun 5, 2021
1 parent 2a9483f commit 744ee63
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
3 changes: 3 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -122,5 +124,6 @@ module.exports = {
end,
getHost,
getMeetingID,
getVersion,
getJoinURL,
};
89 changes: 75 additions & 14 deletions lib/locales.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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}`);
Expand Down

0 comments on commit 744ee63

Please sign in to comment.