update-i18n.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const fs = require('fs')
  2. const path = require('path')
  3. const locales = ['en', 'ja']
  4. const merge = (source, resource, target) => {
  5. for (let key in source) {
  6. if (typeof source[key] === 'string') {
  7. target[key] = resource[key] == void 0 ? source[key] : resource[key]
  8. } else {
  9. target[key] = {}
  10. resource[key] = resource[key] || {}
  11. merge(source[key], resource[key], target[key])
  12. }
  13. }
  14. }
  15. const combine = (source, resource, target) => {
  16. merge(source, resource, target)
  17. }
  18. function exec() {
  19. var source = null
  20. fs.readFile(path.join(__dirname, '..', 'src', 'locales', 'zh.json'), (err, data) => {
  21. if (err) {
  22. return
  23. }
  24. source = JSON.parse(data.toString())
  25. fs.writeFile(path.join(__dirname, '..', 'public', '__langs', 'locales', 'zh.json'), JSON.stringify(source, null, 4), () => {})
  26. locales.forEach(locale => {
  27. try {
  28. fs.readFile(path.join(__dirname, '..', 'src', 'locales', locale + '.json'), (err, response) => {
  29. fs.readFile(path.join(__dirname, '..', 'src', 'locales', 'zh.json'), (err, data) => {
  30. if (err) {
  31. return
  32. }
  33. var target = {}
  34. // var source = JSON.parse(data.toString())
  35. var resource = JSON.parse(response.toString())
  36. combine(source, resource, target)
  37. fs.writeFile(path.join(__dirname, '..', 'src', 'locales', locale + '.json'), JSON.stringify(target, null, 4), () => {})
  38. fs.writeFile(path.join(__dirname, '..', 'public', '__langs', 'locales', locale + '.json'), JSON.stringify(target, null, 4), () => {})
  39. })
  40. })
  41. } catch (error) {
  42. console.log(error)
  43. }
  44. })
  45. })
  46. }
  47. exec()