Skip to content

Commit d3bddfc

Browse files
authored
feat: support virtual resoruce importing (#117)
1 parent 25866cb commit d3bddfc

File tree

3 files changed

+153
-5
lines changed

3 files changed

+153
-5
lines changed

packages/unplugin-vue-i18n/README.md

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm i @intlify/unplugin-vue-i18n
2828

2929
```ts
3030
// vite.config.ts
31-
import vueI18n from 'unplugin-vue-i18n/vite'
31+
import vueI18n from '@intlify/unplugin-vue-i18n/vite'
3232

3333
export default defineConfig({
3434
plugins: [
@@ -43,7 +43,7 @@ export default defineConfig({
4343
<summary>Webpack</summary><br>
4444

4545
```ts
46-
const VueI18nPlugin = require('unplugin-vue-i18n/webpack')
46+
const VueI18nPlugin = require('@intlify/unplugin-vue-i18n/webpack')
4747

4848
// webpack.config.js
4949
module.exports = {
@@ -56,6 +56,148 @@ module.exports = {
5656

5757
<br></details>
5858

59+
## 🚀 Usage
60+
61+
### i18n resources pre-compilation
62+
63+
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.
64+
65+
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.
66+
67+
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`.
68+
69+
70+
### i18n custom block
71+
72+
The below example that `examples/vite/src/App.vue` have `i18n` custom block:
73+
74+
```vue
75+
<template>
76+
<form>
77+
<label>{{ t('language') }}</label>
78+
<select v-model="locale">
79+
<option value="en">en</option>
80+
<option value="ja">ja</option>
81+
</select>
82+
</form>
83+
<p>{{ t('hello') }}</p>
84+
<Banana />
85+
</template>
86+
87+
<script>
88+
import { useI18n } from 'vue-i18n'
89+
import Banana from './Banana.vue'
90+
91+
export default {
92+
name: 'App',
93+
components: {
94+
Banana
95+
},
96+
setup() {
97+
const { t, locale } = useI18n({
98+
inheritLocale: true,
99+
useScope: 'local'
100+
})
101+
return { t, locale }
102+
}
103+
}
104+
</script>
105+
106+
<i18n>
107+
{
108+
"en": {
109+
"language": "Language",
110+
"hello": "hello, world!"
111+
},
112+
"ja": {
113+
"language": "言語",
114+
"hello": "こんにちは、世界!"
115+
}
116+
}
117+
</i18n>
118+
```
119+
120+
### Locale Messages formatting
121+
122+
You can be used by specifying the following format in the `lang` attribute:
123+
124+
- json (default)
125+
- yaml
126+
- yml
127+
- json5
128+
129+
example `yaml` format:
130+
131+
```vue
132+
<i18n lang="yaml">
133+
en:
134+
hello: 'Hello World!'
135+
ja:
136+
hello: 'こんにちは、世界!'
137+
</i18n>
138+
```
139+
140+
### Static bundle importing
141+
142+
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.
143+
144+
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.
145+
146+
```js
147+
import { createApp } from 'vue'
148+
import { createI18n } from 'vue-i18n'
149+
/*
150+
* The i18n resources in the path specified in the plugin `include` option can be read
151+
* as vue-i18n optimized locale messages using the import syntax
152+
*/
153+
import en from './src/locales/en.json'
154+
import ja from './src/locales/ja.yaml'
155+
import fr from './src/locales/fr.json5'
156+
157+
const i18n = createI18n({
158+
locale: 'en',
159+
messages: {
160+
en,
161+
ja,
162+
fr
163+
}
164+
})
165+
166+
const app = createApp()
167+
app.use(i18n).mount('#app')
168+
```
169+
170+
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:
171+
172+
```js
173+
import { createApp } from 'vue'
174+
import { createI18n } from 'vue-i18n'
175+
/*
176+
* All i18n resources specified in the plugin `include` option can be loaded
177+
* at once using the import syntax
178+
*/
179+
import messages from '@intlify/unplugin-vue-i18n/messages'
180+
181+
const i18n = createI18n({
182+
locale: 'en',
183+
messages
184+
})
185+
186+
const app = createApp()
187+
app.use(i18n).mount('#app')
188+
```
189+
190+
### Types
191+
192+
If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
193+
194+
```json
195+
{
196+
"compilerOptions": {
197+
"types": ["@intlify/unplugin-vue-i18n/messages"]
198+
}
199+
}
200+
```
59201

60202
## 🔧 Options
61203

@@ -73,7 +215,7 @@ module.exports = {
73215
- yml
74216
```
75217

76-
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.
218+
Note `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.
77219

78220
### `forceStringify`
79221

@@ -206,9 +348,7 @@ module.exports = {
206348

207349

208350
## ✅ TODO
209-
- [ ] Virtual resource importing
210351
- [ ] Bundling optimizations
211-
- [ ] i18n custom block birdge mode (for vite)
212352
- [ ] Support nuxt
213353

214354
## 📜 Changelog
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '@intlify/unplugin-vue-i18n/messages' {
2+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3+
const messages: any
4+
export default messages
5+
}

packages/unplugin-vue-i18n/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
"./types": {
8080
"types": "./types.d.ts"
8181
},
82+
"./messages": {
83+
"types": "./messages.d.ts"
84+
},
8285
"./lib/*": "./lib/*",
8386
"./package.json": "./package.json"
8487
},

0 commit comments

Comments
 (0)