This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from LedgerHQ/support/i18n
Support/i18n
- Loading branch information
Showing
19 changed files
with
881 additions
and
626 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
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
File renamed without changes.
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
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,18 @@ | ||
"use client"; | ||
|
||
import { useI18n } from "@/i18n/client"; | ||
|
||
const LocaleSelector = () => { | ||
const { locale, changeLocale, t } = useI18n(); | ||
|
||
return ( | ||
<> | ||
<button onClick={() => (locale === "en" ? changeLocale("fr") : changeLocale("en"))}> | ||
Click me to change the current locale to: {locale === "en" ? "fr" : "en"} | ||
<div>{t("welcome")}</div> | ||
</button> | ||
</> | ||
); | ||
}; | ||
|
||
export default LocaleSelector; |
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,27 @@ | ||
# Internationalization | ||
|
||
> In order to use internationalization we are using the [`next-international`](https://github.com/QuiiBz/next-international) library. | ||
## Get started | ||
|
||
### Locales | ||
|
||
The folder `locales` at the root of this directory holds all the translation dictionnaries for each supported locales (e.g "en", "fr"). | ||
|
||
### Config | ||
|
||
The `config` file holds the necessary information regarding the supported locales at the code level, it also provides some typings and export the necessary config object for `next-international`. | ||
|
||
### Client | ||
|
||
The `client` file expose 2 elements made for client components. In first place it exports a `I18nProvider` wrapper that is to be used at the top level of our app, here in `app/[locale]/layout.tsx`. Last but not least it also exports a hook that handle the logic behind `i18n`. Once wrapped, you can call `useI18n` in your client components. | ||
|
||
> Note: One wrapper at the top level of your client components only. | ||
### Server | ||
|
||
The `server` file expose a single `useI18n` hook made for server components. You can call it straight away from any server components. | ||
|
||
### Middleware | ||
|
||
The `middleware` file export a `I18nMiddleware` object that feeds NextJs middleware system. |
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,30 @@ | ||
"use client"; | ||
|
||
import { LocalesI18nConfig } from "@/i18n/config"; | ||
import { createI18nClient } from "next-international/client"; | ||
|
||
/** | ||
* Create i18n client. | ||
*/ | ||
const I18nClient = createI18nClient(LocalesI18nConfig); | ||
|
||
/** | ||
* Client component wrapper for the app. | ||
*/ | ||
export function I18nProvider({ children }: { children?: React.ReactNode }) { | ||
return <I18nClient.I18nProviderClient>{children}</I18nClient.I18nProviderClient>; | ||
} | ||
|
||
/** | ||
* i18n client hook. | ||
* | ||
* @returns the necessary functions and variables to use i18n on client components. | ||
*/ | ||
export const useI18n = () => { | ||
const t = I18nClient.useI18n(); | ||
const changeLocale = I18nClient.useChangeLocale(); | ||
|
||
const locale = I18nClient.useCurrentLocale(); | ||
|
||
return { t, changeLocale, locale }; | ||
}; |
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,29 @@ | ||
/** | ||
* @dev Add locales here. | ||
*/ | ||
export const LOCALES = ["en", "fr"] as const; | ||
|
||
export type Locale = (typeof LOCALES)[number]; | ||
export type LocaleInfo = { file: File }; | ||
export type LocaleMap<T = string> = { [key in Locale]: T }; | ||
|
||
export const Locales = { | ||
en: { file: () => import("./locales/en.json") }, | ||
fr: { file: () => import("./locales/fr.json") }, | ||
} as const satisfies LocaleMap<LocaleInfo>; | ||
|
||
export const DEFAULT_LOCALE: Locale = "en"; | ||
|
||
/** | ||
* Mapping from locales to their respective i18n files. | ||
*/ | ||
export const LocalesI18nConfig = Object.values(LOCALES).reduce( | ||
(acc, locale) => ({ ...acc, [locale]: Locales[locale].file }), | ||
{} as LocaleMap<LocaleSchema>, | ||
); | ||
|
||
/** | ||
* Utils type. | ||
*/ | ||
export type File = () => Promise<unknown>; | ||
export type LocaleSchema = (typeof Locales)[typeof DEFAULT_LOCALE]["file"]; |
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,4 @@ | ||
{ | ||
"hello": "Hello", | ||
"welcome": "Welcome" | ||
} |
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,4 @@ | ||
{ | ||
"hello": "Bonjour", | ||
"welcome": "Bienvenue" | ||
} |
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,8 @@ | ||
import { createI18nMiddleware } from "next-international/middleware"; | ||
import { DEFAULT_LOCALE, LOCALES } from "./config"; | ||
|
||
export const I18nMiddleware = createI18nMiddleware({ | ||
locales: LOCALES, | ||
defaultLocale: DEFAULT_LOCALE, | ||
urlMappingStrategy: "rewrite", | ||
}); |
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,21 @@ | ||
import { LocalesI18nConfig } from "@/i18n/config"; | ||
import { createI18nServer } from "next-international/server"; | ||
|
||
/** | ||
* Create i18n server. | ||
*/ | ||
const I18nServer = createI18nServer(LocalesI18nConfig); | ||
|
||
/** | ||
* i18n server hook. | ||
* | ||
* @returns the necessary functions and variables to use i18n on server components. | ||
*/ | ||
export const useI18n = async () => { | ||
const t = await I18nServer.getI18n(); | ||
const getStaticParams = I18nServer.getStaticParams; | ||
|
||
const locale = I18nServer.getCurrentLocale(); | ||
|
||
return { t, getStaticParams, locale }; | ||
}; |
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,10 @@ | ||
import { NextRequest } from "next/server"; | ||
import { I18nMiddleware } from "@/i18n/middleware"; | ||
|
||
export function middleware(request: NextRequest) { | ||
return I18nMiddleware(request); | ||
} | ||
|
||
export const config = { | ||
matcher: ["/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt).*)"], | ||
}; |
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.