版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. # @intlify/unplugin-vue-i18n
  2. [![Lint](https://github.com/intlify/bundle-tools/actions/workflows/lint.yml/badge.svg)](https://github.com/intlify/bundle-tools/actions/workflows/lint.yml)
  3. [![Test](https://github.com/intlify/bundle-tools/actions/workflows/test.yml/badge.svg)](https://github.com/intlify/bundle-tools/actions/workflows/test.yml)
  4. [![npm](https://img.shields.io/npm/v/@intlify/unplugin-vue-i18n.svg?color=yellow)](https://www.npmjs.com/package/@intlify/unplugin-vue-i18n)
  5. unplugin for Vue I18n
  6. ## 🌟 Features
  7. - i18n resource pre-compilation
  8. - i18n custom block
  9. - i18n resource definition
  10. - i18n resource importing
  11. - Locale of i18n resource definition
  12. - Locale of i18n resource definition for global scope
  13. - i18n resource formatting
  14. ## 💿 Installation
  15. ```sh
  16. npm i @intlify/unplugin-vue-i18n
  17. ```
  18. <details>
  19. <summary>Vite</summary><br>
  20. ```ts
  21. // vite.config.ts
  22. import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
  23. export default defineConfig({
  24. plugins: [
  25. VueI18nPlugin({ /* options */ }),
  26. ],
  27. })
  28. ```
  29. <br></details>
  30. <details>
  31. <summary>Webpack</summary><br>
  32. ```ts
  33. const VueI18nPlugin = require('@intlify/unplugin-vue-i18n/webpack')
  34. // webpack.config.js
  35. module.exports = {
  36. /* ... */
  37. plugins: [
  38. VueI18nPlugin({ /* options */ })
  39. ]
  40. }
  41. ```
  42. <br></details>
  43. <details>
  44. <summary>Nuxt</summary><br>
  45. ```ts
  46. // nuxt.config.ts
  47. import { defineNuxtConfig } from 'nuxt'
  48. import VueI18nPlugin from '@intlify/unplugin-vue-i18n'
  49. export default defineNuxtConfig({
  50. vite: {
  51. plugins: [
  52. VueI18nPlugin.vite({ /* options */ }),
  53. ],
  54. },
  55. // When using Webpack
  56. // builder: '@nuxt/webpack-builder',
  57. webpack: {
  58. plugins: [
  59. VueI18nPlugin.webpack({ /* options */ }),
  60. ]
  61. }
  62. })
  63. ```
  64. <br></details>
  65. ## 🚀 Usage
  66. ### i18n resources pre-compilation
  67. Since `vue-i18n@v9.x`, 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.
  68. However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, `vue-i18n@v9.x` and later offer a full version that includes compiler and runtime, and a runtime only version.
  69. 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`.
  70. ### i18n custom block
  71. The below example that `examples/vite/src/App.vue` have `i18n` custom block:
  72. ```vue
  73. <template>
  74. <form>
  75. <label>{{ t('language') }}</label>
  76. <select v-model="locale">
  77. <option value="en">en</option>
  78. <option value="ja">ja</option>
  79. </select>
  80. </form>
  81. <p>{{ t('hello') }}</p>
  82. <Banana />
  83. </template>
  84. <script>
  85. import { useI18n } from 'vue-i18n'
  86. import Banana from './Banana.vue'
  87. export default {
  88. name: 'App',
  89. components: {
  90. Banana
  91. },
  92. setup() {
  93. const { t, locale } = useI18n({
  94. inheritLocale: true,
  95. useScope: 'local'
  96. })
  97. return { t, locale }
  98. }
  99. }
  100. </script>
  101. <i18n>
  102. {
  103. "en": {
  104. "language": "Language",
  105. "hello": "hello, world!"
  106. },
  107. "ja": {
  108. "language": "言語",
  109. "hello": "こんにちは、世界!"
  110. }
  111. }
  112. </i18n>
  113. ```
  114. ### Locale Messages formatting
  115. You can be used by specifying the following format in the `lang` attribute:
  116. - json (default)
  117. - yaml
  118. - yml
  119. - json5
  120. example `yaml` format:
  121. ```vue
  122. <i18n lang="yaml">
  123. en:
  124. hello: 'Hello World!'
  125. ja:
  126. hello: 'こんにちは、世界!'
  127. </i18n>
  128. ```
  129. ### Static bundle importing
  130. unplugin-vue-i18n allows you to statically bundle i18n resources such as `json` or `yaml` specified by the [`include` option](#include) of the plugin described below as locale messages with the `import` syntax.
  131. 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.
  132. ```js
  133. import { createApp } from 'vue'
  134. import { createI18n } from 'vue-i18n'
  135. /*
  136. * The i18n resources in the path specified in the plugin `include` option can be read
  137. * as vue-i18n optimized locale messages using the import syntax
  138. */
  139. import en from './src/locales/en.json'
  140. import ja from './src/locales/ja.yaml'
  141. import fr from './src/locales/fr.json5'
  142. const i18n = createI18n({
  143. locale: 'en',
  144. messages: {
  145. en,
  146. ja,
  147. fr
  148. }
  149. })
  150. const app = createApp()
  151. app.use(i18n).mount('#app')
  152. ```
  153. unplugin-vue-i18n can use the bundler virtual mechanism to import all locales at once, using the special identifier `@intlify/unplugin-vue-i18n/messages`, as the bellow:
  154. ```js
  155. import { createApp } from 'vue'
  156. import { createI18n } from 'vue-i18n'
  157. /*
  158. * All i18n resources specified in the plugin `include` option can be loaded
  159. * at once using the import syntax
  160. */
  161. import messages from '@intlify/unplugin-vue-i18n/messages'
  162. const i18n = createI18n({
  163. locale: 'en',
  164. messages
  165. })
  166. const app = createApp()
  167. app.use(i18n).mount('#app')
  168. ```
  169. Change your vite.config.ts file accordingly to import all the files from locales folder on the root. Change `'./locales/**'` to path of your locales.
  170. ```ts
  171. // vite.config.ts
  172. import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
  173. import path from 'path'
  174. export default defineConfig({
  175. plugins: [
  176. VueI18nPlugin({
  177. include: [path.resolve(__dirname, './locales/**')],
  178. }),
  179. ],
  180. })
  181. ```
  182. ### Types
  183. If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
  184. ```json
  185. {
  186. "compilerOptions": {
  187. "types": ["@intlify/unplugin-vue-i18n/messages"]
  188. }
  189. }
  190. ```
  191. ## 📦 Automatic bundling
  192. ### For Vue I18n
  193. As noted [here](https://vue-i18n.intlify.dev/guide/installation.html#explanation-of-different-builds), NPM provides many different builds of Vue I18n.
  194. This plugin will automatically select and bundle Vue I18n build according to the following behavior:
  195. - development: `vue-i18n.esm-bundler.js`
  196. - production: `vue-i18n.runtime.esm-bundler.js`
  197. About details, See the [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only)
  198. ### For `petite-vue-i18n`
  199. This plugin will automatically select and bundle `petite-vue-i18n` build according to the following vite behavior:
  200. - vite dev: `petite-vue-i18n.esm-bundler.js`
  201. - vite build: `petite-vue-i18n.runtime.esm-bundler.js`
  202. ## 🔧 Options
  203. ### `include`
  204. - **Type:** `string | string[] | undefined`
  205. - **Default:** `undefined`
  206. A [picomatch](https://github.com/micromatch/picomatch) 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:
  207. ```
  208. - json
  209. - json5
  210. - yaml
  211. - yml
  212. - js
  213. - ts
  214. ```
  215. If nothing is specified for this option, i.e. `undefined`, nothing is done to the resource in the above format.
  216. > ⚠️ NOTE:
  217. `json` resources matches this option, it will be handled **before the internal json plugin of bundler, and will not be processed afterwards**, else the option doesn't match, the bundler side will handle.
  218. > ⚠️ NOTE:
  219. `yaml` resources don't support multi documentation with `|`, alias with `&` and `*`, tags with `! `, `@`, etc. Only simple data structures.
  220. > ⚠️ NOTE:
  221. `js` and `ts` resources are set **simple export (`export default`) as locale messages object, as default**.
  222. ```js
  223. export default {
  224. hello: 'Hello, {name}!',
  225. // ...
  226. }
  227. ```
  228. If you need to use programmatically dynamic resource construction, you would be enable `allowDynamic` option. about details, see the section.
  229. > ⚠️ NOTE:
  230. If you use the `js` and `ts` resources formats, set the paths, so your application code is not targeted. We recommend that resources be isolated from the application code.
  231. ### `strictMessage`
  232. - **Type:** `boolean`
  233. - **Default:** `true`
  234. Strictly checks that the locale message does not contain html tags.
  235. If html tags are included, an error is thrown.
  236. If you would not the error to be thrown, you can work around it by setting it to `false`, but **it means that the locale message might cause security problems with XSS**.
  237. In that case, we recommend setting the `escapeHtml` option to `true`.
  238. ### `escapeHtml`
  239. - **Type:** `boolean`
  240. - **Default:** `false`
  241. Whether to escape html tags if they are included in the locale message.
  242. If `strictMessage` is disabled by `false`, we recommend this option be enabled.
  243. ### `allowDynamic`
  244. - **Type:** `boolean`
  245. - **Default:** `false`
  246. Whether or not programmatically dynamic resource construction for `js` or `ts` resource format.
  247. In this case, you need to export the function with `export default` and construct the resource with the function:
  248. ```js
  249. import resources from './locales/all.json'
  250. export default async function loadResource(url) {
  251. const res = await import(url).then(r => r.default || r)
  252. return { ...resources, ...res }
  253. }
  254. ```
  255. If you fetch some resources from the backend, the data **must be pre-compiled** for production. exmaple is [here](https://github.com/intlify/vue-i18n-next/tree/master/examples/backend).
  256. ### `runtimeOnly`
  257. - **Type:** `boolean`
  258. - **Default:** `true`
  259. Whether or not to automatically use Vue I18n **runtime-only** in production build, set `vue-i18n.runtime.esm-bundler.js` in the `vue-i18n` field of bundler config, the below:
  260. ```
  261. - vite config: `resolve.alias`
  262. - webpack config: `resolve.alias`
  263. ```
  264. If `false` is specified, Vue I18n (vue-i18n) package.json `module` field will be used.
  265. For more details, See [here](#-automatic-bundling)
  266. ### `compositionOnly`
  267. - **Type:** `boolean`
  268. - **Default:** `true`
  269. Whether to make vue-i18n API only composition API. **By default the legacy API is tree-shaken.**
  270. For more details, See [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags)
  271. ### `fullInstall`
  272. - **Type:** `boolean`
  273. - **Default:** `true`
  274. Whether to install the full set of APIs, components, etc. provided by Vue I18n. By default, all of them will be installed.
  275. If `false` is specified, **buld-in components and directive will not be installed in vue and will be tree-shaken.**
  276. For more details, See [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#reduce-bundle-size-with-feature-build-flags)
  277. ### `forceStringify`
  278. - **Type:** `boolean`
  279. - **Default:** `false`
  280. Whether pre-compile number and boolean values as message functions that return the string value.
  281. For example, the following json resources:
  282. ```json
  283. {
  284. "trueValue": true,
  285. "falseValue": false,
  286. "nullValue": null,
  287. "numberValue": 1
  288. }
  289. ```
  290. after pre-compiled (development):
  291. ```js
  292. export default {
  293. "trueValue": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize(["true"])};fn.source="true";return fn;})(),
  294. "falseValue": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize(["false"])};fn.source="false";return fn;})(),
  295. "nullValue": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize(["null"])};fn.source="null";return fn;})(),
  296. "numberValue": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize(["1"])};fn.source="1";return fn;})()
  297. }
  298. ```
  299. ### `defaultSFCLang`
  300. - **Type:** `string`
  301. - **Default:** `'json'`
  302. Specify the content for all your inlined `i18n` custom blocks on your `SFC`.
  303. `defaultSFCLang` must have one of the following values:
  304. ```
  305. - json
  306. - json5
  307. - yaml
  308. - yml
  309. ```
  310. On inlined `i18n` custom blocks that have specified the `lang` attribute, the `defaultSFCLang` is not applied.
  311. For example, with `defaultSFCLang: "yaml"` or `defaultSFCLang: "yml"`, this custom block:
  312. ```html
  313. <i18n lang="yaml">
  314. en:
  315. hello: Hello
  316. es:
  317. hello: Hola
  318. </i18n>
  319. ```
  320. and this another one, are equivalent:
  321. ```html
  322. <i18n>
  323. en:
  324. hello: Hello
  325. es:
  326. hello: Hola
  327. </i18n>
  328. ```
  329. ### `globalSFCScope`
  330. - **Type:** `boolean`
  331. - **Default:** `undefined`
  332. Whether to include all `i18n` custom blocks on your `SFC` on `global` scope.
  333. If `true`, it will be applied to all inlined `i18n` or `imported` custom blocks.
  334. **Warning**: beware enabling `globalSFCScope: true`, all `i18n` custom blocks in all your `SFC` will be on `global` scope.
  335. For example, with `globalSFCScope: true`, this custom block:
  336. ```html
  337. <i18n lang="yaml" global>
  338. en:
  339. hello: Hello
  340. es:
  341. hello: Hola
  342. </i18n>
  343. ```
  344. and this another one, are equivalent:
  345. ```html
  346. <i18n lang="yaml">
  347. en:
  348. hello: Hello
  349. es:
  350. hello: Hola
  351. </i18n>
  352. ```
  353. You can also use `defaultSFCLang: "yaml"`, following with previous example, this another is also equivalent to previous ones:
  354. ```html
  355. <i18n>
  356. en:
  357. hello: Hello
  358. es:
  359. hello: Hola
  360. </i18n>
  361. ```
  362. ### `bridge`
  363. - **Type:** `boolean`
  364. - **Default:** `false`
  365. The mode to birdge the i18n custom block to work in both vue-i18n@v8.x and vue-i18n@v9.x environments.
  366. To support in a smooth transition from vue-i18n@v8.x to vue-i18n@v9.x, we provide a mode that bundles the i18n custom block to be available in either version.
  367. > ⚠️ Note that if you set `bridge: true`, the bundle size will increase. It is recommended to disable this mode after the migration from vue-i18n@v8.26 to vue-i18n@v9.x is completed.
  368. ### `esm`
  369. - **Type:** `boolean`
  370. - **Default:** `true`
  371. For `bridge` option is `true`, whether to bundle locale resources with ESM. By default ESM, if you need to bundl with commonjs for especialy webpack, you need to set `false`
  372. ### `useClassComponent`
  373. - **Type:** `boolean`
  374. - **Default:** `false`
  375. This option that to use i18n custom blocks in `vue-class-component`.
  376. ### `useVueI18nImportName` (Experimental)
  377. - **Type:** `boolean`
  378. - **Default:** `false`
  379. Whether to use the import name of `petite-vue-i18n` with the same import name as vue-i18n (`import { xxx } from 'vue-i18n'`).
  380. This option allows a smooth migration from `petite-vue-i18n` to `vue-i18n` and allows progressive enhacement.
  381. ## 📜 Changelog
  382. Details changes for each release are documented in the [CHANGELOG.md](https://github.com/intlify/bundle-tools/blob/main/packages/unplugin-vue-i18n/CHANGELOG.md)
  383. ## ©️ License
  384. [MIT](http://opensource.org/licenses/MIT)