@@ -43,40 +43,44 @@ interface iUseI18n<L extends Record<string, string | Record<string, string>>> {
43
43
export default function useI18n < L extends Record < string , string | Record < string , string > > > (
44
44
options : iPluginOptions & { locale ?: L } = { }
45
45
) : iUseI18n < L > {
46
- return {
47
- t < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
48
- key : Ko extends string ? K : `${K } .${KA } `,
49
- data : number | { [ key : string ] : unknown ; count ?: number } = { } ,
50
- fallback = `No locale for "${ key } " provided`
51
- ) : string {
52
- // Empty string string if locale doesn't exist
53
- let locale = get ( options . locale || { } , key , fallback ) ;
54
- const interpolate = / \{ ( .+ ?) \} / g;
55
- const plurals = locale . split ( "|" ) ;
56
- const count = typeof data === "number" ? data : ( data ?. count ?? - 1 ) ;
46
+ function t < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
47
+ key : Ko extends string ? K : `${K } .${KA } `,
48
+ data : number | { [ key : string ] : unknown ; count ?: number } = { } ,
49
+ fallback = `No locale for "${ key } " provided`
50
+ ) : string {
51
+ // Empty string string if locale doesn't exist
52
+ let locale = get ( options . locale || { } , key , fallback ) ;
53
+ const interpolate = / \{ ( .+ ?) \} / g;
54
+ const plurals = locale . split ( "|" ) ;
55
+ const count = typeof data === "number" ? data : ( data ?. count ?? - 1 ) ;
57
56
58
- // Pluralization
59
- if ( count > - 1 && plurals . length > 1 ) {
60
- if ( plurals . length === 2 ) {
61
- // product, products
62
- locale = plurals [ count > 1 ? 1 : 0 ] ;
63
- } else if ( plurals . length === 3 ) {
64
- // no products, a product, products
65
- locale = plurals [ count ? ( count > 1 ? 2 : 1 ) : 0 ] ;
66
- }
57
+ // Pluralization
58
+ if ( count > - 1 && plurals . length > 1 ) {
59
+ if ( plurals . length === 2 ) {
60
+ // product, products
61
+ locale = plurals [ count > 1 ? 1 : 0 ] ;
62
+ } else if ( plurals . length === 3 ) {
63
+ // no products, a product, products
64
+ locale = plurals [ count ? ( count > 1 ? 2 : 1 ) : 0 ] ;
67
65
}
66
+ }
68
67
69
- const compile = template ( trim ( locale ) , { interpolate } ) ;
68
+ const compile = template ( trim ( locale ) , { interpolate } ) ;
70
69
71
- return compile ( typeof data === "number" ? { count } : data ) ;
72
- } ,
73
- te < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
74
- key : string
75
- ) : key is Ko extends string ? K : `${K } .${KA } ` {
76
- return has ( options . locale || { } , key ) ;
77
- } ,
70
+ return compile ( typeof data === "number" ? { count } : data ) ;
71
+ }
72
+
73
+ function te < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
74
+ key : string
75
+ ) : key is Ko extends string ? K : `${K } .${KA } ` {
76
+ return has ( options . locale || { } , key ) ;
77
+ }
78
+
79
+ return {
80
+ t,
81
+ te,
78
82
tet ( key : string ) : string {
79
- return ( this . te ( key ) && this . t ( key ) ) || key ;
83
+ return ( te ( key ) && t ( key ) ) || key ;
80
84
} ,
81
85
} ;
82
86
}
0 commit comments