Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

THIS REPOSITORY IS DEPRECATED (MOVE TO HERE)


Test

Vite plugin for Vue I18n

⭐ Features


i18n resources pre-compilation
i18n custom block
Static bundle importing
Bundling optimizations

💿 Installation


NPM


  1. ``` shell
  2. $ npm i --save-dev @intlify/vite-plugin-vue-i18n
  3. ```

YARN


  1. ``` shell
  2. $ yarn add -D @intlify/vite-plugin-vue-i18n
  3. ```

⚠️ Notice


When this plugin is installed, Vue I18n can only use the Composition API, and if you want to use the Legacy API, you need to set the compositionOnly option to false.

Also, if you do a production build with vite, Vue I18n will automatically bundle the runtime only module. If you need on-demand compilation with Message compiler, you need to set the runtimeOnly option to false.

🚀 Usage


i18n resources pre-compilation


Since vue-i18n@v9.0, the locale messages are handled with message compiler, which converts them to javascript functions after compiling. After compiling, message compiler converts them into javascript functions, which can improve the performance of the application.

However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, vue-i18n@v9.0 and later offer a full version that includes compiler and runtime, and a runtime only version.

If you are using the runtime version, you will need to compile before importing locale messages by managing them in a file such as .json.

Vite Config


the below example that examples/composition/vite.config.ts :

  1. ``` ts
  2. import path from 'path'
  3. import { defineConfig } from 'vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import vueI18n from '@intlify/vite-plugin-vue-i18n'

  6. export default defineConfig({
  7.   plugins: [
  8.     vue(), // you need to install `@vitejs/plugin-vue`
  9.     vueI18n({
  10.       // if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
  11.       // compositionOnly: false,

  12.       // you need to set i18n resource including paths !
  13.       include: path.resolve(__dirname, './path/to/src/locales/**')
  14.     })
  15.   ]
  16. })
  17. ```

i18n custom block


The below example that examples/composition/App.vue have i18n custom block:

  1. ``` vue
  2. <template>
  3.   <form>
  4.     <label>{{ t('language') }}</label>
  5.     <select v-model="locale">
  6.       <option value="en">en</option>
  7.       <option value="ja">ja</option>
  8.     </select>
  9.   </form>
  10.   <p>{{ t('hello') }}</p>
  11. </template>

  12. <script>
  13. import { useI18n } from 'vue-i18n'

  14. export default {
  15.   name: 'App',
  16.   setup() {
  17.     const { locale, t } = useI18n({
  18.       inheritLocale: true
  19.     })

  20.     return { locale, t }
  21.   }
  22. }
  23. </script>

  24. <i18n>
  25. {
  26.   "en": {
  27.     "language": "Language",
  28.     "hello": "hello, world!"
  29.   },
  30.   "ja": {
  31.     "language": "言語",
  32.     "hello": "こんにちは、世界!"
  33.   }
  34. }
  35. </i18n>
  36. ```

Locale Messages formatting


You can be used by specifying the following format in the lang attribute:

json (default)
yaml
yml
json5

example yaml format:

  1. ``` vue
  2. <i18n lang="yaml">
  3. en:
  4.   hello: 'Hello World!'
  5. ja:
  6.   hello: 'こんにちは、世界!'
  7. </i18n>
  8. ```

Static bundle importing


vite-plugin-vue-i18n allows you to statically bundle i18n resources such as json or yaml specified by the include option of the plugin described below as locale messages with the import syntax.

In this case, only one i18n resource can be statically bundled at a time with import syntax, so the these code will be redundant for multiple locales.

  1. ``` js
  2. import { createApp } from 'vue'
  3. import { createI18n } from 'vue-i18n'
  4. /*
  5. * The i18n resources in the path specified in the plugin `include` option can be read
  6. * as vue-i18n optimized locale messages using the import syntax
  7. */
  8. import en from './src/locales/en.json'
  9. import ja from './src/locales/ja.yaml'
  10. import fr from './src/locales/fr.json5'

  11. const i18n = createI18n({
  12.   locale: 'en',
  13.   messages: {
  14.     en,
  15.     ja,
  16.     fr
  17.   }
  18. })

  19. const app = createApp()
  20. app.use(i18n).mount('#app)
  21. ```

vite-plugin-vue-i18n can use the vite (rollup) mechanism to import all locales at once, using the special identifier @intlify/vite-plugin-vue-i18n/messages, as the bellow:

  1. ``` js
  2. import { createApp } from 'vue'
  3. import { createI18n } from 'vue-i18n'
  4. /*
  5. * All i18n resources specified in the plugin `include` option can be loaded
  6. * at once using the import syntax
  7. */
  8. import messages from '@intlify/vite-plugin-vue-i18n/messages'

  9. const i18n = createI18n({
  10.   locale: 'en',
  11.   messages
  12. })

  13. const app = createApp()
  14. app.use(i18n).mount('#app)
  15. ```

Client Types


If you want type definition of @intlify/vite-plugin-vue-i18n/messages, add vite-plugin-vue-i18n/client to compilerOptions.types of your tsconfig:

  1. ``` json
  2. {
  3.   "compilerOptions": {
  4.     "types": ["@intlify/vite-plugin-vue-i18n/client"]
  5.   }
  6. }
  7. ```

