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: stabilize t in app directory #1206

Merged
merged 1 commit into from
Apr 9, 2024
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
30 changes: 29 additions & 1 deletion __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { render, cleanup, fireEvent } from '@testing-library/react'
import I18nProvider from '../src/I18nProvider'
import useTranslation from '../src/useTranslation'
Expand Down Expand Up @@ -1549,5 +1549,33 @@ describe('useTranslation', () => {
const { container } = render(<Inner />)
expect(container.textContent).toContain(expected)
})

test('`t` is stable', () => {
const Inner = ({ effect }) => {
const { t } = useTranslation()

useEffect(() => {
const text = t('ns:interpolation', {
count: 3,
})
effect(text)
}, [effect, t])
}

globalThis.__NEXT_TRANSLATE__ = {
namespaces: {
ns: {
interpolation: 'There are {{count}} cats.',
},
},
lang: 'en',
config: {},
}

const effect = jest.fn()
const { rerender } = render(<Inner effect={effect} />)
rerender(<Inner effect={effect} />)
expect(effect).toBeCalledTimes(1)
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"scripts": {
"build": "yarn clean && cross-env NODE_ENV=production && yarn tsc",
"clean": "yarn clean:build && yarn clean:examples",
"clean:build": "del lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements AppDirI18nProvider* createTrans*",
"clean:build": "del lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements isServer AppDirI18nProvider* createTrans*",
"clean:examples": "del examples/**/.next examples/**/node_modules examples/**/yarn.lock",
"example": "yarn example:complex",
"example:basic": "yarn build && yarn --cwd examples/basic && yarn --cwd examples/basic dev",
Expand Down
21 changes: 14 additions & 7 deletions src/createTranslation.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useMemo } from 'react'
import isServer from './isServer'
import transCore from './transCore'
import wrapTWithDefaultNs from './wrapTWithDefaultNs'

Expand All @@ -6,12 +8,17 @@ export default function createTranslation(defaultNS?: string) {
const { lang, namespaces, config } = globalThis.__NEXT_TRANSLATE__ ?? {}
const localesToIgnore = config.localesToIgnore || ['default']
const ignoreLang = !lang || localesToIgnore.includes(lang)
const t = transCore({
config,
allNamespaces: namespaces,
pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
lang,
})
const getT = () => {
const t = transCore({
config,
allNamespaces: namespaces,
pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
lang,
})

return { t: wrapTWithDefaultNs(t, defaultNS), lang }
return wrapTWithDefaultNs(t, defaultNS)
}

const t = isServer() ? getT() : useMemo(getT, [defaultNS])
return { t, lang }
}
3 changes: 3 additions & 0 deletions src/isServer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isServer() {
return typeof window === 'undefined'
}
Loading