i18n.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const fs = require('fs');
  18. const path = require('path')
  19. const glob = require('globby')
  20. function moduleName (modules) {
  21. let filename = path.basename(modules)
  22. let parts = filename.split('.')
  23. parts.pop()
  24. filename = parts.join('.')
  25. return path.dirname(modules) + '/' + filename
  26. }
  27. const jsEntry = () => {
  28. const obj = {}
  29. const files = glob.sync([
  30. './src/js/conf/login/**/*.vue',
  31. './src/js/conf/login/**/*.js',
  32. './src/js/conf/home/**/**/**/**/**/**/**/**/*.vue',
  33. './src/js/conf/home/**/**/**/**/**/**/**/**/*.js',
  34. './src/js/module/**/**/**/**/**/*.vue',
  35. './src/js/module/**/**/**/**/**/*.js'
  36. ])
  37. files.forEach(val => {
  38. let parts = val.split(/[\\/]/)
  39. parts.shift()
  40. parts.shift()
  41. let modules = parts.join('/')
  42. let entry = moduleName(modules)
  43. obj[entry] = val
  44. })
  45. return obj
  46. }
  47. /* eslint-disable */
  48. let reg = /\$t\([\w,""''“”~\-\s.?!,。:;《》、\+\/<>()?!\u4e00-\u9fa5]*\)/g
  49. let map = {}
  50. let entryPathList = ''
  51. let matchPathList = ''
  52. let jsEntryObj = jsEntry()
  53. for (let i in jsEntryObj) {
  54. entryPathList += jsEntryObj[i] + '\n'
  55. let data = fs.readFileSync(path.join(jsEntryObj[i]), 'utf-8')
  56. if (reg.test(data)) {
  57. matchPathList += jsEntryObj[i] + '\n'
  58. let str = data.replace(/[""'']/g, '')
  59. str.replace(reg, function () {
  60. if (arguments && arguments[0]) {
  61. let key = arguments[0]
  62. key = key.substring(3, key.length - 1)
  63. map[key] = key
  64. }
  65. })
  66. }
  67. }
  68. let outPath = path.join(__dirname, '../src/js/module/i18n/locale/zh_CN.js')
  69. fs.unlink(outPath, (err) => {
  70. if (err) {
  71. console.error('删除zh_CN.js文件出错 -- \n', err)
  72. } else {
  73. console.log('删除zh_CN.js文件成功')
  74. }
  75. })
  76. fs.writeFile(outPath, 'export default ' + JSON.stringify(map, null, 2), function (err) {
  77. if (err) {
  78. console.error('写入zh_CN.js文件出错 -- \n', err)
  79. } else {
  80. console.log('写入zh_CN.js文件成功')
  81. }
  82. })