Bundle optimizations


vite-plugin-vue-i18n allows you to support bundle size optimization provided by vue-i18n.

📦 Automatic Vue I18n bundling


As noted here, NPM provides many different builds of Vue I18n.

vite-plugin-vue-i18n will automatically select and bundle Vue I18n build according to the following vite behavior:

vite dev: vue-i18n.esm-bundler.js
vite build: vue-i18n.runtime.esm-bundler.js

About details, See the here

🔧 Options


You can specify options in the plugin option to support bundle size optimization provided by vue-i18n.

The same thing can be configured with the define option, but the plugin option is more friendly. Especially if you are using typescript, you can use intelisense.

About details, See the below section

include


Type:string | string[] | undefined

Default:undefined

A minimatch pattern, or array of patterns, you can specify a path to pre-compile i18n resources files. The extensions of i18n resources to be precompiled are as follows:

  1. ``` sh
  2. - json
  3. - json5
  4. - yaml
  5. - yml

  6. ```

Note json resources matches this option, it will be handled before the internal json plugin of Vite, and will not be processed afterwards, else the option doesn't match, the Vite side will handle.

runtimeOnly


Type:boolean

Default:true

Whether or not to automatically use Vue I18n runtime-onlyin production build, set in the vue-i18n field of Vite resolve.alias option. If false is specified, Vue I18n (vue-i18n) package.json module field will be used.

For more details, See here

compositionOnly


Type:boolean

Default:true

Whether to make vue-i18n's API only composition API. By default the legacy API is tree-shaken.

For more details, See here

fullInstall


Type:boolean

Default:true

Whether to install the full set of APIs, components, etc. provided by Vue I18n. By default, all of them will be installed.

If false is specified, buld-in components and directive will not be installed in vue and will be tree-shaken.

For more details, See here

forceStringify


Type:boolean

Default:false

Whether pre-compile number and boolean values as message functions that return the string value.

For example, the following json resources:

  1. ``` json
  2. {
  3.   "trueValue": true,
  4.   "falseValue": false,
  5.   "nullValue": null,
  6.   "numberValue": 1
  7. }
  8. ```

after pre-compiled (development):

  1. ``` js
  2. export default {
  3.   trueValue: (() => {
  4.     const fn = ctx => {
  5.       const { normalize: _normalize } = ctx
  6.       return _normalize(['true'])
  7.     }
  8.     fn.source = 'true'
  9.     return fn
  10.   })(),
  11.   falseValue: (() => {
  12.     const fn = ctx => {
  13.       const { normalize: _normalize } = ctx
  14.       return _normalize(['false'])
  15.     }
  16.     fn.source = 'false'
  17.     return fn
  18.   })(),
  19.   nullValue: (() => {
  20.     const fn = ctx => {
  21.       const { normalize: _normalize } = ctx
  22.       return _normalize(['null'])
  23.     }
  24.     fn.source = 'null'
  25.     return fn
  26.   })(),
  27.   numberValue: (() => {
  28.     const fn = ctx => {
  29.       const { normalize: _normalize } = ctx
  30.       return _normalize(['1'])
  31.     }
  32.     fn.source = '1'
  33.     return fn
  34.   })()
  35. }
  36. ```

defaultSFCLang


Type:string

Default:undefined

Specify the content for all your inlined i18n custom blocks on your SFC.

defaultSFCLang must have one of the following values:

  1. ``` sh
  2. - json
  3. - json5
  4. - yaml
  5. - yml

  6. ```

On inlined i18n custom blocks that have specified the lang attribute, the defaultSFCLang is not applied.

For example, with defaultSFCLang: "yaml" or defaultSFCLang: "yml", this custom block:

  1. ``` html
  2. <i18n lang="yaml">
  3. en:
  4.   hello: Hello
  5. es:
  6.   hello: Hola
  7. </i18n>
  8. ```

and this another one, are equivalent:

  1. ``` html
  2. <i18n>
  3. en:
  4.   hello: Hello
  5. es:
  6.   hello: Hola
  7. </i18n>
  8. ```

globalSFCScope


Type:boolean

Default:undefined

Whether to include all i18n custom blocks on your SFC on global scope.

If true, it will be applied to all inlined i18n or imported custom blocks.

Warning: beware enabling globalSFCScope: true, all i18n custom blocks in all your SFC will be on global scope.

For example, with globalSFCScope: true, this custom block:

  1. ``` html
  2. <i18n lang="yaml" global>
  3. en:
  4.   hello: Hello
  5. es:
  6.   hello: Hola
  7. </i18n>
  8. ```

and this another one, are equivalent:

  1. ``` html
  2. <i18n lang="yaml">
  3. en:
  4.   hello: Hello
  5. es:
  6.   hello: Hola
  7. </i18n>
  8. ```

You can also use defaultSFCLang: "yaml", following with previous example, this another is also equivalent to previous ones:

  1. ``` html
  2. <i18n>
  3. en:
  4.   hello: Hello
  5. es:
  6.   hello: Hola
  7. </i18n>
  8. ```

📜 Changelog


Details changes for each release are documented in the CHANGELOG.md.

❗ Issues


Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

©️ License


MIT
Last Updated: 2023-05-23 11:11:51