forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-localizations.js
86 lines (71 loc) · 2.59 KB
/
generate-localizations.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
84
85
86
/**
* Copyright (c) ppy Pty Ltd <[email protected]>.
*
* This file is part of osu!web. osu!web is distributed with the hope of
* attracting more community contributions to the core ecosystem of osu!.
*
* osu!web is free software: you can redistribute it and/or modify
* it under the terms of the Affero GNU General Public License version 3
* as published by the Free Software Foundation.
*
* osu!web is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with osu!web. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const { spawnSync } = require('child_process');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const mkdirp = require('mkdirp');
const buildPath = path.resolve(__dirname, 'resources/assets/build');
const localesPath = path.resolve(buildPath, 'locales');
const messagesPath = path.resolve(buildPath, 'messages.json');
function extractLanguages() {
console.log('Extracting localizations...')
const messages = getAllMesssages();
const languages = new Map();
for (const key in messages) {
const index = key.indexOf('.');
const language = key.substring(0, index);
if (!languages.has(language)) {
languages.set(language, {});
}
languages.get(language)[key] = messages[key];
}
return languages;
}
function getAllMesssages() {
const content = fs.readFileSync(messagesPath);
return JSON.parse(content);
}
function generateTranslations()
{
spawnSync('php', ['artisan', 'lang:js', '--json', messagesPath], { stdio: 'inherit' });
}
function writeTranslations(languages)
{
for (const lang of languages.keys()) {
const json = JSON.stringify(languages.get(lang));
const filename = path.resolve(localesPath, `${lang}.js`);
const script = `(function() { 'use strict'; Object.assign(Lang.messages, ${json}); })();`;
fs.writeFileSync(filename, script);
console.log(`Created: ${filename}`);
}
}
// Remove previous existing files and ensure directory exists.
glob.sync(path.resolve(localesPath, '*.js')).forEach(fs.unlinkSync);
mkdirp.sync(localesPath);
generateTranslations();
writeTranslations(extractLanguages());
// copy lang.js
fs.copyFileSync(
path.resolve(__dirname, 'vendor/mariuzzo/laravel-js-localization/lib/lang.min.js'),
path.resolve(buildPath, 'lang.js')
);
// cleanup
fs.unlinkSync(messagesPath);
console.log(`Removed: ${messagesPath}`);