1
+ import path from 'path'
1
2
import { reactive , Plugin } from 'vue'
2
3
import { OptionsInterface } from './interfaces/options'
3
4
import { LanguageInterface } from './interfaces/language'
5
+ import { LanguageJsonFileInterface } from './interfaces/language-json-file'
4
6
import { ReplacementsInterface } from './interfaces/replacements'
5
7
import { choose } from './pluralization'
6
8
9
+ /**
10
+ * Resolves the lang location, on a Laravel App.
11
+ */
12
+ const defaultResolve = ( lang : string ) : Promise < LanguageJsonFileInterface > => {
13
+ return import ( `..${ path . sep } ..${ path . sep } ..${ path . sep } ..${ path . sep } resources${ path . sep } lang/${ lang } .json` )
14
+ }
15
+
7
16
/**
8
17
* The default options, for the plugin.
9
18
*/
10
19
const DEFAULT_OPTIONS : OptionsInterface = {
11
20
lang : document . documentElement . lang || 'en' ,
12
- resolve : ( lang : string ) => new Promise ( ( resolve ) => resolve ( { default : { } } ) )
21
+ resolve : defaultResolve
13
22
}
14
23
15
24
/**
16
25
* Stores the current options.
17
26
*/
18
- let options : OptionsInterface = DEFAULT_OPTIONS ;
27
+ let options : OptionsInterface = DEFAULT_OPTIONS
19
28
20
29
/**
21
30
* Stores the loaded languages.
@@ -97,7 +106,7 @@ function makeReplacements(message: string, replacements?: ReplacementsInterface)
97
106
const capitalize = ( s ) => s . charAt ( 0 ) . toUpperCase ( ) + s . slice ( 1 )
98
107
99
108
Object . entries ( replacements || [ ] ) . forEach ( ( [ key , value ] ) => {
100
- value = value . toString ( ) ;
109
+ value = value . toString ( )
101
110
102
111
message = message
103
112
. replace ( `:${ key } ` , value )
@@ -112,19 +121,18 @@ function makeReplacements(message: string, replacements?: ReplacementsInterface)
112
121
* Resets all the data stored in memory.
113
122
*/
114
123
export const reset = ( ) : void => {
115
- loaded = [ ] ;
116
- options = DEFAULT_OPTIONS ;
124
+ loaded = [ ]
125
+ options = DEFAULT_OPTIONS
117
126
118
127
for ( const [ key ] of Object . entries ( activeMessages ) ) {
119
- activeMessages [ key ] = null ;
128
+ activeMessages [ key ] = null
120
129
}
121
130
}
122
131
123
-
124
132
/**
125
133
* Alias to `transChoice` to mimic the same function name from Laravel Framework.
126
134
*/
127
- export const trans_choice = transChoice ;
135
+ export const trans_choice = transChoice
128
136
129
137
/**
130
138
* The Vue Plugin. to be used on your Vue app like this: `app.use(i18nVue)`
@@ -133,7 +141,8 @@ export const i18nVue: Plugin = {
133
141
install : ( app , currentOptions : OptionsInterface = { } ) => {
134
142
options = { ...options , ...currentOptions }
135
143
app . config . globalProperties . $t = ( key : string , replacements : ReplacementsInterface ) => trans ( key , replacements )
136
- app . config . globalProperties . $tChoice = ( key : string , number : number , replacements : ReplacementsInterface ) => transChoice ( key , number , replacements )
144
+ app . config . globalProperties . $tChoice = ( key : string , number : number , replacements : ReplacementsInterface ) =>
145
+ transChoice ( key , number , replacements )
137
146
loadLanguageAsync ( options . lang )
138
147
}
139
148
}
0 commit comments