Skip to content
This repository was archived by the owner on Jul 4, 2021. It is now read-only.

Commit d28a0e4

Browse files
authored
support i18n resource pre compilation (#23)
* support i18n resource pre compilation * update * update docs * bump up * update * updates * support json * update
1 parent 13ec00c commit d28a0e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+629
-5064
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
'@typescript-eslint/member-delimiter-style': 'off',
3333
'@typescript-eslint/no-use-before-define': 'off',
3434
'@typescript-eslint/explicit-module-boundary-types': 'off',
35-
'@typescript-eslint/no-non-null-assertion': 'off'
35+
'@typescript-eslint/no-non-null-assertion': 'off',
36+
'@typescript-eslint/ban-ts-comment': 'off'
3637
}
3738
}

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# These are supported funding model platforms
22

33
github: kazupon
4-
patreon: kazupon
4+
patreon: #
55
open_collective: # Replace with a single Open Collective username
66
ko_fi: # Replace with a single Ko-fi username
77
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ lib
66
*.swp
77
*~
88
.vscode
9+
examples/**/dist

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
lib
22
coverage
33
tsconfig.json
4+
dist

README.md

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![Test](https://github.com/intlify/vite-plugin-vue-i18n/workflows/Test/badge.svg)
44
[![npm](https://img.shields.io/npm/v/@intlify/vite-plugin-vue-i18n.svg)](https://www.npmjs.com/package/@intlify/vite-plugin-vue-i18n)
55

6-
Vite plugin for custom blocks
6+
Vite plugin for i18n resource pre-compilation and custom blocks
77

88
## :cd: Installation
99

@@ -21,9 +21,39 @@ $ yarn add -D @intlify/vite-plugin-vue-i18n
2121

2222
## :rocket: Usages
2323

24+
### i18n resource pre-compilation
25+
26+
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.
27+
28+
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.
29+
30+
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`.
31+
32+
#### Vite Config
33+
34+
the below example that `examples/composition/vite.config.ts`:
35+
36+
```ts
37+
import path from 'path'
38+
import { pluginI18n } from '@intlify/vite-plugin-vue-i18n'
39+
40+
import type { UserConfig } from 'vite'
41+
42+
const config: UserConfig = {
43+
plugins: [
44+
pluginI18n({
45+
// you need to set i18n resource including paths !
46+
include: path.resolve(__dirname, './path/to/src/locales/**')
47+
})
48+
]
49+
}
50+
51+
export default config
52+
```
53+
2454
### `i18n` custom block
2555

26-
the below example that `examples/composable/App.vue` have `i18n` custom block:
56+
the below example that `examples/composition/App.vue` have `i18n` custom block:
2757

2858
```vue
2959
<template>
@@ -67,62 +97,47 @@ export default {
6797
6898
```
6999

70-
### Vite Config
100+
### Locale Messages formatting
71101

72-
the below example that `examples/composition/vite.config.ts`:
102+
You can be used by specifying the following format in the `lang` attribute:
73103

74-
```ts
75-
import type { UserConfig } from 'vite'
76-
import { transformI18n } from '@intlify/vite-plugin-vue-i18n'
104+
- json (default)
105+
- yaml
106+
- json5
77107

78-
const config: UserConfig = {
79-
vueCustomBlockTransforms: {
80-
i18n: transformI18n()
81-
}
82-
}
108+
example `yaml` foramt:
83109

84-
export default config
110+
```vue
111+
<i18n lang="yaml">
112+
en:
113+
hello: "Hello World!"
114+
ja:
115+
hello: "こんにちは、世界!"
116+
</i18n>
85117
```
86118

87-
88119
### `forceStringify` options
89120

90121
Whether pre-compile number and boolean values as message functions that return the string value, default `false`
91122

92123
```ts
124+
import path from 'path'
125+
import { pluginI18n } from '@intlify/vite-plugin-vue-i18n'
126+
93127
import type { UserConfig } from 'vite'
94-
import { transformI18n } from '@intlify/vite-plugin-vue-i18n'
95128

96129
const config: UserConfig = {
97-
vueCustomBlockTransforms: {
98-
i18n: transformI18n({
99-
forceStringify: true
130+
plugins: [
131+
pluginI18n({
132+
forceStringify: true,
133+
include: path.resolve(__dirname, './path/to/src/locales/**')
100134
})
101-
}
135+
]
102136
}
103137

104138
export default config
105139
```
106140

107-
### Locale Messages formatting
108-
109-
You can be used by specifying the following format in the `lang` attribute:
110-
111-
- json (default)
112-
- yaml
113-
- json5
114-
115-
example `yaml` foramt:
116-
117-
```vue
118-
<i18n lang="yaml">
119-
en:
120-
hello: "Hello World!"
121-
ja:
122-
hello: "こんにちは、世界!"
123-
</i18n>
124-
```
125-
126141

127142
## :scroll: Changelog
128143
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/intlify/vite-plugin-vue-i18n/blob/master/CHANGELOG.md).

examples/composition/App.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
</select>
88
</form>
99
<p>{{ t('hello') }}</p>
10+
<Banana />
1011
</template>
1112

1213
<script>
1314
import { useI18n } from 'vue-i18n'
15+
import Banana from './Banana.vue'
16+
1417
export default {
1518
name: 'App',
19+
components: {
20+
Banana
21+
},
1622
setup() {
1723
const { t, locale } = useI18n({
18-
inheritLocale: true
24+
inheritLocale: true,
25+
useScope: 'local'
1926
})
20-
2127
return { t, locale }
2228
}
2329
}

examples/composition/Banana.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<form id="fruits">
3+
<label>{{ t('select') }}</label>
4+
<select v-model.number="select">
5+
<option value="0">0</option>
6+
<option value="1">1</option>
7+
<option value="2">2</option>
8+
<option value="3">3</option>
9+
</select>
10+
</form>
11+
<p>{{ t('fruits.banana', select, { n: select }) }}</p>
12+
</template>
13+
14+
<script>
15+
import { ref } from 'vue'
16+
import { useI18n } from 'vue-i18n'
17+
18+
export default {
19+
name: 'Banana',
20+
setup() {
21+
const { t } = useI18n({ useScope: 'global' })
22+
const select = ref(0)
23+
return { t, select }
24+
}
25+
}
26+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select: Do you want banana?
2+
fruits:
3+
banana: 'no bananas | {n} banana | {n} bananas'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
select: "バナナが欲しい?",
3+
fruits: {
4+
banana: "バナナがない | バナナ {n} 個"
5+
}
6+
}

examples/composition/main.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import { createApp } from 'vue'
22
import { createI18n } from 'vue-i18n'
33
import App from './App.vue'
44

5+
import ja from './locales/ja.json5'
6+
import en from './locales/en.yaml'
7+
58
const i18n = createI18n({
69
legacy: false,
710
locale: 'ja',
8-
messages: {}
11+
messages: {
12+
en,
13+
ja
14+
}
915
})
1016

1117
const app = createApp(App)

0 commit comments

Comments
 (0)