11import { isForbiddenPropertyName } from 'botframework-webchat-core' ;
22import { useCallback } from 'react' ;
33
4+ import { isPlainObject } from '@msinternal/botframework-webchat-base/utils' ;
45import getAllLocalizedStrings from '../localization/getAllLocalizedStrings' ;
56import useLocalizedGlobalize from './internal/useLocalizedGlobalize' ;
67import useLocalizedStrings from './internal/useLocalizedStrings' ;
7- import { isPlainObject } from '@msinternal/botframework-webchat-base/utils' ;
88
99const DEFAULT_STRINGS = getAllLocalizedStrings ( ) [ 'en-US' ] ;
1010
@@ -17,12 +17,18 @@ type Plural = {
1717 other : string ;
1818} ;
1919
20- export default function useLocalizer ( { plural } : { plural ?: boolean } = { } ) {
20+ type PluralLocalizer = ( id : Plural , arg0 : number , ...argRest : readonly string [ ] ) => string ;
21+ type SingularLocalizer = ( id : string , ...argRest : readonly string [ ] ) => string ;
22+
23+ function useLocalizer ( init : { plural : true } ) : PluralLocalizer ;
24+ function useLocalizer ( init ?: { plural ?: false } | undefined ) : SingularLocalizer ;
25+
26+ function useLocalizer ( { plural } : { plural ?: boolean } = { } ) : PluralLocalizer | SingularLocalizer {
2127 const [ globalize ] = useLocalizedGlobalize ( ) ;
2228 const localizedStrings = useLocalizedStrings ( ) ;
2329
2430 return useCallback (
25- ( id : string | Plural , ... args : [ ( number | string ) ? , ...string [ ] ] ) => {
31+ ( id : string | Plural , arg0 : number | string , ...argRest : readonly string [ ] ) => {
2632 let stringId = id as string ;
2733
2834 if ( plural ) {
@@ -32,7 +38,7 @@ export default function useLocalizer({ plural }: { plural?: boolean } = {}) {
3238 throw new Error ( 'useLocalizer: Plural string must pass "id" as a plain object instead of string.' ) ;
3339 } else if ( typeof pluralId . other !== 'string' ) {
3440 throw new Error ( 'useLocalizer: Plural string must have "id.other" of string.' ) ;
35- } else if ( typeof args [ 0 ] !== 'number' ) {
41+ } else if ( typeof arg0 !== 'number' ) {
3642 throw new Error ( 'useLocalizer: Plural string must have first argument as a number.' ) ;
3743 }
3844
@@ -58,12 +64,12 @@ export default function useLocalizer({ plural }: { plural?: boolean } = {}) {
5864 ) ;
5965 }
6066
61- stringId = pluralId [ globalize . plural ( args [ 0 ] ) ] || pluralId . other ;
67+ stringId = pluralId [ globalize . plural ( arg0 ) ] || pluralId . other ;
6268 } else if ( typeof id !== 'string' ) {
6369 throw new Error ( 'useLocalizer: "id" must be a string.' ) ;
6470 }
6571
66- return Object . entries ( args ) . reduce (
72+ return Object . entries ( [ arg0 , ... argRest ] ) . reduce (
6773 ( str , [ index , arg ] ) => str . replace ( `$${ + index + 1 } ` , arg ) ,
6874 // Mitigation through denylisting.
6975 // eslint-disable-next-line security/detect-object-injection
@@ -73,3 +79,5 @@ export default function useLocalizer({ plural }: { plural?: boolean } = {}) {
7379 [ globalize , localizedStrings , plural ]
7480 ) ;
7581}
82+
83+ export default useLocalizer ;
0 commit comments