@@ -5,21 +5,63 @@ function checkInstalled (target) {
55 let ret = true
66 try {
77 const resolveModule = require ( path . resolve ( target ) )
8- if ( ! resolveModule ) { ret = false }
8+ if ( ! resolveModule ) {
9+ ret = false
10+ }
11+ } catch ( e ) {
12+ ret = false
13+ }
14+ return ret
15+ }
16+
17+ function exists ( path ) {
18+ let ret = true
19+ try {
20+ fs . accessSync ( path , fs . constants . F_OK )
21+ } catch ( e ) {
22+ ret = false
23+ }
24+ return ret
25+ }
26+
27+ function mkdir ( path ) {
28+ let ret = true
29+ try {
30+ fs . mkdirSync ( path )
31+ } catch ( e ) {
32+ ret = false
33+ }
34+ return ret
35+ }
36+
37+ function writeFile ( path , content ) {
38+ let ret = true
39+ try {
40+ fs . writeFileSync ( path , content , { encoding : 'utf8' } )
941 } catch ( e ) {
1042 ret = false
1143 }
1244 return ret
1345}
1446
47+ function readFile ( path ) {
48+ let ret = ''
49+ try {
50+ ret = fs . readFileSync ( path , { encoding : 'utf8' } )
51+ } catch ( e ) {
52+ ret = ''
53+ }
54+ return ret
55+ }
56+
1557module . exports = ( api , options , rootOptions ) => {
16- const { enableInSFC } = options
58+ const { locale , fallbackLocale , enableInSFC } = options
1759
1860 try {
1961 const tsPath = api . resolve ( 'src/main.ts' )
2062 const jsPath = api . resolve ( 'src/main.js' )
21- const tsExists = fs . existsSync ( tsPath )
22- const jsExists = fs . existsSync ( jsPath )
63+ const tsExists = exists ( tsPath )
64+ const jsExists = exists ( jsPath )
2365
2466 if ( ! tsExists && ! jsExists ) {
2567 api . exitLog ( 'No entry found' , 'error' )
@@ -67,9 +109,6 @@ module.exports = (api, options, rootOptions) => {
67109 * render templates
68110 */
69111
70- // common i18n templates
71- api . render ( './templates/common' , { ...additionalOptions } )
72-
73112 // i18n templates for program language
74113 api . render ( `./templates/${ lang } ` , { ...additionalOptions } )
75114
@@ -88,6 +127,59 @@ module.exports = (api, options, rootOptions) => {
88127 api . exitLog ( `unexpected error in vue-cli-plugin-i18n: ${ e . message } ` , 'error' )
89128 }
90129
130+ api . onCreateComplete ( ( ) => {
131+ const defaultLocaleMessages = JSON . stringify ( {
132+ message : 'hello i18n !!'
133+ } , null , 2 )
134+
135+ function writeLocaleFile ( path ) {
136+ if ( ! exists ( path ) ) {
137+ if ( ! writeFile ( path , defaultLocaleMessages ) ) {
138+ api . exitLog ( `cannot make ${ path } ` , 'error' )
139+ }
140+ } else {
141+ console . warn ( `already exist ${ path } ` )
142+ }
143+ }
144+
145+ const localesDirPath = api . resolve ( './src/locales' )
146+ if ( ! exists ( localesDirPath ) ) {
147+ if ( ! mkdir ( localesDirPath ) ) {
148+ api . exitLog ( `cannot make ${ localesDirPath } ` , 'error' )
149+ }
150+ }
151+
152+ writeLocaleFile ( api . resolve ( `./src/locales/${ locale } .json` ) )
153+ if ( locale !== fallbackLocale ) {
154+ writeLocaleFile ( api . resolve ( `./src/locales/${ fallbackLocale } .json` ) )
155+ }
156+
157+ const envPath = api . resolve ( '.env' )
158+ let content = exists ( envPath ) ? readFile ( envPath ) : ''
159+
160+ if ( content . indexOf ( 'VUE_APP_I18N_LOCALE=' ) === - 1 ) {
161+ content += `VUE_APP_I18N_LOCALE=${ locale } \n`
162+ } else {
163+ content = content . replace (
164+ / V U E _ A P P _ I 1 8 N _ L O C A L E = ( .* ) \n / ,
165+ `VUE_APP_I18N_LOCALE=${ locale } `
166+ )
167+ }
168+
169+ if ( content . indexOf ( 'VUE_APP_I18N_FALLBACK_LOCALE=' ) === - 1 ) {
170+ content += `VUE_APP_I18N_FALLBACK_LOCALE=${ fallbackLocale } \n`
171+ } else {
172+ content = content . replace (
173+ / V U E _ A P P _ I 1 8 N _ F A L L B A C K _ L O C A L E = ( .* ) \n / ,
174+ `VUE_APP_I18N_FALLBACK_LOCALE=${ fallbackLocale } `
175+ )
176+ }
177+
178+ if ( ! writeFile ( envPath , content ) ) {
179+ api . exitLog ( `cannot write ${ envPath } ` , 'error' )
180+ }
181+ } )
182+
91183 api . postProcessFiles ( files => {
92184 if ( ! api . hasPlugin ( 'typescript' ) ) { return }
93185 const tsConfigRaw = files [ 'tsconfig.json' ]
0 commit comments