Unverified Commit 4b2a8253 authored by Dustin Deus's avatar Dustin Deus Committed by GitHub

Fix with-i18n-rosetta example (#16023)

* fix

* fix unstable useeffect dep

* FIxed lint issue
Co-authored-by: 's avatarLuis Alvarez <luis@vercel.com>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 782d27e5
...@@ -14,28 +14,10 @@ export const I18nContext = createContext() ...@@ -14,28 +14,10 @@ export const I18nContext = createContext()
i18n.locale(defaultLanguage) i18n.locale(defaultLanguage)
export default function I18n({ children, locale, lngDict }) { export default function I18n({ children, locale, lngDict }) {
const [activeDict, setActiveDict] = useState(() => lngDict)
const activeLocaleRef = useRef(locale || defaultLanguage) const activeLocaleRef = useRef(locale || defaultLanguage)
const [, setTick] = useState(0) const [, setTick] = useState(0)
const firstRender = useRef(true) const firstRender = useRef(true)
// for initial SSR render
if (locale && firstRender.current === true) {
firstRender.current = false
i18n.locale(locale)
i18n.set(locale, activeDict)
}
useEffect(() => {
if (locale) {
i18n.locale(locale)
i18n.set(locale, activeDict)
activeLocaleRef.current = locale
// force rerender
setTick((tick) => tick + 1)
}
}, [locale, activeDict])
const i18nWrapper = { const i18nWrapper = {
activeLocale: activeLocaleRef.current, activeLocale: activeLocaleRef.current,
t: (...args) => i18n.t(...args), t: (...args) => i18n.t(...args),
...@@ -44,13 +26,26 @@ export default function I18n({ children, locale, lngDict }) { ...@@ -44,13 +26,26 @@ export default function I18n({ children, locale, lngDict }) {
activeLocaleRef.current = l activeLocaleRef.current = l
if (dict) { if (dict) {
i18n.set(l, dict) i18n.set(l, dict)
setActiveDict(dict)
} else {
setTick((tick) => tick + 1)
} }
// force rerender to update view
setTick((tick) => tick + 1)
}, },
} }
// for initial SSR render
if (locale && firstRender.current === true) {
firstRender.current = false
i18nWrapper.locale(locale, lngDict)
}
// when locale is updated
useEffect(() => {
if (locale) {
i18nWrapper.locale(locale, lngDict)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lngDict, locale])
return ( return (
<I18nContext.Provider value={i18nWrapper}>{children}</I18nContext.Provider> <I18nContext.Provider value={i18nWrapper}>{children}</I18nContext.Provider>
) )
......
...@@ -19,7 +19,7 @@ const HomePage = () => { ...@@ -19,7 +19,7 @@ const HomePage = () => {
<h2>{i18n.t('intro.text')}</h2> <h2>{i18n.t('intro.text')}</h2>
<h3>{i18n.t('intro.description')}</h3> <h3>{i18n.t('intro.description')}</h3>
<div>Current locale: {i18n.activeLocale}</div> <div>Current locale: {i18n.activeLocale}</div>
<Link href="/de"> <Link href="/[lng]" as="/de">
<a>Use client-side routing to change language to 'de'</a> <a>Use client-side routing to change language to 'de'</a>
</Link> </Link>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment