Skip to content

Commit

Permalink
fix: stabilize t in app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jessemartin committed Apr 2, 2024
1 parent 335c039 commit 3e96897
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
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 @@ -47,7 +47,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*",
"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*",
"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
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'
}
19 changes: 12 additions & 7 deletions src/useTranslation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useContext, useMemo } from 'react'
import { I18n } from '.'
import wrapTWithDefaultNs from './wrapTWithDefaultNs'
import I18nContext from './context'
import isServer from './isServer'
import transCore from './transCore'

function useTranslationInPages(defaultNS?: string): I18n {
Expand All @@ -19,14 +20,18 @@ function useTranslationAppDir(defaultNS?: string) {
const { lang, namespaces, config } = globalThis.__NEXT_TRANSLATE__ ?? {}
const localesToIgnore = config.localesToIgnore || ['default']
const ignoreLang = 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 }
}

export default function useTranslation(defaultNS?: string): I18n {
Expand Down

0 comments on commit 3e96897

Please sign in to comment.