-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'aralroca:master' into master
- Loading branch information
Showing
10 changed files
with
626 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Type safety consuming translations (TypeScript only) | ||
|
||
To enable type safety consuming translations, you have to add this to the `next-translate.d.ts` file specifying the name and location of the namespaces: | ||
|
||
```ts | ||
import type { Paths, I18n, Translate } from 'next-translate' | ||
|
||
type Tail<T> = T extends [unknown, ...infer Rest] ? Rest : never; | ||
|
||
export interface TranslationsKeys { | ||
// Example with "common" and "home" namespaces in "en" (the default language): | ||
common: Paths<typeof import('./locales/en/common.json')> | ||
home: Paths<typeof import('./locales/en/home.json')> | ||
// Specify here all the namespaces you have... | ||
} | ||
|
||
export interface TypeSafeTranslate<Namespace extends keyof TranslationsKeys> | ||
extends Omit<I18n, 't'> { | ||
t: { | ||
( | ||
key: TranslationsKeys[Namespace], | ||
...rest: Tail<Parameters<Translate>> | ||
): string | ||
<T extends string>(template: TemplateStringsArray): string | ||
} | ||
} | ||
|
||
declare module 'next-translate/useTranslation' { | ||
export default function useTranslation< | ||
Namespace extends keyof TranslationsKeys | ||
>(namespace: Namespace): TypeSafeTranslate<Namespace> | ||
} | ||
``` | ||
Then type safety should work: | ||
<img width="472" alt="Screenshot 2023-07-17 at 19 17 13" src="https://github.com/aralroca/next-translate/assets/13313058/e9e505a7-4cc5-41e3-b2e4-b7f27fb2d181"> | ||
<img width="282" alt="Screenshot 2023-07-17 at 19 22 00" src="https://github.com/aralroca/next-translate/assets/13313058/616987b4-e49b-4cf2-b511-cdfaba57e1d2"> | ||
Reference: https://github.com/aralroca/next-translate/pull/1108 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Paths, I18n, Translate } from 'next-translate' | ||
|
||
export interface TranslationsKeys { | ||
common: Paths<typeof import('./locales/en/common.json')> | ||
home: Paths<typeof import('./locales/en/home.json')> | ||
} | ||
|
||
export interface TypeSafeTranslate<Namespace extends keyof TranslationsKeys> | ||
extends Omit<I18n, 't'> { | ||
t: { | ||
( | ||
key: TranslationsKeys[Namespace], | ||
...rest: Tail<Parameters<Translate>> | ||
): string | ||
<T extends string>(template: TemplateStringsArray): string | ||
} | ||
} | ||
|
||
declare module 'next-translate/useTranslation' { | ||
export default function useTranslation< | ||
Namespace extends keyof TranslationsKeys | ||
>(namespace: Namespace): TypeSafeTranslate<Namespace> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = !lang || localesToIgnore.includes(lang) | ||
const t = transCore({ | ||
config, | ||
allNamespaces: namespaces, | ||
pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang), | ||
lang, | ||
}) | ||
|
||
return { t: wrapTWithDefaultNs(t, defaultNS), lang } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.