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

feat: add createTranslation helper for app dir #1150

Merged
merged 1 commit into from
Sep 27, 2023
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [3. Configuration](#3-configuration)
- [4. API](#4-api)
- [useTranslation](#usetranslation)
- [createTranslation](#createtranslation)
- [withTranslation](#withtranslation)
- [Trans Component](#trans-component)
- [DynamicNamespaces](#dynamicnamespaces)
Expand Down Expand Up @@ -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 📦
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions src/createTranslation.tsx
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 = localesToIgnore.includes(lang)
const t = transCore({
config,
allNamespaces: namespaces,
pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
lang,
})

return { t: wrapTWithDefaultNs(t, defaultNS), lang }
}
18 changes: 2 additions & 16 deletions src/useTranslation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Loading