1- import { ReactNode , useEffect } from 'react' ;
1+ import { ReactNode , useEffect , useState } from 'react' ;
22import { IntlProvider } from 'react-intl' ;
33
44import {
88import { useI18nConfig } from 'lib/hooks/session' ;
99import moment from 'lib/moment' ;
1010
11- import translations from '../../../../build/locales/locales.json ' ;
11+ import LoadingIndicator from '../core/LoadingIndicator ' ;
1212
1313interface I18nProviderProps {
1414 children : ReactNode ;
@@ -17,26 +17,61 @@ interface I18nProviderProps {
1717const getLocaleWithoutRegionCode = ( locale : string ) : string =>
1818 locale . toLowerCase ( ) . split ( / [ _ - ] + / ) [ 0 ] ;
1919
20- const getMessages = ( locale : string ) : Record < string , string > | undefined => {
21- const localeWithoutRegionCode = getLocaleWithoutRegionCode ( locale ) ;
22-
23- return localeWithoutRegionCode !== DEFAULT_LOCALE
24- ? translations [ localeWithoutRegionCode ] || translations [ locale ]
25- : undefined ;
26- } ;
27-
2820const I18nProvider = ( props : I18nProviderProps ) : JSX . Element => {
2921 const { locale, timeZone } = useI18nConfig ( ) ;
22+ const [ messages , setMessages ] = useState < Record < string , string > > ( ) ;
3023
3124 useEffect ( ( ) => {
3225 moment . tz . setDefault ( timeZone ?. trim ( ) || DEFAULT_TIME_ZONE ) ;
3326 } , [ timeZone ] ) ;
3427
28+ const localeWithoutRegionCode = getLocaleWithoutRegionCode ( locale ) ;
29+
30+ useEffect ( ( ) => {
31+ setMessages ( undefined ) ;
32+
33+ let ignore = false ;
34+
35+ ( async ( ) : Promise < void > => {
36+ let loadedMessages : Record < string , string > ;
37+
38+ try {
39+ loadedMessages = await import (
40+ /* webpackChunkName: "locale-[request]" */
41+ `../../../../compiled-locales/${ localeWithoutRegionCode } .json`
42+ ) ;
43+ } catch ( error ) {
44+ if (
45+ ! (
46+ error instanceof Error &&
47+ error . message . includes ( 'Cannot find module' )
48+ )
49+ )
50+ throw error ;
51+
52+ loadedMessages = await import (
53+ /* webpackChunkName: "locale-[request]" */
54+ `../../../../compiled-locales/${ DEFAULT_LOCALE } .json`
55+ ) ;
56+ }
57+
58+ if ( ignore ) return ;
59+
60+ setMessages ( loadedMessages ) ;
61+ } ) ( ) ;
62+
63+ return ( ) => {
64+ ignore = true ;
65+ } ;
66+ } , [ localeWithoutRegionCode ] ) ;
67+
68+ if ( ! messages ) return < LoadingIndicator /> ;
69+
3570 return (
3671 < IntlProvider
3772 defaultLocale = { DEFAULT_LOCALE }
3873 locale = { locale }
39- messages = { getMessages ( locale ) }
74+ messages = { messages }
4075 textComponent = "span"
4176 >
4277 { props . children }
0 commit comments