Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with-i18n-rosetta example #16023

Merged
merged 4 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions examples/with-i18n-rosetta/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@ export const I18nContext = createContext()
i18n.locale(defaultLanguage)

export default function I18n({ children, locale, lngDict }) {
const [activeDict, setActiveDict] = useState(() => lngDict)
const activeLocaleRef = useRef(locale || defaultLanguage)
const [, setTick] = useState(0)
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 = {
activeLocale: activeLocaleRef.current,
t: (...args) => i18n.t(...args),
Expand All @@ -44,13 +26,26 @@ export default function I18n({ children, locale, lngDict }) {
activeLocaleRef.current = l
if (dict) {
i18n.set(l, dict)
setActiveDict(dict)
} else {
setTick((tick) => tick + 1)
}
// force rerender to update view
setTick((tick) => tick + 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work for me. I get an Unhandled Runtime Error on setTick((tick) => tick + 1) on loading:

Unhandled Runtime Error
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

},
}

// 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 (
<I18nContext.Provider value={i18nWrapper}>{children}</I18nContext.Provider>
)
Expand Down
2 changes: 1 addition & 1 deletion examples/with-i18n-rosetta/pages/[lng]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const HomePage = () => {
<h2>{i18n.t('intro.text')}</h2>
<h3>{i18n.t('intro.description')}</h3>
<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>
</Link>
</div>
Expand Down