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

Commit 88646ee

Browse files
authored
feat: global attribute (#16)
1 parent 0a8fc4b commit 88646ee

File tree

14 files changed

+1480
-1491
lines changed

14 files changed

+1480
-1491
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ the below example that `examples/composable/App.vue` have `i18n` custom block:
3939
4040
<script>
4141
import { useI18n } from 'vue-i18n'
42+
4243
export default {
4344
name: 'App',
4445
setup() {
45-
return { ...useI18n({
46+
const { locale, t } = useI18n({
4647
inheritLocale: true
47-
}) }
48+
})
49+
50+
return { locale, t }
4851
}
4952
}
5053
</script>

examples/composable/App.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { useI18n } from 'vue-i18n'
1414
export default {
1515
name: 'App',
1616
setup() {
17-
return { ...useI18n({
17+
const { t, locale } = useI18n({
1818
inheritLocale: true
19-
}) }
19+
})
20+
21+
return { t, locale }
2022
}
2123
}
2224
</script>

examples/composable/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createI18n } from 'vue-i18n'
33
import App from './App.vue'
44

55
const i18n = createI18n({
6+
legacy: false,
67
locale: 'ja',
78
messages: {}
89
})

examples/global/App.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
</template>
11+
12+
<script>
13+
import { useI18n } from 'vue-i18n'
14+
15+
export default {
16+
name: 'App',
17+
setup() {
18+
const { t, locale } = useI18n({
19+
useScope: 'global'
20+
})
21+
22+
return { t, locale }
23+
}
24+
}
25+
</script>
26+
27+
<i18n global>
28+
{
29+
"en": {
30+
"language": "Language",
31+
"hello": "hello, world!"
32+
},
33+
"ja": {
34+
"language": "言語",
35+
"hello": "こんにちは、世界!"
36+
}
37+
}
38+
</i18n>

examples/global/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>vite-plugin-vue-i18n global example</title>
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<script type="module" src="./main.ts"></script>
10+
</body>
11+
</html>

examples/global/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createApp } from 'vue'
2+
import { createI18n } from 'vue-i18n'
3+
import App from './App.vue'
4+
5+
const i18n = createI18n({
6+
legacy: false,
7+
locale: 'ja'
8+
})
9+
10+
const app = createApp(App)
11+
12+
app.use(i18n)
13+
app.mount('#app')

examples/global/vite.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { UserConfig } from 'vite'
2+
import i18n from '../../lib/index'
3+
4+
const config: UserConfig = {
5+
vueCustomBlockTransforms: {
6+
i18n
7+
}
8+
}
9+
10+
export default config

examples/legacy/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { createI18n } from 'vue-i18n'
33
import App from './App.vue'
44

55
const i18n = createI18n({
6-
legacy: true,
76
locale: 'ja',
87
messages: {}
98
})

package.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,38 @@
2626
"dependencies": {
2727
"js-yaml": "^3.14.0",
2828
"json5": "^2.1.3",
29-
"vue-i18n": "^9.0.0-beta.6"
29+
"vue-i18n": "^9.0.0-beta.10"
3030
},
3131
"devDependencies": {
3232
"@rollup/plugin-alias": "^3.1.1",
3333
"@types/debug": "^4.1.5",
34-
"@types/jest": "^26.0.8",
35-
"@types/js-yaml": "^3.12.4",
36-
"@types/jsdom": "^16.2.3",
34+
"@types/jest": "^26.0.16",
35+
"@types/js-yaml": "^3.12.5",
36+
"@types/jsdom": "^16.2.5",
3737
"@types/json5": "^0.0.30",
38-
"@types/node": "^14.11.0",
39-
"@typescript-eslint/eslint-plugin": "^4.4.0",
40-
"@typescript-eslint/parser": "^4.4.0",
38+
"@types/node": "^14.14.10",
39+
"@typescript-eslint/eslint-plugin": "^4.9.0",
40+
"@typescript-eslint/parser": "^4.9.0",
4141
"debug": "^4.1.1",
42-
"eslint": "^7.11.0",
42+
"eslint": "^7.14.0",
4343
"eslint-config-prettier": "^6.11.0",
4444
"eslint-plugin-prettier": "^3.1.3",
4545
"eslint-plugin-vue": "^7.1.0",
4646
"eslint-plugin-vue-libs": "^4.0.0",
47-
"jest": "^26.5.0",
47+
"jest": "^26.6.3",
4848
"jest-puppeteer": "^4.4.0",
4949
"jest-watch-typeahead": "^0.6.1",
5050
"jsdom": "^16.4.0",
5151
"lerna-changelog": "^1.0.1",
5252
"npm-run-all": "^4.1.5",
53-
"opener": "^1.5.1",
53+
"opener": "^1.5.2",
5454
"puppeteer": "^3.3.0",
5555
"serve": "^11.3.2",
56-
"shipjs": "^0.22.0",
56+
"shipjs": "^0.23.0",
5757
"ts-jest": "^26.4.0",
58-
"typescript": "^4.0.3",
59-
"typescript-eslint-language-service": "^4.0.1",
60-
"vite": "1.0.0-rc.4"
58+
"typescript": "^4.1.2",
59+
"typescript-eslint-language-service": "^4.1.2",
60+
"vite": "1.0.0-rc.13"
6161
},
6262
"engines": {
6363
"node": ">= 10"
@@ -81,13 +81,15 @@
8181
"scripts": {
8282
"build": "tsc -p .",
8383
"build:example": "yarn build && npm-run-all --parallel build:example:*",
84-
"build:example:legacy": "vite build -c ./examples/legacy/vite.config.ts --outDir ./examples/legacy",
85-
"build:example:composable": "vite build -c ./examples/composable/vite.config.ts --outDir ./examples/composable",
84+
"build:example:legacy": "npx vite build --config ./examples/legacy/vite.config.ts --outDir ./examples/legacy",
85+
"build:example:composable": "npx vite build --config ./examples/composable/vite.config.ts --outDir ./examples/composable",
86+
"build:example:global": "npx vite build --config ./examples/global/vite.config.ts --outDir ./examples/global",
8687
"clean": "npm-run-all clean:*",
8788
"clean:lib": "rm -rf ./lib/index.js",
8889
"clean:example": "npm-run-all clean:example:*",
8990
"clean:example:legacy": "rm -rf ./examples/legacy/index.js",
9091
"clean:example:composable": "rm -rf ./examples/composable/index.js",
92+
"clean:example:global": "rm -rf ./examples/global/index.js",
9193
"clean:cache:jest": "jest --clearCache",
9294
"clean:coverage": "rm -rf ./coverage",
9395
"coverage": "opener coverage/lcov-report/index.html",

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const i18n: TransformFn = function ({ code, query }) {
1313
debug('vueSFCTransform: query', JSON.stringify(query))
1414

1515
return new Promise<string>(resolve => {
16+
const variableName = query.global ? '__i18nGlobal' : '__i18n'
1617
const result = `export default Comp => {
17-
Comp.__i18n = Comp.__i18n || []
18-
Comp.__i18n.push(${stringify(parse(code.trim(), query), query)})
18+
Comp.${variableName} = Comp.${variableName} || []
19+
Comp.${variableName}.push(${stringify(parse(code.trim(), query), query)})
1920
}`.trim()
2021
resolve(result)
2122
})

0 commit comments

Comments
 (0)