From e2a5363ec067db5875aa101ca7a691d180c68c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Wed, 6 Mar 2019 23:28:50 +0900 Subject: [PATCH] Make building locales better (#4427) --- gulpfile.ts | 8 ------- locales/index.js | 57 +++++++++++++++++++++++++++++++++-------------- package.json | 1 - webpack.config.ts | 11 +++++---- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/gulpfile.ts b/gulpfile.ts index 56949a706..b2956c240 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -5,7 +5,6 @@ import * as gulp from 'gulp'; import * as gutil from 'gulp-util'; import * as ts from 'gulp-typescript'; -const yaml = require('gulp-yaml'); const sourcemaps = require('gulp-sourcemaps'); import tslint from 'gulp-tslint'; const cssnano = require('gulp-cssnano'); @@ -126,12 +125,6 @@ gulp.task('copy:client', () => .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.src('./src/docs/**/*.styl') .pipe(stylus()) @@ -149,7 +142,6 @@ gulp.task('build', gulp.parallel( 'build:ts', 'build:copy', 'build:client', - 'locales', 'doc' )); diff --git a/locales/index.js b/locales/index.js index 84081fc90..2fdf5aec0 100644 --- a/locales/index.js +++ b/locales/index.js @@ -5,22 +5,45 @@ const fs = require('fs'); 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 locales = langs - .map(lang => [lang, loadLocale(lang)]) - .map(([lang, locale], _, locales) => { - switch (lang) { - case 'ja-JP': return [lang, locale]; - case 'en-US': return [lang, { ...locales['ja-JP'], ...locale }]; - default: return [lang, { - ...(lang.startsWith('ja-') ? {} : locales['en-US']), - ...locales['ja-JP'], - ...locale - }]; +const languages = [ + 'de-DE', + 'en-US', + 'es-ES', + 'fr-FR', + 'ja-JP', + 'ja-KS', + 'ko-KR', + 'nl-NL', + 'pl-PL', + '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 + ); } - }) - .map(([lang, locale]) => ({ [lang]: loadLocale(lang) })); - -module.exports = locales.reduce((a, b) => ({ ...a, ...b })); + })(), a), {}); diff --git a/package.json b/package.json index 255b78492..3ce86545f 100644 --- a/package.json +++ b/package.json @@ -136,7 +136,6 @@ "gulp-typescript": "5.0.0", "gulp-uglify": "3.0.1", "gulp-util": "3.0.8", - "gulp-yaml": "2.0.3", "hard-source-webpack-plugin": "0.13.1", "html-minifier": "3.5.21", "http-signature": "1.2.0", diff --git a/webpack.config.ts b/webpack.config.ts index e1d7ad1af..b485b0745 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -123,16 +123,19 @@ module.exports = { _COPYRIGHT_: JSON.stringify(constants.copyright), _VERSION_: JSON.stringify(meta.version), _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) }), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development') }), new WebpackOnBuildPlugin((stats: any) => { - fs.writeFileSync('./built/client/meta.json', JSON.stringify({ - version: meta.version - }), 'utf-8'); + fs.writeFileSync('./built/client/meta.json', JSON.stringify({ 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 webpack.optimize.ModuleConcatenationPlugin()