-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
languages.js
83 lines (70 loc) · 2.18 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const { paths } = require('./constants');
const plugins = require('./plugins');
const Languages = module.exports;
const languagesPath = path.join(__dirname, '../build/public/language');
const files = fs.readdirSync(path.join(paths.nodeModules, '/timeago/locales'));
Languages.timeagoCodes = files.filter(f => f.startsWith('jquery.timeago')).map(f => f.split('.')[2]);
Languages.get = async function (language, namespace) {
const data = await fs.promises.readFile(path.join(languagesPath, language, `${namespace}.json`), 'utf8');
const parsed = JSON.parse(data) || {};
const result = await plugins.hooks.fire('filter:languages.get', {
language,
namespace,
data: parsed,
});
return result.data;
};
let codeCache = null;
Languages.listCodes = async function () {
if (codeCache && codeCache.length) {
return codeCache;
}
try {
const file = await fs.promises.readFile(path.join(languagesPath, 'metadata.json'), 'utf8');
const parsed = JSON.parse(file);
codeCache = parsed.languages;
return parsed.languages;
} catch (err) {
if (err.code === 'ENOENT') {
return [];
}
throw err;
}
};
let listCache = null;
Languages.list = async function () {
if (listCache && listCache.length) {
return listCache;
}
const codes = await Languages.listCodes();
let languages = await Promise.all(codes.map(async (folder) => {
try {
const configPath = path.join(languagesPath, folder, 'language.json');
const file = await fs.promises.readFile(configPath, 'utf8');
const lang = JSON.parse(file);
return lang;
} catch (err) {
if (err.code === 'ENOENT') {
return;
}
throw err;
}
}));
// filter out invalid ones
languages = languages.filter(lang => lang && lang.code && lang.name && lang.dir);
listCache = languages;
return languages;
};
Languages.userTimeagoCode = async function (userLang) {
const languageCodes = await Languages.listCodes();
const timeagoCode = utils.userLangToTimeagoCode(userLang);
if (languageCodes.includes(userLang) && Languages.timeagoCodes.includes(timeagoCode)) {
return timeagoCode;
}
return '';
};
require('./promisify')(Languages);