|
2 | 2 |
|
3 | 3 | const fs = require('fs'); |
4 | 4 | const path = require('path'); |
| 5 | +const axios = require('axios'); |
5 | 6 |
|
6 | 7 | const appLocalesFolder = path.resolve(__dirname, '../static/locales'); |
7 | | -const pogoLocalesFolder = path.resolve(__dirname, '../node_modules/pogo-translations/static/locales'); |
8 | 8 |
|
9 | | -fs.readdir(appLocalesFolder, (err, files) => { |
10 | | - let pogoLocalesFiles = []; |
11 | | - |
12 | | - if (fs.existsSync(pogoLocalesFolder)) { |
13 | | - pogoLocalesFiles = fs.readdirSync(pogoLocalesFolder); |
14 | | - } |
15 | | - |
16 | | - files.filter(file => { return file.startsWith('_'); }).forEach(file => { |
17 | | - const locale = path.basename(file, '.json').replace('_', ''); |
18 | | - const localeFile = locale + '.json'; |
19 | | - let translations = {}; |
20 | | - |
21 | | - console.log('Creating locale', locale); |
22 | | - |
23 | | - if (pogoLocalesFiles.includes(localeFile)) { |
24 | | - console.log('Found pogo-translations for locale', locale); |
25 | | - |
26 | | - const pogoTranslations = fs.readFileSync( |
27 | | - path.resolve(pogoLocalesFolder, localeFile), |
28 | | - { encoding: 'utf8', flag: 'r' } |
| 9 | +module.exports.locales = async function locales() { |
| 10 | + const localTranslations = await fs.promises.readdir(appLocalesFolder); |
| 11 | + const englishRef = fs.readFileSync(path.resolve(appLocalesFolder, '_en.json'), { encoding: 'utf8', flag: 'r' }); |
| 12 | + |
| 13 | + await Promise.all(localTranslations.map(async locale => { |
| 14 | + if (locale.startsWith('_')) { |
| 15 | + const mapJsTranslations = fs.readFileSync(path.resolve(appLocalesFolder, locale), { encoding: 'utf8', flag: 'r' }); |
| 16 | + const baseName = locale.replace('.json', '').replace('_', ''); |
| 17 | + const trimmedRemoteFiles = {}; |
| 18 | + |
| 19 | + try { |
| 20 | + const { data } = await axios.get(`https://raw.githubusercontent.com/WatWowMap/pogo-translations/master/static/locales/${baseName}.json`); |
| 21 | + |
| 22 | + Object.keys(data).forEach(key => { |
| 23 | + if (!key.startsWith('desc_') && !key.startsWith('pokemon_category_')) { |
| 24 | + trimmedRemoteFiles[key] = data[key]; |
| 25 | + } |
| 26 | + }); |
| 27 | + } catch (e) { |
| 28 | + console.warn(e, '\n', locale); |
| 29 | + } |
| 30 | + |
| 31 | + const finalTranslations = { |
| 32 | + ...JSON.parse(englishRef), |
| 33 | + ...JSON.parse(mapJsTranslations), |
| 34 | + ...trimmedRemoteFiles, |
| 35 | + }; |
| 36 | + fs.writeFile( |
| 37 | + path.resolve(appLocalesFolder, `${baseName}.json`), |
| 38 | + JSON.stringify(finalTranslations, null, 2), |
| 39 | + 'utf8', |
| 40 | + () => { }, |
29 | 41 | ); |
30 | | - translations = JSON.parse(pogoTranslations.toString()); |
| 42 | + console.log('localeFile', 'file saved.'); |
31 | 43 | } |
32 | | - |
33 | | - if (locale !== 'en') { |
34 | | - // include en as fallback first |
35 | | - const appTransFallback = fs.readFileSync( |
36 | | - path.resolve(appLocalesFolder, '_en.json'), |
37 | | - { encoding: 'utf8', flag: 'r' } |
38 | | - ); |
39 | | - translations = Object.assign(translations, JSON.parse(appTransFallback.toString())); |
40 | | - } |
41 | | - |
42 | | - const appTranslations = fs.readFileSync(path.resolve(appLocalesFolder, file), { encoding: 'utf8', flag: 'r' }); |
43 | | - translations = Object.assign(translations, JSON.parse(appTranslations.toString())); |
44 | | - |
45 | | - fs.writeFile( |
46 | | - path.resolve(appLocalesFolder, localeFile), |
47 | | - JSON.stringify(translations, null, 2), |
48 | | - 'utf8', |
49 | | - () => {} |
50 | | - ); |
51 | | - console.log(localeFile, 'file saved.'); |
52 | | - }); |
53 | | -}); |
| 44 | + })); |
| 45 | +}; |
0 commit comments