This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathlanguages.js
66 lines (54 loc) · 1.96 KB
/
languages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const debug = require('debug')('sentencecollector:languages');
const fetch = require('node-fetch');
const { Sentence } = require('./models');
const FALLBACK_LOCALE = 'en';
const MAX_CACHE_AGE = 30 * 60 * 1000;
const cache = {};
module.exports = {
FALLBACK_LOCALE,
getAllLanguages,
getLanguagesNotInPontoon,
};
async function getAllLanguages() {
if (typeof cache.lastCacheUpdate !== 'undefined' && Date.now() - cache.lastCacheUpdate <= MAX_CACHE_AGE) {
debug('RETURN_CACHED_LANGUAGES_LIST');
return cache.languages;
}
debug('FETCHING_NEW_LANGUAGES_LIST');
const fetchedLanguages = await fetchAllLanguages();
cache.languages = fetchedLanguages;
cache.lastCacheUpdate = Date.now();
return fetchedLanguages;
}
async function fetchAllLanguages() {
const pontoonLanguages = await fetchPontoonLanguages();
const allLanguages = pontoonLanguages.map(({ code, isRTL }) => {
return {
id: code,
isRTL,
};
});
return allLanguages;
}
async function getLanguagesNotInPontoon() {
const pontoonLanguages = await fetchPontoonLanguages();
const scLanguageCodes = await getLanguagesWithSentences();
const missingLanguages = scLanguageCodes.filter((code) => !pontoonLanguages.find((pontoonLang) => pontoonLang.code === code));
return missingLanguages;
}
async function fetchPontoonLanguages() {
const query = '{ project(slug: "common-voice") { localizations { locale { code, direction } } } }';
const pontoonResponse = await fetch(`https://pontoon.mozilla.org/graphql?query=${query}`);
const { data } = await pontoonResponse.json();
return data.project.localizations
.map(({ locale }) => ({
code: locale.code,
isRTL: locale.direction === 'RTL',
}))
.concat({ code: 'en', isRTL: false });
}
async function getLanguagesWithSentences() {
const distinct = await Sentence.aggregate('localeId', 'DISTINCT', { plain: false });
const uniqueLanguages = distinct.map((entry) => entry.DISTINCT);
return uniqueLanguages;
}