@@ -9,32 +9,39 @@ const CONSOLE_PREFIX = '[vue-i18n]'
9
9
export const injectionKey = Symbol ( 'i18n' ) as InjectionKey < UseI18n >
10
10
11
11
export function createI18n ( config : I18nConfig ) : I18nInstance {
12
- const { defaultLocale = 'en' } = config
12
+ const {
13
+ defaultLocale = 'en' ,
14
+ logLevel = 'warn' ,
15
+ } = config
13
16
const messages = reactive ( klona ( config . messages ?? { } ) )
14
17
const locale = ref ( defaultLocale )
15
18
const locales = config . locales ?? ( Object . keys ( messages ) . length ? Object . keys ( messages ) : [ locale . value ] )
16
19
17
20
const t = ( key : string , params ?: Record < string , any > ) => {
18
21
if ( typeof key !== 'string' ) {
19
- console . warn ( CONSOLE_PREFIX , `Message "${ key } " must be a string` )
22
+ if ( logLevel === 'warn' )
23
+ console . warn ( CONSOLE_PREFIX , `Message "${ key } " must be a string` )
20
24
return ''
21
25
}
22
26
23
27
try {
24
28
return getLocalizedMessage ( key . split ( '.' ) , messages [ locale . value ] , params )
25
29
}
26
30
catch ( error ) {
27
- console . warn ( CONSOLE_PREFIX , ( error as Error ) . message )
31
+ if ( logLevel === 'warn' )
32
+ console . warn ( CONSOLE_PREFIX , ( error as Error ) . message )
28
33
return key
29
34
}
30
35
}
31
36
32
37
const setLocale = ( newLocale : string ) => {
33
38
if ( ! locales . includes ( newLocale ) ) {
34
- console . warn (
35
- CONSOLE_PREFIX ,
39
+ if ( logLevel === 'warn' ) {
40
+ console . warn (
41
+ CONSOLE_PREFIX ,
36
42
`Locale "${ newLocale } " is not defined in the locales list. Available locales: ${ locales . join ( ', ' ) } ` ,
37
- )
43
+ )
44
+ }
38
45
return
39
46
}
40
47
0 commit comments