-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,233 additions
and
275 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client" | ||
|
||
import { I18nProvider } from "@lingui/react" | ||
import { type I18nSetupData } from "./i18n" | ||
import { type ReactNode } from "react" | ||
import { setupI18n } from "@lingui/core" | ||
|
||
type LinguiProviderProps = I18nSetupData & { | ||
children: ReactNode | ||
} | ||
|
||
export function LinguiProvider({ | ||
children, | ||
messages, | ||
locale, | ||
}: LinguiProviderProps) { | ||
return ( | ||
<I18nProvider | ||
i18n={setupI18n({ | ||
messages, | ||
locale, | ||
})} | ||
> | ||
{children} | ||
</I18nProvider> | ||
) | ||
} |
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,16 @@ | ||
import React from "react" | ||
|
||
import { TransNoContext, type TransProps } from "@lingui/react/server" | ||
import { getI18n } from "./i18n" | ||
|
||
export const Trans = (props: TransProps) => { | ||
const i18n = getI18n() | ||
|
||
if (!i18n) { | ||
throw new Error( | ||
"Lingui for RSC is not initialized. Use `setI18n()` first in root of your RSC tree.", | ||
) | ||
} | ||
|
||
return <TransNoContext {...props} lingui={{ i18n }} /> | ||
} |
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 |
---|---|---|
@@ -1,52 +1,47 @@ | ||
import { type Locale, defaultLocale, locales } from "./i18nConfig" | ||
import { type ResourceLanguage, createInstance } from "i18next" | ||
import { initReactI18next } from "react-i18next/initReactI18next" | ||
import { z } from "zod" | ||
import resourcesToBackend from "i18next-resources-to-backend" | ||
import { type I18n, setupI18n } from "@lingui/core" | ||
import { type Locale, localeSchema } from "./i18nConfig" | ||
import { cache } from "react" | ||
import { logError } from "logger" | ||
|
||
const localeSchema = z.enum(locales).catch(defaultLocale) | ||
const getLocaleCtx = cache((): { current: I18n | undefined } => { | ||
return { current: undefined } | ||
}) | ||
|
||
export const setI18n = (locale: I18n) => { | ||
getLocaleCtx().current = locale | ||
} | ||
|
||
export const getI18n = (): I18n | undefined => { | ||
return getLocaleCtx().current | ||
} | ||
|
||
const loadCatalog = async (locale: string) => { | ||
try { | ||
const catalog = await import(`@lingui/loader!./locales/${locale}.po`) | ||
return catalog.messages | ||
} catch (e) { | ||
logError(`No catalog for locale "${locale}"`) | ||
return {} | ||
} | ||
} | ||
|
||
export type I18nSetupData = { | ||
locale: Locale | ||
messages: Partial<Record<Locale, Record<string, string>>> | ||
} | ||
|
||
export const initServerTranslations = async (params: { locale: string }) => { | ||
const locale = localeSchema.parse(params.locale) | ||
const i18nInstance = await initTranslations(locale) | ||
const catalog = await loadCatalog(locale) | ||
|
||
return i18nInstance | ||
} | ||
const i18nSetupData: I18nSetupData = { | ||
locale, | ||
messages: { [locale]: catalog }, | ||
} | ||
|
||
const i18n = setupI18n(i18nSetupData) | ||
|
||
setI18n(i18n) | ||
|
||
const namespaces = ["translation"] as const | ||
|
||
const en = { | ||
translation: { | ||
"hello world": "Hello World", | ||
"View rankings": "View rankings", | ||
}, | ||
} satisfies ResourceLanguage | ||
|
||
export const initTranslations = async (locale: Locale) => { | ||
const i18nInstance = createInstance() | ||
|
||
await i18nInstance | ||
.use(initReactI18next) | ||
.use( | ||
resourcesToBackend({ | ||
en, | ||
fr: { | ||
translation: { | ||
"hello world": "Bonjour le monde", | ||
"View rankings": "Voir le classement", | ||
}, | ||
}, | ||
}), | ||
) | ||
.init({ | ||
lng: locale, | ||
fallbackLng: defaultLocale, | ||
supportedLngs: locales, | ||
defaultNS: namespaces[0], | ||
fallbackNS: namespaces[0], | ||
ns: namespaces, | ||
preload: typeof window === "undefined" ? locales : [], | ||
}) | ||
|
||
return i18nInstance | ||
return i18nSetupData | ||
} |
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 |
---|---|---|
@@ -1,8 +1,29 @@ | ||
export const locales = ["en", "fr", "de", "ja"] as const | ||
export type Locale = (typeof locales)[number] | ||
export const defaultLocale: Locale = "en" | ||
import { z } from "zod" | ||
|
||
export const i18nConfig = { | ||
const IS_DEV = process.env.NODE_ENV !== "production" | ||
|
||
const locales = ["en", "fr"] as const | ||
const devLocales = [...locales, "pseudo-LOCALE"] as const | ||
export type Locale = (typeof locales)[number] | (typeof devLocales)[number] | ||
|
||
type I18nConfig = { | ||
locales: Locale[] | ||
defaultLocale: Locale | ||
} | ||
|
||
const i18nDevConfig: I18nConfig = { | ||
locales: [...devLocales], | ||
defaultLocale: "en", | ||
} | ||
|
||
const i18nProdConfig: I18nConfig = { | ||
locales: [...locales], | ||
defaultLocale, | ||
defaultLocale: "en", | ||
} | ||
|
||
export const i18nConfig = IS_DEV ? i18nDevConfig : i18nProdConfig | ||
|
||
export const localeSchema = | ||
process.env.NODE_ENV === "production" | ||
? z.enum(locales).catch("en") | ||
: z.enum(devLocales).catch("en") |
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,34 @@ | ||
msgid "" | ||
msgstr "" | ||
"POT-Creation-Date: 2023-10-22 00:59+0000\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=utf-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"X-Generator: @lingui/cli\n" | ||
"Language: en\n" | ||
"Project-Id-Version: \n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"PO-Revision-Date: \n" | ||
"Last-Translator: \n" | ||
"Language-Team: \n" | ||
"Plural-Forms: \n" | ||
|
||
#: app/[locale]/_landing/DiscordCard.tsx:77 | ||
msgid "{DISCORD_MEMBERS} discord members, and growing!" | ||
msgstr "{DISCORD_MEMBERS} discord members, and growing!" | ||
|
||
#: app/[locale]/page.tsx:97 | ||
msgid "2v2" | ||
msgstr "2v2" | ||
|
||
#: app/[locale]/page.tsx:81 | ||
msgid "or" | ||
msgstr "or" | ||
|
||
#: app/[locale]/_landing/DiscordCard.tsx:85 | ||
msgid "Question of the day" | ||
msgstr "Question of the day" | ||
|
||
#: app/[locale]/page.tsx:89 | ||
msgid "View rankings" | ||
msgstr "View rankings" |
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,34 @@ | ||
msgid "" | ||
msgstr "" | ||
"POT-Creation-Date: 2023-10-22 00:59+0000\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=utf-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"X-Generator: @lingui/cli\n" | ||
"Language: fr\n" | ||
"Project-Id-Version: \n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"PO-Revision-Date: \n" | ||
"Last-Translator: \n" | ||
"Language-Team: \n" | ||
"Plural-Forms: \n" | ||
|
||
#: app/[locale]/_landing/DiscordCard.tsx:77 | ||
msgid "{DISCORD_MEMBERS} discord members, and growing!" | ||
msgstr "{DISCORD_MEMBERS} membres sur Discord, et ça continue !" | ||
|
||
#: app/[locale]/page.tsx:97 | ||
msgid "2v2" | ||
msgstr "2v2" | ||
|
||
#: app/[locale]/page.tsx:81 | ||
msgid "or" | ||
msgstr "ou" | ||
|
||
#: app/[locale]/_landing/DiscordCard.tsx:85 | ||
msgid "Question of the day" | ||
msgstr "Question du jour" | ||
|
||
#: app/[locale]/page.tsx:89 | ||
msgid "View rankings" | ||
msgstr "Voir le classement" |
Oops, something went wrong.