44 formatDistanceToNow ,
55 formatDistanceToNowStrict ,
66} from 'date-fns'
7- import type { BaseLocale , LocaleValue } from 'international-types'
7+ import type { BaseLocale } from 'international-types'
88import PropTypes from 'prop-types'
99import {
1010 ReactElement ,
@@ -20,21 +20,12 @@ import ReactDOM from 'react-dom'
2020import dateFormat , { FormatDateOptions } from './formatDate'
2121import unitFormat , { FormatUnitOptions } from './formatUnit'
2222import formatters , { IntlListFormatOptions } from './formatters'
23- import type { ScopedTranslateFn , TranslateFn } from './types'
23+ import type { ReactParamsObject , ScopedTranslateFn , TranslateFn } from './types'
2424
2525const LOCALE_ITEM_STORAGE = 'locale'
2626
2727type TranslationsByLocales = Record < string , BaseLocale >
2828
29- export type InitialTranslateFn = (
30- key : string ,
31- context ?: Record < string , LocaleValue | JSX . Element > ,
32- ) => string
33- export type InitialScopedTranslateFn = (
34- namespace : string ,
35- t ?: InitialTranslateFn ,
36- ) => InitialTranslateFn
37-
3829const areNamespacesLoaded = (
3930 namespaces : string [ ] ,
4031 loadedNamespaces : string [ ] = [ ] ,
@@ -62,7 +53,7 @@ const getCurrentLocale = ({
6253 )
6354}
6455
65- interface Context < Locale extends BaseLocale | undefined = undefined > {
56+ interface Context < Locale extends BaseLocale > {
6657 currentLocale : string
6758 dateFnsLocale ?: DateFnsLocale
6859 datetime : (
@@ -82,9 +73,7 @@ interface Context<Locale extends BaseLocale | undefined = undefined> {
8273 ) => Promise < string >
8374 locales : string [ ]
8475 namespaces : string [ ]
85- namespaceTranslation : Locale extends BaseLocale
86- ? ScopedTranslateFn < Locale >
87- : InitialScopedTranslateFn
76+ namespaceTranslation : ScopedTranslateFn < Locale >
8877 relativeTime : (
8978 date : Date | number ,
9079 options ?: {
@@ -102,15 +91,15 @@ interface Context<Locale extends BaseLocale | undefined = undefined> {
10291 ) => string
10392 setTranslations : React . Dispatch < React . SetStateAction < TranslationsByLocales > >
10493 switchLocale : ( locale : string ) => void
105- t : Locale extends BaseLocale ? TranslateFn < Locale > : InitialTranslateFn
94+ t : TranslateFn < Locale >
10695 translations : TranslationsByLocales
10796}
10897
109- const I18nContext = createContext < Context | undefined > ( undefined )
98+ // It's safe to use any here because the Locale can be anything at this point:
99+ // useI18n / useTranslation requires to explicitely give a Locale to use.
100+ const I18nContext = createContext < Context < any > | undefined > ( undefined )
110101
111- export function useI18n <
112- Locale extends BaseLocale | undefined = undefined ,
113- > ( ) : Context < Locale > {
102+ export function useI18n < Locale extends BaseLocale > ( ) : Context < Locale > {
114103 const context = useContext ( I18nContext )
115104 if ( context === undefined ) {
116105 throw new Error ( 'useI18n must be used within a I18nProvider' )
@@ -119,9 +108,7 @@ export function useI18n<
119108 return context as unknown as Context < Locale >
120109}
121110
122- export function useTranslation <
123- Locale extends BaseLocale | undefined = undefined ,
124- > (
111+ export function useTranslation < Locale extends BaseLocale > (
125112 namespaces : string [ ] = [ ] ,
126113 load : LoadTranslationsFn | undefined = undefined ,
127114) : Context < Locale > & { isLoaded : boolean } {
@@ -323,8 +310,8 @@ const I18nContextProvider = ({
323310 [ dateFnsLocale ] ,
324311 )
325312
326- const translate = useCallback < InitialTranslateFn > (
327- ( key , context ) => {
313+ const translate = useCallback (
314+ ( key : string , context ?: ReactParamsObject < any > ) => {
328315 const value = translations [ currentLocale ] ?. [ key ] as string
329316 if ( ! value ) {
330317 if ( enableDebugKey ) {
@@ -344,10 +331,9 @@ const I18nContextProvider = ({
344331 [ currentLocale , translations , enableDebugKey ] ,
345332 )
346333
347- const namespaceTranslation = useCallback < InitialScopedTranslateFn > (
348- ( namespace , t = translate ) =>
349- ( identifier , context ) =>
350- t ( `${ namespace } .${ identifier } ` , context ) || t ( identifier , context ) ,
334+ const namespaceTranslation = useCallback (
335+ ( scope : string ) => ( key : string , context ?: ReactParamsObject < any > ) =>
336+ translate ( `${ scope } .${ key } ` , context ) || translate ( key , context ) ,
351337 [ translate ] ,
352338 )
353339
0 commit comments