WXL
2025-12-27 05e6b08007a86b5b10c680babc9c3bcc3a1a201b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const debug = require('debug')('vue-cli-plugin-i18n:generator')
const {
  checkInstalled,
  exists,
  writeFile,
  readEnv,
  buildEnvContent
} = require('../utils')
const chalk = require(require.resolve('chalk'))
 
module.exports = (api, options, rootOptions) => {
  const {
    locale,
    fallbackLocale,
    localeDir,
    enableLegacy,
    enableInSFC,
    enableBridge
  } = options
  debug('options', options)
  debug('rootOptions', rootOptions)
  const isVue3 = rootOptions && rootOptions.vueVersion === '3'
 
  try {
    const lang = api.hasPlugin('typescript') ? 'ts' : 'js'
    const classComponent =
      checkInstalled('./node_modules/vue-class-component/package.json') &&
      checkInstalled('./node_modules/vue-property-decorator/package.json')
    const additionalOptions = {
      ...options,
      ...{ lang, localeDir, classComponent, enableLegacy }
    }
    debug('additionalOptions', additionalOptions)
 
    /*
     * extend packages
     */
 
    const pkg = {
      scripts: {
        'i18n:report': `vue-cli-service i18n:report --src "./src/**/*.?(js|vue)" --locales "${`./src/${localeDir}/**/*.json`}"`
      },
      dependencies: {},
      devDependencies: {},
      vue: {
        pluginOptions: {
          i18n: {
            locale,
            fallbackLocale,
            localeDir
          }
        }
      }
    }
 
    if (isVue3) {
      pkg.dependencies['vue-i18n'] = '^9.1.0'
      pkg.devDependencies['@intlify/vue-i18n-loader'] = '^3.0.0'
      pkg.vue.pluginOptions.i18n['enableLegacy'] = enableLegacy
      pkg.vue.pluginOptions.i18n['runtimeOnly'] = false
      pkg.vue.pluginOptions.i18n['compositionOnly'] = !!enableLegacy
      pkg.vue.pluginOptions.i18n['fullInstall'] = true
    } else {
      pkg.dependencies['vue-i18n'] = '^8.26.3'
      if (enableInSFC) {
        pkg.devDependencies['@intlify/vue-i18n-loader'] = '^1.1.0'
      }
      pkg.vue.pluginOptions.i18n['enableInSFC'] = enableInSFC
      if (enableBridge) {
        pkg.devDependencies['@intlify/vue-i18n-loader'] = '^3.2.0'
        pkg.dependencies['vue-i18n-bridge'] = '^9.2.0-beta.10'
        pkg.vue.pluginOptions.i18n['includeLocales'] = false
      }
      pkg.vue.pluginOptions.i18n['enableBridge'] = enableBridge
    }
    debug('pkg', pkg)
 
    api.extendPackage(pkg)
 
    /*
     * Modify entry file
     */
 
    api.injectImports(api.entryFile, `import i18n from './i18n'`)
    if (isVue3) {
      api.transformScript(api.entryFile, require('./injectUseI18n'))
    } else {
      api.injectRootOptions(api.entryFile, `i18n,`)
    }
 
    /*
     * render templates
     */
 
    const renderOptions = { ...additionalOptions }
 
    // i18n templates for program language
    if (isVue3) {
      api.render(`./templates-vue3/${lang}`, renderOptions)
    } else {
      api.render(`./templates/${lang}`, renderOptions)
    }
 
    // locale messages
    const defaultLocaleMessages = JSON.stringify(
      {
        message: 'hello i18n !!'
      },
      null,
      2
    )
    api.render((files, render) => {
      files[`src/${localeDir}/${locale}.json`] = render(
        defaultLocaleMessages,
        renderOptions
      )
    })
 
    // locale messages in SFC examples
    if (isVue3) {
      if (enableLegacy) {
        api.render('./templates-vue3/sfc/legacy', renderOptions)
      } else {
        api.render('./templates-vue3/sfc/composition', renderOptions)
      }
    } else {
      if (enableInSFC) {
        api.render('./templates/sfc', renderOptions)
      }
    }
  } catch (e) {
    api.exitLog(
      `unexpected error in vue-cli-plugin-i18n: ${e.message}`,
      'error'
    )
    return
  }
 
  api.onCreateComplete(() => {
    debug('onCreateComplete called')
    const envPath = api.resolve('.env')
    const envVars = exists(envPath) ? readEnv(envPath) : {}
 
    if (envVars['VUE_APP_I18N_LOCALE']) {
      api.exitLog(`overwrite VUE_APP_I18N_LOCALE at ${envPath}`, 'info')
    }
    envVars['VUE_APP_I18N_LOCALE'] = locale
 
    if (envVars['VUE_APP_I18N_FALLBACK_LOCALE']) {
      api.exitLog(
        `overwrite VUE_APP_I18N_FALLBACK_LOCALE at ${envPath}`,
        'info'
      )
    }
    envVars['VUE_APP_I18N_FALLBACK_LOCALE'] = fallbackLocale
 
    if (!writeFile(envPath, buildEnvContent(envVars))) {
      api.exitLog(`cannot write to ${envPath}`, 'error')
      return
    }
 
    if (
      enableBridge &&
      checkInstalled('./node_modules/@vue/composition-api/package.json')
    ) {
      api.exitLog(
        chalk.yellow.underline(
          `You need to install '@vue/composition-api' https://github.com/vuejs/composition-api`
        )
      )
    }
  })
}