10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
13
+ import { getDefaultLocale , Locale , useDefaultLocale } from './useDefaultLocale' ;
13
14
import { isRTL } from './utils' ;
14
- import { Locale , useDefaultLocale } from './useDefaultLocale' ;
15
15
import React , { JSX , ReactNode , useContext } from 'react' ;
16
16
17
17
export interface I18nProviderProps {
@@ -23,36 +23,65 @@ export interface I18nProviderProps {
23
23
24
24
const I18nContext = React . createContext < Locale | null > ( null ) ;
25
25
26
+ interface I18nProviderWithLocaleProps extends I18nProviderProps {
27
+ locale : string
28
+ }
29
+
26
30
/**
27
- * Provides the locale for the application to all child components .
31
+ * Internal component that handles the case when locale is provided .
28
32
*/
29
- export function I18nProvider ( props : I18nProviderProps ) : JSX . Element {
30
- let { locale, children} = props ;
31
- let defaultLocale = useDefaultLocale ( ) ;
33
+ function I18nProviderWithLocale ( props : I18nProviderWithLocaleProps ) : JSX . Element {
34
+ let { locale, children} = props ;
35
+ let value : Locale = React . useMemo ( ( ) => ( {
36
+ locale,
37
+ direction : isRTL ( locale ) ? 'rtl' : 'ltr'
38
+ } ) , [ locale ] ) ;
39
+
40
+ return (
41
+ < I18nContext . Provider value = { value } >
42
+ { children }
43
+ </ I18nContext . Provider >
44
+ ) ;
45
+ }
32
46
33
- let value : Locale = React . useMemo ( ( ) => {
34
- if ( ! locale ) {
35
- return defaultLocale ;
36
- }
47
+ interface I18nProviderWithDefaultLocaleProps {
48
+ children : ReactNode
49
+ }
37
50
38
- return {
39
- locale,
40
- direction : isRTL ( locale ) ? 'rtl' : 'ltr'
41
- } ;
42
- } , [ defaultLocale , locale ] ) ;
51
+ /**
52
+ * Internal component that handles the case when no locale is provided.
53
+ */
54
+ function I18nProviderWithDefaultLocale ( props : I18nProviderWithDefaultLocaleProps ) : JSX . Element {
55
+ let { children} = props ;
56
+ let defaultLocale = useDefaultLocale ( ) ;
43
57
44
58
return (
45
- < I18nContext . Provider value = { value } >
59
+ < I18nContext . Provider value = { defaultLocale } >
46
60
{ children }
47
61
</ I18nContext . Provider >
48
62
) ;
49
63
}
50
64
65
+ /**
66
+ * Provides the locale for the application to all child components.
67
+ */
68
+ export function I18nProvider ( props : I18nProviderProps ) : JSX . Element {
69
+ let { locale, children} = props ;
70
+
71
+ // Conditionally render different components to avoid calling useDefaultLocale.
72
+ // This is necessary because useDefaultLocale triggers a re-render.
73
+ if ( locale ) {
74
+ return < I18nProviderWithLocale locale = { locale } children = { children } /> ;
75
+ }
76
+
77
+ return < I18nProviderWithDefaultLocale children = { children } /> ;
78
+ }
79
+
51
80
/**
52
81
* Returns the current locale and layout direction.
53
82
*/
54
83
export function useLocale ( ) : Locale {
55
- let defaultLocale = useDefaultLocale ( ) ;
84
+ let defaultLocale = getDefaultLocale ( ) ;
56
85
let context = useContext ( I18nContext ) ;
57
86
return context || defaultLocale ;
58
87
}
0 commit comments