@@ -3,72 +3,80 @@ import get from "lodash-es/get";
3
3
import trim from "lodash-es/trim" ;
4
4
import has from "lodash-es/has" ;
5
5
6
- /**
7
- * I18n Composable
8
- *
9
- * @composable
10
- */
11
- export default function useI18n < L extends Record < string , string | Record < string , string > > > (
12
- options : { locale ?: L } = { }
13
- ) {
6
+ import type { iPluginOptions } from "@open-xamu-co/ui-common-types" ;
7
+
8
+ interface iUseI18n < L extends Record < string , string | Record < string , string > > > {
14
9
/**
15
10
* Interpolates localized text
16
11
*
17
12
* @param key key to the text to interpolate. Ex: "hello_name" => "Hello {name}!"
18
13
* @param data Optional number or variables to interpolate into text
19
14
* @returns {string }
20
15
*/
21
- function t < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
16
+ t : < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
22
17
key : Ko extends string ? K : `${K } .${KA } `,
23
- data : number | { [ key : string ] : unknown ; count ?: number } = { } ,
24
- fallback = `No locale for "${ key } " provided`
25
- ) : string {
26
- // Empty string string if locale doesn't exist
27
- let locale = get ( options . locale || { } , key , fallback ) ;
28
- const interpolate = / \{ ( .+ ?) \} / g;
29
- const plurals = locale . split ( "|" ) ;
30
- const count = typeof data === "number" ? data : ( data ?. count ?? - 1 ) ;
31
-
32
- // Pluralization
33
- if ( count > - 1 && plurals . length > 1 ) {
34
- if ( plurals . length === 2 ) {
35
- // product, products
36
- locale = plurals [ count > 1 ? 1 : 0 ] ;
37
- } else if ( plurals . length === 3 ) {
38
- // no products, a product, products
39
- locale = plurals [ count ? ( count > 1 ? 2 : 1 ) : 0 ] ;
40
- }
41
- }
42
-
43
- const compile = template ( trim ( locale ) , { interpolate } ) ;
44
-
45
- return compile ( typeof data === "number" ? { count } : data ) ;
46
- }
47
-
18
+ data ?: number | { [ key : string ] : unknown ; count ?: number } ,
19
+ fallback ?: string
20
+ ) => string ;
48
21
/**
49
22
* Checks if the interpolation key exist
50
23
*
51
24
* @param key interpolation key to check
52
25
* @returns {boolean } true if the key exists
53
26
*/
54
- function te < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
27
+ te : < K extends string & keyof L , Ko extends L [ K ] , KA extends string & keyof Ko > (
55
28
key : string
56
- ) : key is Ko extends string ? K : `${K } .${KA } ` {
57
- return has ( options . locale || { } , key ) ;
58
- }
59
-
29
+ ) => key is Ko extends string ? K : `${K } .${KA } `;
60
30
/**
61
31
* Returns translation if key exist
62
32
* @param key interpolation key to check
63
33
* @returns {string }
64
34
*/
65
- function tet ( key : string ) : string {
66
- return ( te ( key ) && t ( key ) ) || key ;
67
- }
35
+ tet : ( key : string ) => string ;
36
+ }
68
37
38
+ /**
39
+ * I18n Composable
40
+ *
41
+ * @composable
42
+ */
43
+ export default function useI18n < L extends Record < string , string | Record < string , string > > > (
44
+ options : iPluginOptions & { locale ?: L } = { }
45
+ ) : iUseI18n < L > {
69
46
return {
70
- t,
71
- te,
72
- tet,
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 ) ;
57
+
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
+ }
67
+ }
68
+
69
+ const compile = template ( trim ( locale ) , { interpolate } ) ;
70
+
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
+ } ,
78
+ tet ( key : string ) : string {
79
+ return ( this . te ( key ) && this . t ( key ) ) || key ;
80
+ } ,
73
81
} ;
74
82
}
0 commit comments