12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- const fs = require('fs')
- const path = require('path')
- const locales = ['en', 'ja']
- const merge = (source, resource, target) => {
- for (let key in source) {
- if (typeof source[key] === 'string') {
- target[key] = resource[key] == void 0 ? source[key] : resource[key]
- } else {
- target[key] = {}
- resource[key] = resource[key] || {}
- merge(source[key], resource[key], target[key])
- }
- }
- }
- const combine = (source, resource, target) => {
- merge(source, resource, target)
- }
- function exec() {
- var source = null
- fs.readFile(path.join(__dirname, '..', 'src', 'locales', 'zh.json'), (err, data) => {
- if (err) {
- return
- }
- source = JSON.parse(data.toString())
- fs.writeFile(path.join(__dirname, '..', 'public', '__langs', 'locales', 'zh.json'), JSON.stringify(source, null, 4), () => {})
- locales.forEach(locale => {
- try {
- fs.readFile(path.join(__dirname, '..', 'src', 'locales', locale + '.json'), (err, response) => {
- fs.readFile(path.join(__dirname, '..', 'src', 'locales', 'zh.json'), (err, data) => {
- if (err) {
- return
- }
- var target = {}
- // var source = JSON.parse(data.toString())
- var resource = JSON.parse(response.toString())
- combine(source, resource, target)
- fs.writeFile(path.join(__dirname, '..', 'src', 'locales', locale + '.json'), JSON.stringify(target, null, 4), () => {})
- fs.writeFile(path.join(__dirname, '..', 'public', '__langs', 'locales', locale + '.json'), JSON.stringify(target, null, 4), () => {})
- })
- })
- } catch (error) {
- console.log(error)
- }
- })
- })
- }
- exec()
|