Skip to content

Commit 3fbea37

Browse files
committed
fix(lingui): Use json files instead of TS
1 parent 16fc17f commit 3fbea37

7 files changed

Lines changed: 19 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
dist/
77
node_modules
88
storybook-static/
9-
src/locales/_*.ts
9+
src/locales/_*.json
1010

1111
.eslintcache

lingui.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from "@lingui/cli"
33
export default defineConfig({
44
sourceLocale: "en",
55
locales: ["en", "de"],
6+
compileNamespace: "json",
67
catalogs: [
78
{
89
path: "<rootDir>/src/locales/_{locale}",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"start": "pnpm run build && pnpm run --filter ./docs start",
4343
"build": "node ./scripts/build.mjs",
4444
"build:docs": "pnpm run build && pnpm run --filter ./docs build",
45-
"l10n:build": "lingui extract --clean && lingui compile --typescript",
45+
"l10n:build": "lingui extract --clean && lingui compile",
4646
"lint": "tsc -b && eslint",
4747
"lint:fix": "pnpm run lint --fix",
4848
"test": "vitest"

scripts/build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const run = cmd =>
2929
})
3030

3131
printHeadline("🛠️ Compile translations")
32-
await run("lingui compile --typescript")
32+
await run("lingui compile")
3333

3434
printHeadline("🛠️ Bundling javascript")
3535
await run("vite build")

src/locales/_de.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ msgstr "Ein unerwarteter Fehler ist aufgetreten"
2121
msgid "Cancel"
2222
msgstr "Abbrechen"
2323

24-
#: src/components/ui/text-input.tsx:79
24+
#: src/components/ui/text-input.tsx:81
2525
msgid "Clear text field"
2626
msgstr "Textfeld leeren"
2727

2828
#: src/components/ui/dialog/dialog.tsx:128
2929
msgid "Close"
3030
msgstr "Schließen"
3131

32-
#: src/components/ui/toaster/toast.tsx:127
32+
#: src/components/ui/toaster/toast.tsx:126
3333
msgid "Close message"
3434
msgstr "Nachricht schließen"
3535

@@ -65,6 +65,6 @@ msgstr "Vorheriges Jahr"
6565
msgid "Size"
6666
msgstr "Größe"
6767

68-
#: src/components/ui/date-input.tsx:16
68+
#: src/components/ui/date-input.tsx:18
6969
msgid "Today"
7070
msgstr "Heute"

src/locales/_en.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ msgstr "An unexpected error occurred here"
2121
msgid "Cancel"
2222
msgstr "Cancel"
2323

24-
#: src/components/ui/text-input.tsx:79
24+
#: src/components/ui/text-input.tsx:81
2525
msgid "Clear text field"
2626
msgstr "Clear text field"
2727

2828
#: src/components/ui/dialog/dialog.tsx:128
2929
msgid "Close"
3030
msgstr "Close"
3131

32-
#: src/components/ui/toaster/toast.tsx:127
32+
#: src/components/ui/toaster/toast.tsx:126
3333
msgid "Close message"
3434
msgstr "Close message"
3535

@@ -65,6 +65,6 @@ msgstr "Previous year"
6565
msgid "Size"
6666
msgstr "Size"
6767

68-
#: src/components/ui/date-input.tsx:16
68+
#: src/components/ui/date-input.tsx:18
6969
msgid "Today"
7070
msgstr "Today"

src/locales/l10n-provider.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@ import { type PropsWithChildren, useEffect, useMemo, useState } from "react"
22

33
import { type I18n, type MessageDescriptor, setupI18n } from "@lingui/core"
44

5-
import { messages as de } from "./_de"
6-
import { messages as en } from "./_en"
5+
import { messages as de } from "./_de.json"
6+
import { messages as en } from "./_en.json"
77
import { createContext } from "../utils/create-context"
88

9-
const availableLanguages = new Set(["en", "de"])
10-
9+
const defaultLanguage = "en"
1110
const catalog = { en, de }
12-
1311
const i18n = setupI18n({
14-
locales: [...availableLanguages],
15-
locale: "en",
12+
locales: Object.keys(catalog),
13+
locale: defaultLanguage,
1614
messages: catalog,
1715
})
1816

19-
const activate = (languageArg: "en" | "de") => {
20-
const language = availableLanguages.has(languageArg) ? languageArg : "en"
21-
i18n.loadAndActivate({ locale: language, messages: catalog[language] })
22-
return language
23-
}
24-
2517
interface L10nProviderState {
2618
i18n: I18n
2719
locale: string
@@ -31,14 +23,16 @@ const Context = createContext<L10nProviderState>("L10nProvider")
3123

3224
export const L10nProvider = ({
3325
locale,
34-
language,
26+
language: languageProp,
3527
children,
3628
}: PropsWithChildren<Omit<L10nProviderState, "i18n">>) => {
29+
const language = languageProp in catalog ? languageProp : defaultLanguage
3730
const [activeLanguage, setActiveLanguage] = useState<"en" | "de">()
3831

3932
useEffect(() => {
33+
i18n.activate(language)
4034
// enforce rerendering consumers when language changes
41-
setActiveLanguage(activate(language))
35+
setActiveLanguage(language)
4236
}, [language])
4337

4438
return (

0 commit comments

Comments
 (0)