Skip to content

Commit

Permalink
Update package.json version (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Jun 19, 2023
1 parent c64a787 commit 1a4ad01
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 71 deletions.
9 changes: 5 additions & 4 deletions __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react'
import { render, cleanup, fireEvent } from '@testing-library/react'
import I18nProvider from '../src/I18nProvider'
import useTranslation from '../src/useTranslation'
import _store from '../src/_store'

const Inner = ({ i18nKey, query }) => {
const { t } = useTranslation()
Expand Down Expand Up @@ -1525,7 +1526,7 @@ describe('useTranslation', () => {
})

describe('Next.js 13 app-dir', () => {
test('should work without context (with globalThis.__NEXT_TRANSLATE__)', () => {
test('should work without context (with _store)', () => {
const Inner = () => {
const { t } = useTranslation()
const text = t('ns:interpolation', {
Expand All @@ -1536,15 +1537,15 @@ describe('useTranslation', () => {

const expected = 'There are 3 cats.'

globalThis.__NEXT_TRANSLATE__ = {
_store.set({
namespaces: {
ns: {
interpolation: 'There are {{count}} cats.',
},
},
lang: 'en',
}
globalThis.i18nConfig = {}
config: {},
})

const { container } = render(<Inner />)
expect(container.textContent).toContain(expected)
Expand Down
2 changes: 1 addition & 1 deletion examples/with-app-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@next/bundle-analyzer": "13.4.6",
"@types/node": "20.3.1",
"@types/react": "18.2.12",
"next-translate-plugin": "2.3.0-canary.6",
"next-translate-plugin": "link:../../../next-translate-plugin",
"typescript": "5.1.3"
},
"resolutions": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
"useTranslation*",
"setLanguage*",
"index*",
"app-dir*"
"_store*"
],
"scripts": {
"build": "yarn clean && cross-env NODE_ENV=production && yarn tsc",
"clean": "yarn clean:build && yarn clean:examples",
"clean:build": "rm -rf lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements app-dir*",
"clean:build": "rm -rf lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements _store*",
"clean:examples": "rm -rf examples/**/.next && rm -rf examples/**/node_modules && rm -rf examples/**/yarn.lock",
"example": "yarn example:complex",
"example:basic": "yarn build && yarn --cwd examples/basic && yarn --cwd examples/basic dev",
Expand Down
18 changes: 18 additions & 0 deletions src/_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DataForStoreType } from '.'

let data: DataForStoreType

export default {
set(storeData: DataForStoreType) {
data = {
...storeData,
namespaces: {
...(data?.namespaces || {}),
...(storeData.namespaces || {}),
},
}
},
get(): DataForStoreType {
return data
},
}
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export interface TranslationQuery {
[name: string]: any
}

export type DataForStoreType = {
lang: string
namespaces: Record<string, I18nDictionary>
config: LoaderConfig
}

export type Translate = <T extends unknown = string>(
i18nKey: string | TemplateStringsArray,
query?: TranslationQuery | null,
Expand Down
11 changes: 6 additions & 5 deletions src/useTranslation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useContext, useMemo } from 'react'
import { I18n } from '.'
import store from './_store'
import wrapTWithDefaultNs from './wrapTWithDefaultNs'
import I18nContext from './context'
import transCore from './transCore'
Expand All @@ -16,11 +17,11 @@ function useTranslationInPages(defaultNS?: string): I18n {
}

function useTranslationAppDir(defaultNS?: string) {
const { lang, namespaces } = globalThis.__NEXT_TRANSLATE__ || {}
const localesToIgnore = globalThis.i18nConfig.localesToIgnore || ['default']
const { lang, namespaces, config } = store.get()
const localesToIgnore = config.localesToIgnore || ['default']
const ignoreLang = localesToIgnore.includes(lang)
const t = transCore({
config: globalThis.i18nConfig,
config,
allNamespaces: namespaces,
pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
lang,
Expand All @@ -30,7 +31,7 @@ function useTranslationAppDir(defaultNS?: string) {
}

export default function useTranslation(defaultNS?: string): I18n {
const isAppDir = !!globalThis.__NEXT_TRANSLATE__
const useT = isAppDir ? useTranslationAppDir : useTranslationInPages
const appDir = store.get()
const useT = appDir?.config ? useTranslationAppDir : useTranslationInPages
return useT(defaultNS)
}
59 changes: 0 additions & 59 deletions src/withTranslationClientComponent.tsx

This file was deleted.

0 comments on commit 1a4ad01

Please sign in to comment.