From af31d89391e432a980e741dc2d3b86f1d30bd3e1 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Wed, 27 Sep 2023 16:42:22 +0200 Subject: [PATCH 01/23] feat: add createTranslation helper for app dir (#1150) --- README.md | 10 ++++++++++ package.json | 7 ++++--- src/createTranslation.tsx | 17 +++++++++++++++++ src/useTranslation.tsx | 18 ++---------------- 4 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 src/createTranslation.tsx diff --git a/README.md b/README.md index 3a2137f..d48503d 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ - [3. Configuration](#3-configuration) - [4. API](#4-api) - [useTranslation](#usetranslation) + - [createTranslation](#createtranslation) - [withTranslation](#withtranslation) - [Trans Component](#trans-component) - [DynamicNamespaces](#dynamicnamespaces) @@ -299,6 +300,15 @@ The `t` function: - **ns**: string - Namespace to use when none is embded in the `i18nKey`. - **Output**: string +### createTranslation + +Similar than `useTranslation` but without being a hook. This helper **only works** in **app dir**. + +```ts + const { t, lang } = createTranslation('ns1') // default namespace (optional) + const title = t('title') +``` + ### withTranslation **Size**: ~560b 📦 diff --git a/package.json b/package.json index a161493..719fce3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "2.5.3", + "version": "2.6.0", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ @@ -42,12 +42,13 @@ "useTranslation*", "setLanguage*", "index*", - "AppDirI18nProvider*" + "AppDirI18nProvider*", + "createTranslation*" ], "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 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", diff --git a/src/createTranslation.tsx b/src/createTranslation.tsx new file mode 100644 index 0000000..e50909d --- /dev/null +++ b/src/createTranslation.tsx @@ -0,0 +1,17 @@ +import transCore from './transCore' +import wrapTWithDefaultNs from './wrapTWithDefaultNs' + +// Only for App directory +export default function createTranslation(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, + }) + + return { t: wrapTWithDefaultNs(t, defaultNS), lang } +} diff --git a/src/useTranslation.tsx b/src/useTranslation.tsx index bd1cbba..33f98ac 100644 --- a/src/useTranslation.tsx +++ b/src/useTranslation.tsx @@ -2,7 +2,7 @@ import { useContext, useMemo } from 'react' import { I18n } from '.' import wrapTWithDefaultNs from './wrapTWithDefaultNs' import I18nContext from './context' -import transCore from './transCore' +import createTranslation from './createTranslation' function useTranslationInPages(defaultNS?: string): I18n { const ctx = useContext(I18nContext) @@ -15,22 +15,8 @@ function useTranslationInPages(defaultNS?: string): I18n { ) } -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, - }) - - return { t: wrapTWithDefaultNs(t, defaultNS), lang } -} - export default function useTranslation(defaultNS?: string): I18n { const appDir = globalThis.__NEXT_TRANSLATE__ - const useT = appDir?.config ? useTranslationAppDir : useTranslationInPages + const useT = appDir?.config ? createTranslation : useTranslationInPages return useT(defaultNS) } From 0765cc82b2ea830bd254582b75a6e18ca918fecb Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Sat, 14 Oct 2023 22:33:50 +0200 Subject: [PATCH 02/23] chore: update package.json version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 719fce3..646b02e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "2.6.0", + "version": "2.6.1", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ From 3fece41e6f24e1919892c5f1596f2981e3c1576c Mon Sep 17 00:00:00 2001 From: HardikBandhiya Date: Mon, 30 Oct 2023 16:21:40 +0530 Subject: [PATCH 03/23] Update README.md (#1165) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit changed logo of Twitter to New logo 𝕏 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d48503d..c663da7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ -follow on Twitter From 20c197551ae6eb15c997c07423ac072eea9ea58b Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Mon, 30 Oct 2023 11:52:13 +0100 Subject: [PATCH 04/23] docs: add BandhiyaHardik as a contributor for doc (#1167) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 701935e..7a197eb 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -521,6 +521,15 @@ "contributions": [ "maintenance" ] + }, + { + "login": "BandhiyaHardik", + "name": "HardikBandhiya", + "avatar_url": "https://avatars.githubusercontent.com/u/110784317?v=4", + "profile": "https://github.com/BandhiyaHardik", + "contributions": [ + "doc" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index c663da7..b7f49e6 100644 --- a/README.md +++ b/README.md @@ -1151,6 +1151,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Honza
Honza

🚧 + HardikBandhiya
HardikBandhiya

đź“– From 65bb290bf08076f5eb014e6e45e6fe04dad96d79 Mon Sep 17 00:00:00 2001 From: "Tim O. Peters" Date: Sat, 4 Nov 2023 13:20:00 +0100 Subject: [PATCH 05/23] fix: ignore empty lang value #1162 (#1163) --- src/createTranslation.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createTranslation.tsx b/src/createTranslation.tsx index e50909d..8509903 100644 --- a/src/createTranslation.tsx +++ b/src/createTranslation.tsx @@ -5,7 +5,7 @@ import wrapTWithDefaultNs from './wrapTWithDefaultNs' export default function createTranslation(defaultNS?: string) { const { lang, namespaces, config } = globalThis.__NEXT_TRANSLATE__ ?? {} const localesToIgnore = config.localesToIgnore || ['default'] - const ignoreLang = localesToIgnore.includes(lang) + const ignoreLang = !lang || localesToIgnore.includes(lang) const t = transCore({ config, allNamespaces: namespaces, From abc84aac164cc0f30128efbbdaa4ef6924b64cb9 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Sat, 4 Nov 2023 13:20:57 +0100 Subject: [PATCH 06/23] docs: add timotew as a contributor for code (#1169) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 7a197eb..e473a90 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -530,6 +530,15 @@ "contributions": [ "doc" ] + }, + { + "login": "timotew", + "name": "Tim O. Peters", + "avatar_url": "https://avatars.githubusercontent.com/u/12928383?v=4", + "profile": "https://www.linkedin.com/in/timotew/", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index b7f49e6..6a6e6c0 100644 --- a/README.md +++ b/README.md @@ -1152,6 +1152,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Honza
Honza

🚧 HardikBandhiya
HardikBandhiya

đź“– + Tim O. Peters
Tim O. Peters

đź’» From 2052d85da5d4d688a4ca097aec24c5c83faf1285 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Sat, 4 Nov 2023 13:27:16 +0100 Subject: [PATCH 07/23] fix(i18n-provider): fix locale to ignore detection (#1170) --- src/I18nProvider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/I18nProvider.tsx b/src/I18nProvider.tsx index cec17c0..8230eb3 100644 --- a/src/I18nProvider.tsx +++ b/src/I18nProvider.tsx @@ -24,7 +24,7 @@ export default function I18nProvider({ const lang = lng || parentLang || locale || defaultLocale || '' const config = { ...internal.config, ...newConfig } const localesToIgnore = config.localesToIgnore || ['default'] - const ignoreLang = localesToIgnore.includes(lang) + const ignoreLang = !lang || localesToIgnore.includes(lang) const pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang) const t = transCore({ config, allNamespaces, pluralRules, lang }) From 9915e3931e9e9d8d5ec39a7ba8ce4de9286f8e06 Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Sat, 4 Nov 2023 13:28:19 +0100 Subject: [PATCH 08/23] update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 646b02e..5f59b64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "2.6.1", + "version": "2.6.2", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ From 06f87ba3daf2e5333e64b3c353ca50bd7c02485a Mon Sep 17 00:00:00 2001 From: Li Ming Date: Wed, 29 Nov 2023 05:20:19 +0900 Subject: [PATCH 09/23] Update README.md (#1174) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a6e6c0..b57f08c 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ In order to do this we use a **webpack loader** that loads the necessary transla - **`getServerSideProps`**. This is the **default method for dynamic pages** like `[slug].js` or `[...catchall].js`. This is because for these pages it is necessary to define the `getStaticPaths` and there is no knowledge of how the slugs should be for each locale. Likewise, how is it by default, only that you write the getStaticPaths then it will already use the getStaticProps to load the translations. - **`getInitialProps`**. This is the **default method for these pages that use a HoC**. This is in order to avoid conflicts because HoC could overwrite a `getInitialProps`. -This **whole process is transparent**, so in your pages you can directly consume the `useTranslate` hook to use the namespaces, and you don't need to do anything else. +This **whole process is transparent**, so in your pages you can directly consume the `useTranslation` hook to use the namespaces, and you don't need to do anything else. If for some reason you use a `getInitialProps` in your `_app.js` file, then the translations will only be loaded into your `getInitialProps` from `_app.js`. We recommend that for optimization reasons you don't use this approach unless it is absolutely necessary. From bf53b1c1ed8b92fb0822482b640e34ec37d9d8d0 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:20:32 +0100 Subject: [PATCH 10/23] docs: add hydRAnger as a contributor for doc (#1175) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index e473a90..117b3b8 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -539,6 +539,15 @@ "contributions": [ "code" ] + }, + { + "login": "hydRAnger", + "name": "Li Ming", + "avatar_url": "https://avatars.githubusercontent.com/u/1228449?v=4", + "profile": "https://github.com/hydRAnger", + "contributions": [ + "doc" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index b57f08c..fa07a7c 100644 --- a/README.md +++ b/README.md @@ -1153,6 +1153,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Honza
Honza

🚧 HardikBandhiya
HardikBandhiya

đź“– Tim O. Peters
Tim O. Peters

đź’» + Li Ming
Li Ming

📖 From c21e733d5afbfd1c701464c9c6109b82cc47043e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Hern=C3=A1ndez?= <86410308+acidfernando@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:47:24 +0000 Subject: [PATCH 11/23] feat: add support for arrays and pluralization to Trans component (#1179) --- __tests__/Trans.test.js | 69 +++++++++++++++++++++++++++++++++++++++++ src/Trans.tsx | 13 ++++++-- src/index.tsx | 1 + src/transCore.tsx | 30 +++++++++++++----- 4 files changed, 104 insertions(+), 9 deletions(-) diff --git a/__tests__/Trans.test.js b/__tests__/Trans.test.js index e39f6ee..c217497 100644 --- a/__tests__/Trans.test.js +++ b/__tests__/Trans.test.js @@ -49,6 +49,75 @@ describe('Trans', () => { expect(container.textContent).toContain(expected) }) + test('should work with arrays', () => { + const i18nKey = 'ns:parent.child' + const expectedFirstElement = 'First element 42' + const expectedSecondElement = 'Second element 42' + const withSingular = { + parent: { + child: [ + '<0>First element {{num}}', + '<0>Second element {{num}}', + ], + }, + } + const { container } = render( + ]} + /> + ) + expect(container.innerHTML).toContain(expectedFirstElement) + expect(container.innerHTML).toContain(expectedSecondElement) + }) + + test('should work with arrays and singulars', () => { + const i18nKey = 'ns:withsingular' + const expected = 'The number is one' + const withSingular = { + withsingular_0: ['<0>The number is ZERO!'], + withsingular_one: ['<0>The number is one'], + withsingular_other: ['<0>The number is plural'], + } + + const { container } = render( + ]} + /> + ) + + expect(container.innerHTML).toContain(expected) + }) + + test('should work with arrays and plurals', () => { + const i18nKey = 'ns:withsingular' + const expected = 'The number is plural' + const withSingular = { + withsingular: ['<0>First is not zero'], + withsingular_0: ['<0>The number is ZERO!'], + withsingular_other: ['<0>The number is plural'], + } + + const { container } = render( + ]} + /> + ) + + expect(container.innerHTML).toContain(expected) + }) + test('should work with nested keys and custom keySeparator', () => { const i18nKey = 'ns:parent_child' const expected = 'The number is 42' diff --git a/src/Trans.tsx b/src/Trans.tsx index 4d6bbe4..077e7d8 100644 --- a/src/Trans.tsx +++ b/src/Trans.tsx @@ -16,6 +16,7 @@ export default function Trans({ fallback, defaultTrans, ns, + returnObjects, }: TransProps): any { const { t, lang } = useTranslation(ns) @@ -23,11 +24,19 @@ export default function Trans({ * Memoize the transformation */ const result = useMemo(() => { - const text = t(i18nKey, values, { fallback, default: defaultTrans }) + const text = t(i18nKey, values, { + fallback, + default: defaultTrans, + returnObjects, + }) if (!text) return text - if (!components || components.length === 0) return text + if (!components || components.length === 0) + return Array.isArray(text) ? text.map((item) => item) : text + + if (Array.isArray(text)) + return text.map((item) => formatElements(item, components)) return formatElements(text, components) }, [i18nKey, values, components, lang]) as string diff --git a/src/index.tsx b/src/index.tsx index 52ee3c8..fa4bd7c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -41,6 +41,7 @@ export interface TransProps { fallback?: string | string[] defaultTrans?: string ns?: string + returnObjects?: boolean } export type PageValue = string[] | ((context: object) => string[]) diff --git a/src/transCore.tsx b/src/transCore.tsx index 3f5a40a..9f06cdd 100644 --- a/src/transCore.tsx +++ b/src/transCore.tsx @@ -65,7 +65,14 @@ export default function transCore({ ) const dic = (namespace && allNamespaces[namespace]) || {} - const keyWithPlural = plural(pluralRules, dic, i18nKey, config, query) + const keyWithPlural = plural( + pluralRules, + dic, + i18nKey, + config, + query, + options + ) const dicValue = getDicValue(dic, keyWithPlural, config, options) const value = typeof dicValue === 'object' @@ -157,11 +164,14 @@ function getDicValue( if ( typeof value === 'string' || - ((value as unknown) instanceof Object && options.returnObjects) + ((value as unknown) instanceof Object && + options.returnObjects && + Object.keys(value).length > 0) ) { return value } + if (Array.isArray(value) && options.returnObjects) return value return undefined } @@ -173,23 +183,29 @@ function plural( dic: I18nDictionary, key: string, config: I18nConfig, - query?: TranslationQuery | null + query?: TranslationQuery | null, + options?: { + returnObjects?: boolean + fallback?: string | string[] + } ): string { if (!query || typeof query.count !== 'number') return key const numKey = `${key}_${query.count}` - if (getDicValue(dic, numKey, config) !== undefined) return numKey + if (getDicValue(dic, numKey, config, options) !== undefined) return numKey const pluralKey = `${key}_${pluralRules.select(query.count)}` - if (getDicValue(dic, pluralKey, config) !== undefined) { + if (getDicValue(dic, pluralKey, config, options) !== undefined) { return pluralKey } const nestedNumKey = `${key}.${query.count}` - if (getDicValue(dic, nestedNumKey, config) !== undefined) return nestedNumKey + if (getDicValue(dic, nestedNumKey, config, options) !== undefined) + return nestedNumKey const nestedKey = `${key}.${pluralRules.select(query.count)}` - if (getDicValue(dic, nestedKey, config) !== undefined) return nestedKey + if (getDicValue(dic, nestedKey, config, options) !== undefined) + return nestedKey return key } From b058cb93c052f01e3d08c2cd9745cb283d6c3816 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Fri, 12 Jan 2024 14:48:07 +0100 Subject: [PATCH 12/23] docs: add acidfernando as a contributor for code (#1180) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 117b3b8..06ff57f 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -548,6 +548,15 @@ "contributions": [ "doc" ] + }, + { + "login": "acidfernando", + "name": "Fernando García Hernández", + "avatar_url": "https://avatars.githubusercontent.com/u/86410308?v=4", + "profile": "https://github.com/acidfernando", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index fa07a7c..cd64845 100644 --- a/README.md +++ b/README.md @@ -1154,6 +1154,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d HardikBandhiya
HardikBandhiya

đź“– Tim O. Peters
Tim O. Peters

đź’» Li Ming
Li Ming

📖 + Fernando García Hernández
Fernando García Hernández

đź’» From fed0091f993f4ebb3747f428b54bb4d0ccd5c171 Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Fri, 12 Jan 2024 14:50:23 +0100 Subject: [PATCH 13/23] chore: update package.json version --- README.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd64845..a43c55a 100644 --- a/README.md +++ b/README.md @@ -382,6 +382,7 @@ Or using `components` prop as a object: - `fallback` - string | string[] - Optional. Fallback i18nKey if the i18nKey doesn't match. - `defaultTrans` - string - Default translation for the key. If fallback keys are used, it will be used only after exhausting all the fallbacks. - `ns` - Namespace to use when none is embedded in `i18nKey` + - `returnObjects` - boolean - Get part of the JSON with all the translations. [See more](#7-nested-translations). In cases where we require the functionality of the `Trans` component, but need a **string** to be interpolated, rather than the output of the `t(props.i18nKey)` function, there is also a `TransText` component, which takes a `text` prop instead of `i18nKey`. diff --git a/package.json b/package.json index 5f59b64..72a58f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "2.6.2", + "version": "2.7.0-canary.1", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ From 9e26a8cec045eabe9f95ecaeada546304678d0f4 Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Mon, 22 Jan 2024 16:30:14 +0100 Subject: [PATCH 14/23] fix: fix useContext error + update example (#1182) --- examples/basic/package.json | 4 ++-- examples/complex/package.json | 18 ++++++++-------- examples/with-app-directory/package.json | 18 ++++++++-------- .../with-app-directory/src/app/layout.tsx | 21 +++++++++++++++++++ examples/without-loader/package.json | 4 ++-- src/context.tsx | 6 +++--- 6 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 examples/with-app-directory/src/app/layout.tsx diff --git a/examples/basic/package.json b/examples/basic/package.json index c68abaf..b5789d1 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -8,12 +8,12 @@ "build": "next build" }, "dependencies": { - "next": "13.4.7", + "next": "14.1.0", "next-translate": "link:../../", "react": "link:../../node_modules/react", "react-dom": "link:../../node_modules/react-dom" }, "devDependencies": { - "next-translate-plugin": "2.4.4" + "next-translate-plugin": "2.6.2" } } diff --git a/examples/complex/package.json b/examples/complex/package.json index d883e8c..90e93ca 100644 --- a/examples/complex/package.json +++ b/examples/complex/package.json @@ -9,20 +9,20 @@ "analyze": "ANALYZE=true yarn build" }, "dependencies": { - "@mdx-js/loader": "2.3.0", - "@mdx-js/react": "2.3.0", - "@next/mdx": "13.4.7", - "next": "13.4.7", + "@mdx-js/loader": "3.0.0", + "@mdx-js/react": "3.0.0", + "@next/mdx": "14.1.0", + "next": "14.1.0", "next-translate": "link:../../", "react": "link:../../node_modules/react", "react-dom": "link:../../node_modules/react-dom" }, "devDependencies": { - "@next/bundle-analyzer": "13.4.7", - "@types/node": "20.3.1", - "@types/react": "18.2.13", - "next-translate-plugin": "2.4.4", - "typescript": "5.1.3" + "@next/bundle-analyzer": "14.1.0", + "@types/node": "20.11.5", + "@types/react": "18.2.48", + "next-translate-plugin": "2.6.2", + "typescript": "5.3.3" }, "resolutions": { "webpack": "5.11.1" diff --git a/examples/with-app-directory/package.json b/examples/with-app-directory/package.json index bbf52f4..d178ce2 100644 --- a/examples/with-app-directory/package.json +++ b/examples/with-app-directory/package.json @@ -10,20 +10,20 @@ "analyze": "ANALYZE=true yarn build" }, "dependencies": { - "@mdx-js/loader": "2.3.0", - "@mdx-js/react": "2.3.0", - "@next/mdx": "13.4.7", - "next": "13.4.7", + "@mdx-js/loader": "3.0.0", + "@mdx-js/react": "3.0.0", + "@next/mdx": "14.1.0", + "next": "14.1.0", "next-translate": "link:../../", "react": "link:../../node_modules/react", "react-dom": "link:../../node_modules/react-dom" }, "devDependencies": { - "@next/bundle-analyzer": "13.4.6", - "@types/node": "20.3.1", - "@types/react": "18.2.12", - "next-translate-plugin": "2.4.4", - "typescript": "5.1.3" + "@next/bundle-analyzer": "14.1.0", + "@types/node": "20.11.5", + "@types/react": "18.2.48", + "next-translate-plugin": "2.6.2", + "typescript": "5.3.3" }, "resolutions": { "webpack": "5.11.1" diff --git a/examples/with-app-directory/src/app/layout.tsx b/examples/with-app-directory/src/app/layout.tsx new file mode 100644 index 0000000..5bb08f6 --- /dev/null +++ b/examples/with-app-directory/src/app/layout.tsx @@ -0,0 +1,21 @@ +import useTranslation from 'next-translate/useTranslation' +import i18n from '../../i18n' +import { redirect } from 'next/navigation' + +export default function RootLayout({ + children, +}: { + children: React.ReactNode +}) { + const { lang } = useTranslation('common') + + // Redirect to default locale if lang is not supported. /second-page -> /en/second-page + if (!i18n.locales.includes(lang)) redirect(`/${i18n.defaultLocale}/${lang}`) + + return ( + + + {children} + + ) +} diff --git a/examples/without-loader/package.json b/examples/without-loader/package.json index 0809478..b5789d1 100644 --- a/examples/without-loader/package.json +++ b/examples/without-loader/package.json @@ -8,12 +8,12 @@ "build": "next build" }, "dependencies": { - "next": "13.4.7", + "next": "14.1.0", "next-translate": "link:../../", "react": "link:../../node_modules/react", "react-dom": "link:../../node_modules/react-dom" }, "devDependencies": { - "next-translate-plugin": "2.4.0" + "next-translate-plugin": "2.6.2" } } diff --git a/src/context.tsx b/src/context.tsx index 7c6c131..5a34a73 100644 --- a/src/context.tsx +++ b/src/context.tsx @@ -1,13 +1,13 @@ -import { createContext } from 'react' import { I18n } from '.' +import React from 'react' let context // For serverComponents (app-dir), the context cannot be used and // this makes that all the imports to here don't break the build. // The use of this context is inside each util, depending pages-dir or app-dir. -if (typeof createContext === 'function') { - context = createContext({ +if (typeof React.createContext === 'function') { + context = React.createContext({ t: (k) => (Array.isArray(k) ? k[0] : k), lang: '', }) From 4f74c0d52d5cc95a92d0923bb072262dce1b215a Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Mon, 22 Jan 2024 16:34:45 +0100 Subject: [PATCH 15/23] chore: update package.json version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 72a58f0..4b8dc89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "2.7.0-canary.1", + "version": "3.0.0-canary.1", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [ From b63006fdaaecbffcf5d88d2e1898f555f63b0bc7 Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Fri, 26 Jan 2024 01:12:03 +0100 Subject: [PATCH 16/23] Add type safety for getT function (#1184) * Update type-safety.md * Update next-translate.d.ts with new TranslateFunction interface * Refactor translation types in next-translate.d.ts * Refactor type safety in TranslateFunction --- docs/type-safety.md | 30 +++++++++++------ .../with-app-directory/next-translate.d.ts | 32 +++++++++++++------ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/docs/type-safety.md b/docs/type-safety.md index ea9daf0..43532dd 100644 --- a/docs/type-safety.md +++ b/docs/type-safety.md @@ -14,22 +14,32 @@ export interface TranslationsKeys { // Specify here all the namespaces you have... } -export interface TypeSafeTranslate +type TranslationNamespace = keyof TranslationsKeys; + +export interface TranslateFunction { + ( + key: TranslationsKeys[Namespace], + ...rest: Tail> + ): string + (template: TemplateStringsArray): string +}; + +export interface TypeSafeTranslate extends Omit { - t: { - ( - key: TranslationsKeys[Namespace], - ...rest: Tail> - ): string - (template: TemplateStringsArray): string - } + t: TranslateFunction } declare module 'next-translate/useTranslation' { export default function useTranslation< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(namespace: Namespace): TypeSafeTranslate } + +declare module 'next-translate/getT' { + export default function getT< + Namespace extends TranslationNamespace + >(locale?: string, namespace: Namespace): Promise> +} ``` Then type safety should work: @@ -38,4 +48,4 @@ Then type safety should work: Screenshot 2023-07-17 at 19 22 00 -Reference: https://github.com/aralroca/next-translate/pull/1108 +Reference: diff --git a/examples/with-app-directory/next-translate.d.ts b/examples/with-app-directory/next-translate.d.ts index ea3fac6..ed1185a 100644 --- a/examples/with-app-directory/next-translate.d.ts +++ b/examples/with-app-directory/next-translate.d.ts @@ -1,23 +1,37 @@ import type { Paths, I18n, Translate } from 'next-translate' +type Tail = T extends [unknown, ...infer Rest] ? Rest : never; + export interface TranslationsKeys { + // Example with "common" and "home" namespaces in "en" (the default language): common: Paths home: Paths + // Specify here all the namespaces you have... } -export interface TypeSafeTranslate +type TranslationNamespace = keyof TranslationsKeys; + +export interface TranslateFunction { + ( + key: TranslationsKeys[Namespace], + ...rest: Tail> + ): string + (template: TemplateStringsArray): string +}; + +export interface TypeSafeTranslate extends Omit { - t: { - ( - key: TranslationsKeys[Namespace], - ...rest: Tail> - ): string - (template: TemplateStringsArray): string - } + t: TranslateFunction } declare module 'next-translate/useTranslation' { export default function useTranslation< - Namespace extends keyof TranslationsKeys + Namespace extends TranslationNamespace >(namespace: Namespace): TypeSafeTranslate } + +declare module 'next-translate/getT' { + export default function getT< + Namespace extends TranslationNamespace + >(locale?: string, namespace: Namespace): Promise> +} From df541f79712948153115e8413440539457fb16ed Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Fri, 26 Jan 2024 01:14:12 +0100 Subject: [PATCH 17/23] docs: add please as a contributor for code (#1186) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 06ff57f..0f8e531 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -557,6 +557,15 @@ "contributions": [ "code" ] + }, + { + "login": "please", + "name": "please", + "avatar_url": "https://avatars.githubusercontent.com/u/2391996?v=4", + "profile": "https://github.com/please", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index a43c55a..0a89018 100644 --- a/README.md +++ b/README.md @@ -1156,6 +1156,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Tim O. Peters
Tim O. Peters

đź’» Li Ming
Li Ming

📖 Fernando García Hernández
Fernando García Hernández

đź’» + please
please

đź’» From 9a96eac95734eb15e918e526b607708ad2fc9f2a Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Sat, 27 Jan 2024 00:20:08 +0100 Subject: [PATCH 18/23] remove invalid entry (#1188) * Update .all-contributorsrc * Update README.md --- .all-contributorsrc | 9 --------- README.md | 1 - 2 files changed, 10 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 0f8e531..06ff57f 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -557,15 +557,6 @@ "contributions": [ "code" ] - }, - { - "login": "please", - "name": "please", - "avatar_url": "https://avatars.githubusercontent.com/u/2391996?v=4", - "profile": "https://github.com/please", - "contributions": [ - "code" - ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index 0a89018..a43c55a 100644 --- a/README.md +++ b/README.md @@ -1156,7 +1156,6 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Tim O. Peters
Tim O. Peters

đź’» Li Ming
Li Ming

📖 Fernando García Hernández
Fernando García Hernández

đź’» - please
please

đź’» From 4b5b576115d6b9da26fc006091c72c4f8953932e Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:15:31 +0100 Subject: [PATCH 19/23] docs: add hichemfantar as a contributor for code (#1187) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> Co-authored-by: Aral Roca Gomez --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 06ff57f..08a469c 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -557,6 +557,15 @@ "contributions": [ "code" ] + }, + { + "login": "hichemfantar", + "name": "Hichem Fantar", + "avatar_url": "https://avatars.githubusercontent.com/u/34947993?v=4", + "profile": "https://www.hichemfantar.com/", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index a43c55a..b23d702 100644 --- a/README.md +++ b/README.md @@ -1156,6 +1156,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Tim O. Peters
Tim O. Peters

đź’» Li Ming
Li Ming

📖 Fernando García Hernández
Fernando García Hernández

đź’» + Hichem Fantar
Hichem Fantar

đź’» From caa3e753107c7fb462eda8385395324aafe3685b Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Mon, 26 Feb 2024 12:49:42 +0100 Subject: [PATCH 20/23] docs: fix mistake in docs (#1193) Fixes https://github.com/aralroca/next-translate/issues/1192 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b23d702..3674d3e 100644 --- a/README.md +++ b/README.md @@ -780,7 +780,7 @@ In Trans Component: ## 9. Formatter -You can format params using the `interpolation.formatter` config function. +You can format params using the `interpolation.format` config function. in `i18n.js`: From 7b50ec5346cc7ea6bd6772b61c29af8cf08a610d Mon Sep 17 00:00:00 2001 From: Arseny Garelyshev Date: Tue, 5 Mar 2024 17:56:35 +0100 Subject: [PATCH 21/23] refactor: provide better type for config.interpolation.format (#1194) --- src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index fa4bd7c..a0cbdce 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -74,7 +74,7 @@ export interface I18nConfig extends NextI18nConfig { revalidate?: number pagesInDir?: string interpolation?: { - format?: Function + format?: (value: TranslationQuery[string], format: any, lang: string | undefined) => string prefix?: string suffix?: string } From 6be00adc0cbaac2f5fc2dc124501d7771d2653fd Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Thu, 21 Mar 2024 10:15:38 +0100 Subject: [PATCH 22/23] docs: add huseyinonalcom as a contributor for code (#1200) * docs: update README.md [skip ci] * docs: update .all-contributorsrc [skip ci] --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 9 +++++++++ README.md | 1 + 2 files changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 08a469c..771c139 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -566,6 +566,15 @@ "contributions": [ "code" ] + }, + { + "login": "huseyinonalcom", + "name": "Huseyin Onal", + "avatar_url": "https://avatars.githubusercontent.com/u/65453275?v=4", + "profile": "https://github.com/huseyinonalcom", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index 3674d3e..8fcc1f8 100644 --- a/README.md +++ b/README.md @@ -1157,6 +1157,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Li Ming
Li Ming

📖 Fernando García Hernández
Fernando García Hernández

đź’» Hichem Fantar
Hichem Fantar

đź’» + Huseyin Onal
Huseyin Onal

đź’» From 14bb2e577c8fff191dde25272b19a93db42d64d1 Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Mon, 22 Jan 2024 18:25:57 +0100 Subject: [PATCH 23/23] chore: update package.json version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4b8dc89..bd8e084 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "3.0.0-canary.1", + "version": "3.0.0-canary.2", "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.", "license": "MIT", "keywords": [