Make building locales better (#4427)

This commit is contained in:
Acid Chicken (硫酸鶏) 2019-03-06 23:28:50 +09:00 committed by syuilo
parent 4e23792d02
commit e2a5363ec0
4 changed files with 47 additions and 30 deletions

View File

@ -5,7 +5,6 @@
import * as gulp from 'gulp'; import * as gulp from 'gulp';
import * as gutil from 'gulp-util'; import * as gutil from 'gulp-util';
import * as ts from 'gulp-typescript'; import * as ts from 'gulp-typescript';
const yaml = require('gulp-yaml');
const sourcemaps = require('gulp-sourcemaps'); const sourcemaps = require('gulp-sourcemaps');
import tslint from 'gulp-tslint'; import tslint from 'gulp-tslint';
const cssnano = require('gulp-cssnano'); const cssnano = require('gulp-cssnano');
@ -126,12 +125,6 @@ gulp.task('copy:client', () =>
.pipe(gulp.dest('./built/client/assets/')) .pipe(gulp.dest('./built/client/assets/'))
); );
gulp.task('locales', () =>
gulp.src('./locales/*.yml')
.pipe(yaml({ schema: 'DEFAULT_SAFE_SCHEMA' }))
.pipe(gulp.dest('./built/client/assets/locales/'))
);
gulp.task('doc', () => gulp.task('doc', () =>
gulp.src('./src/docs/**/*.styl') gulp.src('./src/docs/**/*.styl')
.pipe(stylus()) .pipe(stylus())
@ -149,7 +142,6 @@ gulp.task('build', gulp.parallel(
'build:ts', 'build:ts',
'build:copy', 'build:copy',
'build:client', 'build:client',
'locales',
'doc' 'doc'
)); ));

View File

@ -5,22 +5,45 @@
const fs = require('fs'); const fs = require('fs');
const yaml = require('js-yaml'); const yaml = require('js-yaml');
const langs = ['de-DE', 'en-US', 'fr-FR', 'ja-JP', 'ja-KS', 'pl-PL', 'es-ES', 'nl-NL', 'zh-CN', 'ko-KR']; const merge = (...args) => args.reduce((a, c) => ({
...a,
...c,
...Object.entries(a)
.filter(([k]) => c && typeof c[k] === 'object')
.reduce((a, [k, v]) => (a[k] = merge(v, c[k]), a), {})
}), {});
const loadLocale = lang => yaml.safeLoad(fs.readFileSync(`${__dirname}/${lang}.yml`, 'utf-8')); const languages = [
const locales = langs 'de-DE',
.map(lang => [lang, loadLocale(lang)]) 'en-US',
.map(([lang, locale], _, locales) => { 'es-ES',
switch (lang) { 'fr-FR',
case 'ja-JP': return [lang, locale]; 'ja-JP',
case 'en-US': return [lang, { ...locales['ja-JP'], ...locale }]; 'ja-KS',
default: return [lang, { 'ko-KR',
...(lang.startsWith('ja-') ? {} : locales['en-US']), 'nl-NL',
...locales['ja-JP'], 'pl-PL',
...locale 'zh-CN',
}]; ];
const primaries = {
'ja': 'JP',
};
const locales = languages.reduce((a, c) => (a[c] = yaml.safeLoad(fs.readFileSync(`${__dirname}/${c}.yml`, 'utf-8')) || {}, a), {});
module.exports = Object.entries(locales)
.reduce((a, [k ,v]) => (a[k] = (() => {
const [lang] = k.split('-');
switch (k) {
case 'ja-JP': return v;
case 'ja-KS':
case 'en-US': return merge(locales['ja-JP'], v);
default: return merge(
locales['ja-JP'],
locales['en-US'],
locales[`${lang}-${primaries[lang]}`] || {},
v
);
} }
}) })(), a), {});
.map(([lang, locale]) => ({ [lang]: loadLocale(lang) }));
module.exports = locales.reduce((a, b) => ({ ...a, ...b }));

View File

@ -136,7 +136,6 @@
"gulp-typescript": "5.0.0", "gulp-typescript": "5.0.0",
"gulp-uglify": "3.0.1", "gulp-uglify": "3.0.1",
"gulp-util": "3.0.8", "gulp-util": "3.0.8",
"gulp-yaml": "2.0.3",
"hard-source-webpack-plugin": "0.13.1", "hard-source-webpack-plugin": "0.13.1",
"html-minifier": "3.5.21", "html-minifier": "3.5.21",
"http-signature": "1.2.0", "http-signature": "1.2.0",

View File

@ -123,16 +123,19 @@ module.exports = {
_COPYRIGHT_: JSON.stringify(constants.copyright), _COPYRIGHT_: JSON.stringify(constants.copyright),
_VERSION_: JSON.stringify(meta.version), _VERSION_: JSON.stringify(meta.version),
_CODENAME_: JSON.stringify(codename), _CODENAME_: JSON.stringify(codename),
_LANGS_: JSON.stringify(Object.keys(locales).map(l => [l, locales[l].meta.lang])), _LANGS_: JSON.stringify(Object.entries(locales).map(([k, v]: [string, any]) => [k, v && v.meta && v.meta.lang])),
_ENV_: JSON.stringify(process.env.NODE_ENV) _ENV_: JSON.stringify(process.env.NODE_ENV)
}), }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development') 'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development')
}), }),
new WebpackOnBuildPlugin((stats: any) => { new WebpackOnBuildPlugin((stats: any) => {
fs.writeFileSync('./built/client/meta.json', JSON.stringify({ fs.writeFileSync('./built/client/meta.json', JSON.stringify({ version: meta.version }), 'utf-8');
version: meta.version
}), 'utf-8'); fs.mkdirSync('./built/client/assets/locales', { recursive: true })
for (const [lang, locale] of Object.entries(locales))
fs.writeFileSync(`./built/client/assets/locales/${lang}.json`, JSON.stringify(locale), 'utf-8');
}), }),
new VueLoaderPlugin(), new VueLoaderPlugin(),
new webpack.optimize.ModuleConcatenationPlugin() new webpack.optimize.ModuleConcatenationPlugin()