File tree Expand file tree Collapse file tree 11 files changed +162
-7
lines changed Expand file tree Collapse file tree 11 files changed +162
-7
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,19 @@ You can define locale messages for each locale with `locale` attribute in single
120
120
121
121
The above defines two locales, which are merged at target single-file components.
122
122
123
+ ### Locale of i18n resource definition for global scope
124
+
125
+ You can define locale messages for global scope with ` global ` attribute:
126
+
127
+ ``` vue
128
+ <i18n global>
129
+ {
130
+ "en": {
131
+ "hello": "hello world!"
132
+ }
133
+ }
134
+ </i18n>
135
+ ```
123
136
124
137
### i18n resource formatting
125
138
Original file line number Diff line number Diff line change 1
- ; [ 'composable' , 'legacy' ] . forEach ( pattern => {
1
+ ; [ 'composable' , 'legacy' , 'global' ] . forEach ( pattern => {
2
2
describe ( `${ pattern } ` , ( ) => {
3
3
beforeAll ( async ( ) => {
4
4
await page . goto ( `http://localhost:8080/${ pattern } /` )
Original file line number Diff line number Diff line change
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 , messages } = useI18n ({
19
+ useScope: ' global' ,
20
+ locale: ' ja'
21
+ })
22
+
23
+ console .log (' global locale messages' , messages .value )
24
+
25
+ return { t, locale }
26
+ }
27
+ }
28
+ </script >
29
+
30
+ <i18n global>
31
+ {
32
+ "en": {
33
+ "language": "Language",
34
+ "hello": "hello, world!"
35
+ },
36
+ "ja": {
37
+ "language": "言語",
38
+ "hello": "こんにちは、世界!"
39
+ }
40
+ }
41
+ </i18n >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 " />
5
+ < title > vue-i18n-loader global example</ title >
6
+ </ head >
7
+ < body >
8
+ < div id ="app ">
9
+ < App />
10
+ </ div >
11
+ < script src ="/dist/global.js "> </ script >
12
+ </ body >
13
+ </ html >
Original file line number Diff line number Diff line change
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
+ messages : {
9
+ en : {
10
+ foo : 'foo'
11
+ }
12
+ }
13
+ } )
14
+
15
+ const app = createApp ( App )
16
+
17
+ app . use ( i18n )
18
+ app . mount ( '#app' )
Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ module.exports = {
5
5
mode : 'development' ,
6
6
entry : {
7
7
composable : path . resolve ( __dirname , './composable/main.js' ) ,
8
- legacy : path . resolve ( __dirname , './legacy/main.js' )
8
+ legacy : path . resolve ( __dirname , './legacy/main.js' ) ,
9
+ global : path . resolve ( __dirname , './global/main.js' )
9
10
} ,
10
11
output : {
11
12
path : path . resolve ( __dirname , 'dist' ) ,
@@ -42,7 +43,7 @@ module.exports = {
42
43
{
43
44
loader : path . resolve ( __dirname , '../lib/index.js' ) ,
44
45
options : {
45
- preCompile : true
46
+ // preCompile: true
46
47
}
47
48
}
48
49
]
Original file line number Diff line number Diff line change @@ -27,14 +27,21 @@ export function generateCode(
27
27
const preCompile = ! ! options . preCompile
28
28
29
29
if ( preCompile ) {
30
- code += generateCompiledCode ( value as LocaleMessages )
31
- code += `export default function (Component) {
30
+ if ( query . global ) {
31
+ console . warn (
32
+ '[vue-i18n-loader] cannot support global scope for pre-compilation'
33
+ )
34
+ } else {
35
+ code += generateCompiledCode ( value as LocaleMessages )
36
+ code += `export default function (Component) {
32
37
Component.__i18n = Component.__i18n || _getResource
33
38
}\n`
39
+ }
34
40
} else {
41
+ const variableName = query . global ? '__i18nGlobal' : '__i18n'
35
42
code += `export default function (Component) {
36
- Component.__i18n = Component.__i18n || []
37
- Component.__i18n .push(${ stringify ( value ) } )
43
+ Component.${ variableName } = Component.${ variableName } || []
44
+ Component.${ variableName } .push(${ stringify ( value ) } )
38
45
}\n`
39
46
}
40
47
Original file line number Diff line number Diff line change @@ -10,6 +10,36 @@ Array [
10
10
]
11
11
` ;
12
12
13
+ exports [` global 1` ] = `
14
+ Array [
15
+ Object {
16
+ " en" : Object {
17
+ " hello" : " hello global!" ,
18
+ },
19
+ } ,
20
+ ]
21
+ ` ;
22
+
23
+ exports [` global and local 1` ] = `
24
+ Array [
25
+ Object {
26
+ " ja" : Object {
27
+ " hello" : " hello local!" ,
28
+ },
29
+ } ,
30
+ ]
31
+ ` ;
32
+
33
+ exports [` global and local 2` ] = `
34
+ Array [
35
+ Object {
36
+ " en" : Object {
37
+ " hello" : " hello global!" ,
38
+ },
39
+ } ,
40
+ ]
41
+ ` ;
42
+
13
43
exports [` import 1` ] = `
14
44
Array [
15
45
Object {
Original file line number Diff line number Diff line change
1
+ <i18n global>
2
+ {
3
+ "en": {
4
+ "hello": "hello global!"
5
+ }
6
+ }
7
+ </i18n >
8
+
9
+ <i18n locale="ja">
10
+ {
11
+ "hello": "hello local!"
12
+ }
13
+ </i18n >
Original file line number Diff line number Diff line change
1
+ <i18n global>
2
+ {
3
+ "en": {
4
+ "hello": "hello global!"
5
+ }
6
+ }
7
+ </i18n >
You can’t perform that action at this time.
0 commit comments