@@ -37,15 +37,15 @@ function assignValuesWithPath (path, value, messages) {
37
37
38
38
function getLocales ( targetPath ) {
39
39
debug ( 'getLocales' , targetPath )
40
- return fs . readdirSync ( ` ${ targetPath } /src/locales` ) . map ( locale => {
40
+ return fs . readdirSync ( targetPath ) . map ( locale => {
41
41
return path . basename ( locale , '.json' )
42
42
} )
43
43
}
44
44
45
45
function getLocaleMessages ( targetPath , locales ) {
46
46
debug ( 'getLocaleMessages' , targetPath , locales )
47
47
return locales . reduce ( ( val , locale ) => {
48
- const fullPath = `${ targetPath } /src/locales/ ${ locale } .json`
48
+ const fullPath = `${ targetPath } /${ locale } .json`
49
49
val [ locale ] = require ( fullPath )
50
50
delete require . cache [ require . resolve ( fullPath ) ]
51
51
return val
@@ -65,52 +65,60 @@ function writeLocaleMessages (targetPath, locale, messages, order) {
65
65
const sortedMessages = sortObject ( messages , order )
66
66
debug ( 'writeLocaleMessages after:' , sortedMessages )
67
67
return fs . writeFileSync (
68
- `${ targetPath } /src/locales/ ${ locale } .json` ,
68
+ `${ targetPath } /${ locale } .json` ,
69
69
JSON . stringify ( sortedMessages , null , 2 ) , { encoding : 'utf8' }
70
70
)
71
71
}
72
72
73
73
module . exports = api => {
74
74
const { getSharedData, setSharedData, watchSharedData } = api . namespace ( 'vue-i18n-' )
75
75
76
- function setupAddon ( path ) {
77
- debug ( `setupAddon: path -> ${ path } ` )
76
+ function setupAddon ( path , options ) {
77
+ debug ( `setupAddon: path -> ${ path } , options -> ${ options } ` )
78
+ const localeDir = options . localeDir
78
79
setSharedData ( 'order' , 'asc' )
79
80
const env = readEnv ( `${ path } /.env` )
80
81
const current = env [ 'VUE_APP_I18N_LOCALE' ] || 'en'
81
82
const defaultLocale = env [ 'VUE_APP_I18N_FALLBACK_LOCALE' ] || 'en'
82
83
setSharedData ( 'current' , defaultLocale )
83
84
setSharedData ( 'defaultLocale' , defaultLocale )
84
85
debug ( `setupAddon: current -> ${ current } , defaultLocale -> ${ defaultLocale } ` )
85
- const locales = getLocales ( path )
86
+ const locales = getLocales ( ` ${ path } /src/ ${ localeDir } ` )
86
87
setSharedData ( 'locales' , locales )
87
- const messages = getLocaleMessages ( path , locales )
88
+ const messages = getLocaleMessages ( ` ${ path } /src/ ${ localeDir } ` , locales )
88
89
setSharedData ( 'localeMessages' , messages )
89
90
setSharedData ( 'localePaths' , getLocalePaths ( messages ) )
90
91
}
91
92
92
93
try {
93
94
let currentProject = null
95
+ let currentConfig = null
94
96
95
97
api . onProjectOpen ( ( project , previousProject ) => {
96
98
debug ( 'onProjectOpen' , project , previousProject )
97
99
} )
98
100
99
101
api . onPluginReload ( project => {
100
102
debug ( 'onPluginReload' , project )
101
- setupAddon ( project . path )
103
+ const rawConfig = require ( `${ project . path } /vue.config` )
104
+ const config = rawConfig . pluginOptions || { localeDir : 'locales' }
105
+ if ( ! config . localeDir ) { config . localeDir = 'locales' }
106
+ debug ( 'onPluginReload : load vue.config' , config )
107
+ setupAddon ( project . path , config )
102
108
currentProject = project
109
+ currentConfig = config
103
110
} )
104
111
105
112
api . onAction ( 'add-path-action' , ( { path, locale } ) => {
106
113
debug ( 'add-path-action onAction' , path , locale )
107
- if ( ! currentProject && ! path ) {
114
+ if ( ! currentProject || ! currentConfig || ! path ) {
108
115
console . error ( 'add-path-action: invalid pre-condition !!' )
109
116
return
110
117
}
111
118
112
- const locales = getLocales ( currentProject . path )
113
- const messages = getLocaleMessages ( currentProject . path , locales )
119
+ const localePath = `${ currentProject . path } /src/${ currentConfig . localeDir } `
120
+ const locales = getLocales ( localePath )
121
+ const messages = getLocaleMessages ( localePath , locales )
114
122
const orderData = getSharedData ( 'order' )
115
123
116
124
const additional = { }
@@ -120,21 +128,22 @@ module.exports = api => {
120
128
debug ( 'additional' , additional )
121
129
const message = deepmerge ( original , unflatten ( additional ) )
122
130
debug ( 'merged' , message )
123
- writeLocaleMessages ( currentProject . path , locale , message , orderData . value )
131
+ writeLocaleMessages ( localePath , locale , message , orderData . value )
124
132
messages [ locale ] = message
125
133
setSharedData ( 'localeMessages' , messages )
126
134
setSharedData ( 'localePaths' , getLocalePaths ( messages ) )
127
135
} )
128
136
129
137
api . onAction ( 'update-path-action' , ( { path, old, locale } ) => {
130
138
debug ( 'update-path-action onAction' , path , old , locale )
131
- if ( ! currentProject && ! path && ! old ) {
139
+ if ( ! currentProject || ! currentConfig || ! path || ! old ) {
132
140
console . error ( 'update-path-action: invalid pre-condition !!' )
133
141
return
134
142
}
135
143
136
- const locales = getLocales ( currentProject . path )
137
- const messages = getLocaleMessages ( currentProject . path , locales )
144
+ const localePath = `${ currentProject . path } /src/${ currentConfig . localeDir } `
145
+ const locales = getLocales ( localePath )
146
+ const messages = getLocaleMessages ( localePath , locales )
138
147
const orderData = getSharedData ( 'order' )
139
148
140
149
const original = messages [ locale ]
@@ -143,28 +152,29 @@ module.exports = api => {
143
152
delete flattendOriginal [ old ]
144
153
const newMessage = unflatten ( flattendOriginal )
145
154
assignValuesWithPath ( path , oldValues , newMessage )
146
- writeLocaleMessages ( currentProject . path , locale , newMessage , orderData . value )
155
+ writeLocaleMessages ( localePath , locale , newMessage , orderData . value )
147
156
messages [ locale ] = newMessage
148
157
setSharedData ( 'localeMessages' , messages )
149
158
setSharedData ( 'localePaths' , getLocalePaths ( messages ) )
150
159
} )
151
160
152
161
api . onAction ( 'delete-path-action' , ( { path, locale } ) => {
153
162
debug ( 'delete-path-action onAction' , path , locale )
154
- if ( ! currentProject && ! path ) {
163
+ if ( ! currentProject || ! currentConfig || ! path ) {
155
164
console . log ( 'delete-path-action: invalid pre-condition' )
156
165
return
157
166
}
158
167
159
- const locales = getLocales ( currentProject . path )
160
- const messages = getLocaleMessages ( currentProject . path , locales )
168
+ const localePath = `${ currentProject . path } /src/${ currentConfig . localeDir } `
169
+ const locales = getLocales ( localePath )
170
+ const messages = getLocaleMessages ( localePath , locales )
161
171
const orderData = getSharedData ( 'order' )
162
172
163
173
const message = messages [ locale ]
164
174
const flattendMessage = flatten ( message )
165
175
delete flattendMessage [ path ]
166
176
messages [ locale ] = unflatten ( flattendMessage )
167
- const ret = writeLocaleMessages ( currentProject . path , locale , messages [ locale ] , orderData . value )
177
+ const ret = writeLocaleMessages ( localePath , locale , messages [ locale ] , orderData . value )
168
178
debug ( 'write data' , ret )
169
179
170
180
setSharedData ( 'localeMessages' , messages )
@@ -173,46 +183,48 @@ module.exports = api => {
173
183
174
184
api . onAction ( 'edit-message-action' , ( { path, value, locale } ) => {
175
185
debug ( 'edit-message-action onAction' , path , value , locale )
176
- if ( ! currentProject && ! path ) {
186
+ if ( ! currentProject || ! currentConfig || ! path ) {
177
187
console . error ( 'edit-message-action: invalid pre-condition !!' )
178
188
return
179
189
}
180
190
181
- const locales = getLocales ( currentProject . path )
182
- const localeMessages = getLocaleMessages ( currentProject . path , locales )
191
+ const localePath = `${ currentProject . path } /src/${ currentConfig . localeDir } `
192
+ const locales = getLocales ( localePath )
193
+ const localeMessages = getLocaleMessages ( localePath , locales )
183
194
const orderData = getSharedData ( 'order' )
184
195
185
196
const messages = localeMessages [ locale ]
186
197
const flattendMessage = flatten ( messages )
187
198
flattendMessage [ path ] = value
188
199
localeMessages [ locale ] = unflatten ( flattendMessage )
189
- writeLocaleMessages ( currentProject . path , locale , localeMessages [ locale ] , orderData . value )
200
+ writeLocaleMessages ( localePath , locale , localeMessages [ locale ] , orderData . value )
190
201
191
202
setSharedData ( 'localeMessages' , localeMessages )
192
203
setSharedData ( 'localePaths' , getLocalePaths ( localeMessages ) )
193
204
} )
194
205
195
206
api . onAction ( 'add-locale-action' , ( { locale } ) => {
196
207
debug ( 'add-locale-action onAction' , locale )
197
- if ( ! currentProject && ! locale ) {
208
+ if ( ! currentProject || ! currentConfig || ! locale ) {
198
209
console . error ( 'add-locale-action: invalid pre-condition' )
199
210
return
200
211
}
201
212
213
+ const localePath = `${ currentProject . path } /src/${ currentConfig . localeDir } `
202
214
const defaultLocaleData = getSharedData ( 'defaultLocale' )
203
215
const defaultLocale = defaultLocaleData . value
204
- const oldMessages = getLocaleMessages ( currentProject . path , getLocales ( currentProject . path ) )
216
+ const oldMessages = getLocaleMessages ( localePath , getLocales ( localePath ) )
205
217
const orderData = getSharedData ( 'order' )
206
218
207
219
const oldFlattendMessages = flatten ( oldMessages [ defaultLocale ] )
208
220
const newLocaleMessages = Object . keys ( oldFlattendMessages ) . reduce ( ( val , p ) => {
209
221
val [ p ] = oldFlattendMessages [ p ]
210
222
return val
211
223
} , { } )
212
- writeLocaleMessages ( currentProject . path , locale , unflatten ( newLocaleMessages ) , orderData . value )
224
+ writeLocaleMessages ( localePath , locale , unflatten ( newLocaleMessages ) , orderData . value )
213
225
214
- const locales = getLocales ( currentProject . path )
215
- const messages = getLocaleMessages ( currentProject . path , locales )
226
+ const locales = getLocales ( localePath )
227
+ const messages = getLocaleMessages ( localePath , locales )
216
228
setSharedData ( 'localeMessages' , messages )
217
229
setSharedData ( 'localePaths' , getLocalePaths ( messages ) )
218
230
setSharedData ( 'locales' , locales )
0 commit comments