You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+108-3Lines changed: 108 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,13 @@
16
16
<br/>
17
17
18
18
## :star: Features
19
+
- i18n resource pre-compilation
19
20
-`i18n` custom block
20
21
- i18n resource definition
21
22
- i18n resource importing
22
23
- Locale of i18n resource definition
23
24
- Locale of i18n resource definition for global scope
24
25
- i18n resource formatting
25
-
- i18n resource optimaization
26
26
27
27
28
28
## :cd: Installation
@@ -39,9 +39,84 @@ $ npm i --save-dev @intlify/vue-i18n-loader@next
39
39
$ yarn add -D @intlify/vue-i18n-loader@next
40
40
```
41
41
42
+
## :rocket: i18n resource pre-compilation
43
+
44
+
### Why do we need to require the configuration?
45
+
46
+
Since [email protected], 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.
47
+
48
+
However, with the message compiler, the javascript function conversion will not work in some environments (e.g. CSP). For this reason, [email protected] and later offer a full version that includes compiler and runtime, and a runtime only version.
49
+
50
+
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`.
51
+
52
+
You can pre-commpile by configuring vue-i18n-loader as the webpack loader.
53
+
54
+
### Webpack configration
55
+
56
+
As an example, if your project has the locale messagess in `src/locales`, your webpack config will look like this:
57
+
58
+
```
59
+
├── dist
60
+
├── index.html
61
+
├── package.json
62
+
├── src
63
+
│ ├── App.vue
64
+
│ ├── locales
65
+
│ │ ├── en.json
66
+
│ │ └── ja.json
67
+
│ └── main.js
68
+
└── webpack.config.js
69
+
```
70
+
71
+
```js
72
+
import { createApp } from'vue'
73
+
import { createI18n } from'vue-i18n'// import from runtime only
74
+
importAppfrom'./App.vue'
75
+
76
+
// import i18n resources
77
+
importenfrom'./locale/en.json'
78
+
importjafrom'./locale/ja.json'
79
+
80
+
consti18n=createI18n({
81
+
locale:'ja',
82
+
messages: {
83
+
en,
84
+
ja
85
+
}
86
+
})
87
+
88
+
constapp=createApp(App)
89
+
app.use(i18n)
90
+
app.mount('#app')
91
+
```
92
+
93
+
In the case of the above project, you can use vue-i18n with webpack configuration to the following for runtime only:
94
+
95
+
```javascript
96
+
module.exports= {
97
+
module: {
98
+
rules: [
99
+
// ...
100
+
{
101
+
test:/\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
102
+
type:'javascript/auto',
103
+
loader:'@intlify/vue-i18n-loader',
104
+
include: [ // Use `Rule.include` to specify the files of locale messages to be pre-compiled
105
+
path.resolve(__dirname, 'src/locales')
106
+
]
107
+
},
108
+
// ...
109
+
]
110
+
}
111
+
}
112
+
```
113
+
114
+
The above uses webpack's `Rule.include` to specify the i18n resources to be precompiled. You can also use [`Rule.exclude`](https://webpack.js.org/configuration/module/#ruleexclude) to set the target.
115
+
116
+
42
117
## :rocket:`i18n` custom block
43
118
44
-
the below example that`App.vue` have `i18n` custom block:
119
+
The below example that`App.vue` have `i18n` custom block:
45
120
46
121
### i18n resource definition
47
122
@@ -89,7 +164,7 @@ The locale messages defined at `i18n` custom blocks are **json format default**.
89
164
90
165
### i18n resource importing
91
166
92
-
you also can use `src` attribute:
167
+
You also can use `src` attribute:
93
168
94
169
```vue
95
170
<i18n src="./myLang.json"></i18n>
@@ -219,6 +294,36 @@ module.exports = {
219
294
}
220
295
```
221
296
297
+
## :rocket: loader options
298
+
299
+
### forceStringify
300
+
301
+
Whether pre-compile number and boolean values as message functions that return the string value, default `false`
0 commit comments