Installations
npm install @intlify/unplugin-vue-i18n
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>= 18
Node Version
18.19.1
NPM Version
9.5.1
Releases
@intlify/unplugin-vue-i18n@6.0.3
Published on 30 Dec 2024
@intlify/unplugin-vue-i18n@6.0.2
Published on 21 Dec 2024
@intlify/unplugin-vue-i18n@6.0.1
Published on 06 Dec 2024
@intlify/unplugin-vue-i18n@6.0.0
Published on 16 Nov 2024
@intlify/bundle-utils@10.0.0
Published on 16 Nov 2024
@intlify/unplugin-vue-i18n@5.3.1
Published on 10 Nov 2024
Contributors
Unable to fetch Contributors
Languages
TypeScript (79.29%)
JavaScript (17.1%)
Vue (2.84%)
HTML (0.76%)
Shell (0.01%)
Developer
intlify
Download Statistics
Total Downloads
20,029,766
Last Day
62,978
Last Week
280,569
Last Month
1,234,498
Last Year
13,792,817
GitHub Statistics
251 Stars
433 Commits
39 Forks
9 Watching
12 Branches
30 Contributors
Package Meta Information
Latest Version
6.0.3
Package Id
@intlify/unplugin-vue-i18n@6.0.3
Unpacked Size
93.91 kB
Size
24.12 kB
File Count
26
NPM Version
9.5.1
Node Version
18.19.1
Publised On
30 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
20,029,766
Last day
-5.6%
62,978
Compared to previous day
Last week
-16.4%
280,569
Compared to previous week
Last month
4.8%
1,234,498
Compared to previous month
Last year
135.2%
13,792,817
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
16
Peer Dependencies
3
Dev Dependencies
2
@intlify/unplugin-vue-i18n
unplugin for Vue I18n
🌟 Features
- i18n resource pre-compilation
- i18n custom block
- i18n resource definition
- i18n resource importing
- Locale of i18n resource definition
- Locale of i18n resource definition for global scope
- i18n resource formatting
💿 Installation
1npm i @intlify/unplugin-vue-i18n
Vite
1// vite.config.ts 2import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' 3 4export default defineConfig({ 5 plugins: [ 6 VueI18nPlugin({ 7 /* options */ 8 }) 9 ] 10})
Webpack
1const VueI18nPlugin = require('@intlify/unplugin-vue-i18n/webpack') 2 3// webpack.config.js 4module.exports = { 5 /* ... */ 6 plugins: [ 7 VueI18nPlugin({ 8 /* options */ 9 }) 10 ] 11}
Nuxt
1// nuxt.config.ts 2import { defineNuxtConfig } from 'nuxt' 3import VueI18nPlugin from '@intlify/unplugin-vue-i18n' 4 5export default defineNuxtConfig({ 6 vite: { 7 plugins: [ 8 VueI18nPlugin.vite({ 9 /* options */ 10 }) 11 ] 12 }, 13 // When using Webpack 14 // builder: '@nuxt/webpack-builder', 15 webpack: { 16 plugins: [ 17 VueI18nPlugin.webpack({ 18 /* options */ 19 }) 20 ] 21 } 22})
🚀 Usage
locale messages pre-compilation
Since vue-i18n@v9.x
, the locale messages are handled with message compiler, which transform them to javascript functions or AST objects after compiling, so these can improve the performance of the application.
If you want to maximize the performance of vue-i18n, we recommend using unplugin-vue-i18n for locale messages.
i18n custom block
The below example that examples/vite/src/App.vue
have i18n
custom block:
1<template> 2 <form> 3 <label>{{ t('language') }}</label> 4 <select v-model="locale"> 5 <option value="en">en</option> 6 <option value="ja">ja</option> 7 </select> 8 </form> 9 <p>{{ t('hello') }}</p> 10 <Banana /> 11</template> 12 13<script> 14import { useI18n } from 'vue-i18n' 15import Banana from './Banana.vue' 16 17export default { 18 name: 'App', 19 components: { 20 Banana 21 }, 22 setup() { 23 const { t, locale } = useI18n({ 24 inheritLocale: true, 25 useScope: 'local' 26 }) 27 return { t, locale } 28 } 29} 30</script> 31 32<i18n> 33{ 34 "en": { 35 "language": "Language", 36 "hello": "hello, world!" 37 }, 38 "ja": { 39 "language": "言語", 40 "hello": "こんにちは、世界!" 41 } 42} 43</i18n>
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<i18n lang="yaml"> 2en: 3 hello: 'Hello World!' 4ja: 5 hello: 'こんにちは、世界!' 6</i18n>
Static bundle importing
unplugin-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.
1import { createApp } from 'vue' 2import { createI18n } from 'vue-i18n' 3/* 4 * The i18n resources in the path specified in the plugin `include` option can be read 5 * as vue-i18n optimized locale messages using the import syntax 6 */ 7import en from './src/locales/en.json' 8import ja from './src/locales/ja.yaml' 9import fr from './src/locales/fr.json5' 10 11const i18n = createI18n({ 12 locale: 'en', 13 messages: { 14 en, 15 ja, 16 fr 17 } 18}) 19 20const app = createApp() 21app.use(i18n).mount('#app')
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:
1import { createApp } from 'vue' 2import { createI18n } from 'vue-i18n' 3/* 4 * All i18n resources specified in the plugin `include` option can be loaded 5 * at once using the import syntax 6 */ 7import messages from '@intlify/unplugin-vue-i18n/messages' 8 9const i18n = createI18n({ 10 locale: 'en', 11 messages 12}) 13 14const app = createApp() 15app.use(i18n).mount('#app')
Change your vite.config.ts file accordingly to import all the files from locales folder on the root. Change './src/locales/**'
to path of your locales.
1// vite.config.ts 2import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' 3import path from 'path' 4 5export default defineConfig({ 6 plugins: [ 7 VueI18nPlugin({ 8 include: [path.resolve(__dirname, './src/locales/**')] 9 }) 10 ] 11})
unplugin-vue-i18n will automatically merge locale files into @intlify/unplugin-vue-i18n/messages
. This allows locales to be split across multiple files, for example src/locales/fruits/en.json
and src/locales/vegetables/en.json
.
Types
If you want type definition of @intlify/unplugin-vue-i18n/messages
, add unplugin-vue-i18n/messages
to compilerOptions.types
of your tsconfig:
1{ 2 "compilerOptions": { 3 "types": ["@intlify/unplugin-vue-i18n/messages"] 4 } 5}
📦 Automatic bundling
For Vue I18n
As noted here, NPM provides many different builds of Vue I18n.
This plugin will automatically select and bundle Vue I18n build according to the following behavior:
- development:
vue-i18n.esm-bundler.js
- production:
vue-i18n.runtime.esm-bundler.js
About details, See the here
🔧 Options
include
-
Type:
string | string[] | undefined
-
Default:
undefined
A 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:
- json - json5 - yaml - yml - js - ts
If nothing is specified for this option, i.e.
undefined
, nothing is done to the resource in the above format.
[!WARNING] >
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.
[!WARNING] >
yaml
resources don't support multi documentation with|
, alias with&
and*
, tags with!
,@
, etc. Only simple data structures.
[!WARNING] >
js
andts
resources are set simple export (export default
) as locale messages object, as default.
1export default { 2 hello: 'Hello, {name}!' 3 // ... 4}
If you need to use programmatically dynamic resource construction, you would be enable allowDynamic
option. about details, see the section.
[!WARNING] If you use the
js
andts
resources formats, set the paths, so your application code is not targeted. We recommend that resources be isolated from the application code.
module
-
Type:
string
-
Default:
'vue-i18n'
[!NOTE] This options is supported from v5.1.0, and works with vue-i18n v10 and later.
Bundle target vue-i18n module. You can specify either
‘vue-i18n’
or‘petite-vue-i18n’
.The default is
'vue-i18n'
, and the following installed in node_modules will be bundled.- development:
vue-i18n.esm-bundler.js
- production:
vue-i18n.runtime.esm-bundler.js
In the case of
‘petite-vue-i18n’
, the following installed in node_modules will be bundled.- development:
petite-vue-i18n.esm-bundler.js
- production:
petite-vue-i18n.runtime.esm-bundler.js
If you are using petite-vue-i18n, you will need to set this value.
- development:
strictMessage
-
Type:
boolean
-
Default:
true
Strictly checks that the locale message does not contain html tags.
If html tags are included, an error is thrown.
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.In that case, we recommend setting the
escapeHtml
option totrue
.
escapeHtml
-
Type:
boolean
-
Default:
false
Whether to escape html tags if they are included in the locale message.
If
strictMessage
is disabled byfalse
, we recommend this option be enabled.
allowDynamic
-
Type:
boolean
-
Default:
false
Whether or not programmatically dynamic resource construction for
js
orts
resource format.In this case, you need to export the function with
export default
and construct the resource with the function:1import resources from './locales/all.json' 2 3export default async function loadResource(url) { 4 const res = await import(url).then(r => r.default || r) 5 return { ...resources, ...res } 6}
If you fetch some resources from the backend, the data must be pre-compiled for production. exmaple is here.
jitCompilation
- Type:
boolean
- Default:
true
[!IMPORTANT] 'jitCompilation' option is deprected in v5. This option will be supported with vue-i18n until v9 latest version.
Whether locale mesages should be compiled by JIT (Just in Time) compilation with vue-i18n's message compiler.
JIT compilation has been supported since vue-i18n v9.3. This means that since v9 was released until now, the message compiler compiles to executable JavaScript code, however it did not work in the CSP environment. Also, since this was an AOT (Ahead of Time) compilation, it was not possible to dynamically retrieve locale messages from the back-end Database and compose locale mesages with programatic.
[!WARNING] Enabling JIT compilation causes the message compiler to generate AST objects for locale mesages instead of JavaScript code. If you pre-compile locale messages with a tool such as the Intlify CLI and import them dynamically, you need to rebuild that resource.
About JIT compilation, See here
dropMessageCompiler
- Type:
boolean
- Default:
false
Whether to tree-shake message compiler when we will be bundling.
If do you will use this option, you need to enable jitCompilation
option.
[!NOTE] After v5 or later, this option can be set with or without
jitCompilation
.
[!NOTE] This option works with vue-i18n v9.3 and later.
[!WARNING] If you enable this option, you should check resources in your application are pre-compiled with this plugin. If you will be loading resources dynamically from the back-end via the API, enabling this option do not work because there is not message compiler.
ssr
-
Type:
boolean
-
Default:
false
Whether to bundle vue-i18n module for SSR at build time
[!NOTE] This option works with vue-i18n v9.4 and later.
runtimeOnly
-
Type:
boolean
-
Default:
true
Whether or not to automatically use Vue I18n runtime-only in production build, set
vue-i18n.runtime.esm-bundler.js
in thevue-i18n
field of bundler config, the below:- vite config: `resolve.alias` - webpack config: `resolve.alias`
If
false
is specified, Vue I18n (vue-i18n) package.jsonmodule
field will be used.For more details, See here
compositionOnly
-
Type:
boolean
-
Default:
true
Whether to make vue-i18n 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{ 2 "trueValue": true, 3 "falseValue": false, 4 "nullValue": null, 5 "numberValue": 1 6}
after pre-compiled (development):
1export default { 2 trueValue: (() => { 3 const fn = ctx => { 4 const { normalize: _normalize } = ctx 5 return _normalize(['true']) 6 } 7 fn.source = 'true' 8 return fn 9 })(), 10 falseValue: (() => { 11 const fn = ctx => { 12 const { normalize: _normalize } = ctx 13 return _normalize(['false']) 14 } 15 fn.source = 'false' 16 return fn 17 })(), 18 nullValue: (() => { 19 const fn = ctx => { 20 const { normalize: _normalize } = ctx 21 return _normalize(['null']) 22 } 23 fn.source = 'null' 24 return fn 25 })(), 26 numberValue: (() => { 27 const fn = ctx => { 28 const { normalize: _normalize } = ctx 29 return _normalize(['1']) 30 } 31 fn.source = '1' 32 return fn 33 })() 34}
defaultSFCLang
-
Type:
string
-
Default:
'json'
Specify the content for all your inlined
i18n
custom blocks on yourSFC
.defaultSFCLang
must have one of the following values:- json - json5 - yaml - yml
On inlined
i18n
custom blocks that have specified thelang
attribute, thedefaultSFCLang
is not applied.For example, with
defaultSFCLang: "yaml"
ordefaultSFCLang: "yml"
, this custom block:1<i18n lang="yaml"> 2en: 3 hello: Hello 4es: 5 hello: Hola 6</i18n>
and this another one, are equivalent:
1<i18n> 2en: 3 hello: Hello 4es: 5 hello: Hola 6</i18n>
globalSFCScope
-
Type:
boolean
-
Default:
undefined
Whether to include all
i18n
custom blocks on yourSFC
onglobal
scope.If
true
, it will be applied to all inlinedi18n
orimported
custom blocks.Warning: beware enabling
globalSFCScope: true
, alli18n
custom blocks in all yourSFC
will be onglobal
scope.For example, with
globalSFCScope: true
, this custom block:1<i18n lang="yaml" global> 2en: 3 hello: Hello 4es: 5 hello: Hola 6</i18n>
and this another one, are equivalent:
1<i18n lang="yaml"> 2en: 3 hello: Hello 4es: 5 hello: Hola 6</i18n>
You can also use
defaultSFCLang: "yaml"
, following with previous example, this another is also equivalent to previous ones:1 <i18n> 2en: 3 hello: Hello 4es: 5 hello: Hola 6</i18n>
useClassComponent
-
Type:
boolean
-
Default:
false
This option that to use i18n custom blocks in
vue-class-component
.[!IMPORTANT] >
useClassComponent
option is deprecated in v5. This option will be supported with vue-i18n until v9 latest version.
onlyLocales
-
Type:
string | string[]
-
Default:
[]
By using it you can exclude from the bundle those localizations that are not specified in this option.
useVueI18nImportName
(Experimental)
-
Type:
boolean
-
Default:
false
Whether to use the import name of
petite-vue-i18n
with the same import name as vue-i18n (import { xxx } from 'vue-i18n'
).This option allows a smooth migration from
petite-vue-i18n
tovue-i18n
and allows progressive enhacement.[!IMPORTANT] >
useVueI18nImportName
option is deprecated in v5. This option will be supported with vue-i18n until v9 latest version.
optimizeTranslationDirective
-
Type:
boolean
|string
|string[]
-
Default:
false
Whether to optimize
v-t
directive. If set totrue
, this plugin's transform will automatically translate to vue-i18n's translation function. If you need SSR, you must activate this option.If you want to put it manually, you can specify the signature of the translation function as a string or a string array.
[!WARNING] About for manually signature, see the details vue-i18n-extensions API docs and usecase from vue-i18n-extensions PR
transformI18nBlock
-
Type:
function
-
Default:
undefined
This hook allows a user to modify the
<i18n>
block before the plugin generates the translations. The hook is passed the source of the<ii8n>
block as astring
after the SFC is read from disk.Plugin
1function transformI18nBlock(source) { 2 // transformation logic 3} 4 5// Plugin 6vueI18n({ 7 transformI18nBlock 8})
Before
1<i18n> 2[ 3 'slug-one', 4 'slug-two' 5] 6</i18n>
After
1<i18n> 2{ 3 'en': { 4 'slug-one': 'foo', 5 'slug-two': 'bar' 6 }, 7 ja: { 8 'slug-one': 'foo', 9 'slug-two': 'bar' 10 } 11} 12</i18n>
[!IMPORTANT] The function must return a string or the build will fail.
📜 Changelog
Details changes for each release are documented in the CHANGELOG.md
©️ License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
24 commit(s) and 9 issue activity found in the last 90 days -- score normalized to 10
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/build.yml:14
- Info: topLevel 'contents' permission set to 'read': .github/workflows/lint.yml:14
- Info: topLevel 'contents' permission set to 'read': .github/workflows/test.yml:14
- Info: no jobLevel write permissions found
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 8/29 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/build.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/build.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/lint.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/lint.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/test.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/intlify/bundle-tools/test.yml/main?enable=pin
- Info: 0 out of 7 GitHub-owned GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-7q7g-4xm8-89cq
- Warn: Project is vulnerable to: GHSA-9r9m-ffp6-9x4v
- Warn: Project is vulnerable to: GHSA-hjwq-mjwj-4x6c
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-vg6x-rcgg-rjx6
- Warn: Project is vulnerable to: GHSA-5j4c-8p2g-v4jx
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
Score
5
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More