WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')
 
test('javascript: legacy', async () => {
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: require('@vue/cli-service/generator'),
      options: { vueVersion: '3' }
    },
    {
      id: 'vue-cli-plugin-i18n',
      apply: require('../../generator'),
      options: { localeDir: 'locales', locale: 'en', enableLegacy: true }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.js']
  expect(i18n).toMatch(`import { createI18n } from 'vue-i18n'`)
  expect(i18n).toMatch(
    `const locales = require.context('./locales', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  expect(i18n).not.toMatch(`legacy: false`)
  const locale = files['src/locales/en.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const sfc = files['src/components/HelloI18n.vue']
  expect(sfc).toMatch(`<p>{{ $t('hello') }}</p>`)
  expect(sfc).toMatch(`import { defineComponent } from 'vue'`)
  expect(sfc).toMatch(`export default defineComponent({`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^9.1.0"`)
  expect(pack).toMatch(`"@intlify/vue-i18n-loader": "^3.0.0"`)
})
 
test('javascript: composition', async () => {
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: require('@vue/cli-service/generator'),
      options: { vueVersion: '3' }
    },
    {
      id: 'vue-cli-plugin-i18n',
      apply: require('../../generator'),
      options: { localeDir: 'locales', locale: 'en', enableLegacy: false }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.js']
  expect(i18n).toMatch(`import { createI18n } from 'vue-i18n'`)
  expect(i18n).toMatch(
    `const locales = require.context('./locales', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  expect(i18n).toMatch(`legacy: false`)
  const locale = files['src/locales/en.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const sfc = files['src/components/HelloI18n.vue']
  expect(sfc).toMatch(`<p>{{ t('hello') }}</p>`)
  expect(sfc).toMatch(`import { defineComponent } from 'vue'`)
  expect(sfc).toMatch(`import { useI18n } from 'vue-i18n'`)
  expect(sfc).toMatch(`export default defineComponent({`)
  expect(sfc).toMatch(`const { t } = useI18n({`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^9.1.0"`)
  expect(pack).toMatch(`"@intlify/vue-i18n-loader": "^3.0.0"`)
})
 
test('typescript: composition', async () => {
  const { files } = await generateWithPlugin([
    {
      id: '@vue/cli-service',
      apply: require('@vue/cli-service/generator'),
      options: { vueVersion: '3' }
    },
    {
      id: '@vue/cli-plugin-typescript',
      apply: () => {},
      options: {}
    },
    {
      id: 'vue-cli-plugin-i18n',
      apply: require('../../generator'),
      options: { localeDir: 'loc', locale: 'ja', enableLegacy: false }
    }
  ])
 
  // check files
  const i18n = files['src/i18n.ts']
  expect(i18n).toMatch(
    `import { createI18n, LocaleMessages, VueMessageType } from 'vue-i18n'`
  )
  expect(i18n).toMatch(
    `function loadLocaleMessages(): LocaleMessages<VueMessageType> {`
  )
  expect(i18n).toMatch(
    `const locales = require.context('./loc', true, /[A-Za-z0-9-_,\\s]+\\.json$/i)`
  )
  expect(i18n).toMatch(`const messages: LocaleMessages<VueMessageType> = {}`)
  const locale = files['src/loc/ja.json']
  expect(locale).toMatch(`{\n  "message": "hello i18n !!"\n}`)
  const sfc = files['src/components/HelloI18n.vue']
  expect(sfc).toMatch(`<p>{{ t('hello') }}</p>`)
  expect(sfc).toMatch(`<script lang="ts">`)
  expect(sfc).toMatch(`import { defineComponent } from 'vue'`)
  expect(sfc).toMatch(`import { useI18n } from 'vue-i18n'`)
  expect(sfc).toMatch(`export default defineComponent({`)
  expect(sfc).toMatch(`const { t } = useI18n({`)
  const pack = files['package.json']
  expect(pack).toMatch(`"vue-i18n": "^9.1.0"`)
  expect(pack).toMatch(`"@intlify/vue-i18n-loader": "^3.0.0"`)
})