forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add use translation hook (refinedev#5733)
* feat(core): add useTranslation hook * feat(docs): add useTranslation doc * feat(examples): update examples * chore(core): update changeset * fix(docs): redirections * feat(docs): update guide and concept
- Loading branch information
1 parent
4ff40b5
commit 2b5ac6f
Showing
26 changed files
with
285 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
"@refinedev/core": patch | ||
--- | ||
|
||
feat: added `useTranslation` hook. It combines `useTranslate`, `useSetLocale` and `useGetLocale` hooks and returns `translate`, `changeLocale` and `getLocale` methods from that hooks for better developer experience. | ||
|
||
It returns all [`i18nProvider`](/docs/i18n/i18n-provider) methods in one hook. It can be used to translate texts, change the locale, and get the current locale in your own components. | ||
|
||
```tsx | ||
import { useTranslation } from "@refinedev/core"; | ||
|
||
export const MyComponent = () => { | ||
const { translate, getLocale, changeLocale } = useTranslation(); | ||
const currentLocale = getLocale(); | ||
|
||
return ( | ||
<div> | ||
<h1>{translate("languages")}</h1> | ||
<button | ||
onClick={() => changeLocale("en")} | ||
disabled={currentLocale === "en"} | ||
> | ||
English | ||
</button> | ||
<button | ||
onClick={() => changeLocale("de")} | ||
disabled={currentLocale === "de"} | ||
> | ||
German | ||
</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
Example of combining `useTranslation` with `useTranslate`, `useSetLocale` and `useGetLocale` hooks. | ||
|
||
```diff | ||
import { | ||
- useGetLocale, | ||
- useSetLocale, | ||
- useTranslate, | ||
+ useTranslation, | ||
} from "@refinedev/core"; | ||
|
||
export const MyComponent = () => { | ||
- const changeLocale = useSetLocale(); | ||
- const getLocale = useGetLocale(); | ||
- const translate = useTranslate(); | ||
|
||
+ const { translate, getLocale, changeLocale } = useTranslation(); | ||
|
||
return <div>{/* ... */}</div>; | ||
}; | ||
``` |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,84 @@ | ||
--- | ||
title: useTranslation | ||
--- | ||
|
||
The `useTranslation` hook, allows you to use call `translate`, `changeLocale`, and `getLocale` methods from the [`i18nProvider`](/docs/i18n/i18n-provider) that you provided. It can be used to translate texts, change the locale, and get the current locale in your own components. | ||
|
||
## Usage | ||
|
||
> This hook can only be used if [`i18nProvider`](/docs/i18n/i18n-provider) is provided. | ||
```tsx | ||
import { useTranslation } from "@refinedev/core"; | ||
|
||
export const MyComponent = () => { | ||
const { translate, getLocale, changeLocale } = useTranslation(); | ||
const currentLocale = getLocale(); | ||
|
||
return ( | ||
<div> | ||
<h1>{translate("languages")}</h1> | ||
<button | ||
onClick={() => changeLocale("en")} | ||
disabled={currentLocale === "en"} | ||
> | ||
English | ||
</button> | ||
<button | ||
onClick={() => changeLocale("de")} | ||
disabled={currentLocale === "de"} | ||
> | ||
German | ||
</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## translate | ||
|
||
If you need to translate the texts in your own components, you can use `translate` method. It calls the `translate` method from [`i18nProvider`](/docs/i18n/i18n-provider) under the hood. | ||
|
||
```tsx | ||
import { useTranslate } from "@refinedev/core"; | ||
|
||
export const MyComponent = () => { | ||
const { translate } = useTranslate(); | ||
|
||
return <button>{translate("my.translate.text")}</button>; | ||
}; | ||
``` | ||
|
||
## changeLocale | ||
|
||
If you need to change the locale at runtime, you can use the `changeLocale` method. It calls the `changeLocale` method from [`i18nProvider`](/docs/i18n/i18n-provider) under the hood. | ||
|
||
```tsx | ||
import { useSetLocale } from "@refinedev/core"; | ||
|
||
export const LanguageSwicher = () => { | ||
const { changeLocale } = useTranslation(); | ||
|
||
return ( | ||
<div> | ||
<span>Languages</span> | ||
<button onClick={() => changeLanguage("en")}>English</button> | ||
<button onClick={() => changeLanguage("es")}>Spanish</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## getLocale | ||
|
||
If you need to know the current locale, you can use the `getLocale` method. It calls the `getLocale` method from [`i18nProvider`](/docs/i18n/i18n-provider) under the hood. | ||
|
||
```tsx | ||
import { useSetLocale } from "@refinedev/core"; | ||
|
||
export const LanguageSwicher = () => { | ||
const { getLocale } = useTranslation(); | ||
|
||
return <h1>Current Locale: {getLocale()}</h1>; | ||
}; | ||
``` |
Oops, something went wrong.