@@ -8,9 +8,9 @@ import Footer from './components/Footer.js';
88import GetStarted from './pages/GetStarted/get-started.js' ;
99import SupportUs from './pages/SupportUs/SupportUs.js' ;
1010import { createContext } from 'preact' ;
11- import { useLayoutEffect , useState } from 'preact/hooks' ;
11+ import { useLayoutEffect , useRef , useState } from 'preact/hooks' ;
1212import { default as i18next , changeLanguage } from 'i18next' ;
13- import { extractLocaleFromUrl , LOCALES , mapLocale } from './i18n' ;
13+ import { extractLocaleFromUrl , initTranslations , LOCALES , mapLocale } from './i18n' ;
1414import HttpApi from 'i18next-http-backend' ;
1515import { initReactI18next } from "react-i18next" ;
1616
@@ -43,29 +43,20 @@ export function App(props: {repoStargazersCount: number}) {
4343export function LocaleProvider ( { children } ) {
4444 const { path } = useLocation ( ) ;
4545 const localeId = mapLocale ( extractLocaleFromUrl ( path ) || navigator . language ) ;
46- const [ loaded , setLoaded ] = useState ( false ) ;
46+ const loadedRef = useRef ( false ) ;
4747
48- useLayoutEffect ( ( ) => {
49- i18next
50- . use ( HttpApi )
51- . use ( initReactI18next ) ;
52- i18next . init ( {
53- lng : localeId ,
54- fallbackLng : "en" ,
55- backend : {
56- loadPath : "/translations/{{lng}}/{{ns}}.json" ,
57- } ,
58- returnEmptyString : false
59- } ) . then ( ( ) => setLoaded ( true ) )
60- } , [ ] ) ;
48+ if ( ! loadedRef . current ) {
49+ initTranslations ( localeId ) ;
50+ loadedRef . current = true ;
51+ } else {
52+ changeLanguage ( localeId ) ;
53+ }
6154
6255 useLayoutEffect ( ( ) => {
63- if ( ! loaded ) return ;
64- changeLanguage ( localeId ) ;
6556 const correspondingLocale = LOCALES . find ( l => l . id === localeId ) ;
6657 document . documentElement . lang = localeId ;
6758 document . documentElement . dir = correspondingLocale ?. rtl ? "rtl" : "ltr" ;
68- } , [ loaded , localeId ] ) ;
59+ } , [ localeId ] ) ;
6960
7061 return (
7162 < LocaleContext . Provider value = { localeId } >
0 commit comments