2026-07-29 21:23:00 +08:00
|
|
|
import * as React from "react"
|
|
|
|
|
|
|
|
|
|
import type { I18nProjectConfig } from "../../config/lingui-config"
|
|
|
|
|
import { I18nProvider } from "../../runtime"
|
|
|
|
|
import { requestDevtoolJson } from "../api-client"
|
|
|
|
|
import { I18nDevtool } from "../i18n-devtool"
|
|
|
|
|
|
|
|
|
|
type ProjectState =
|
|
|
|
|
| { status: "loading" }
|
|
|
|
|
| { error: Error; status: "error" }
|
|
|
|
|
| { project: I18nProjectConfig; status: "ready" }
|
|
|
|
|
|
|
|
|
|
export function DevtoolApp() {
|
|
|
|
|
const [state, setState] = React.useState<ProjectState>({ status: "loading" })
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
requestDevtoolJson<I18nProjectConfig>("/__i18n/project").then(
|
|
|
|
|
(project) => setState({ project, status: "ready" }),
|
|
|
|
|
(reason) =>
|
|
|
|
|
setState({
|
|
|
|
|
error:
|
|
|
|
|
reason instanceof Error
|
|
|
|
|
? reason
|
|
|
|
|
: new Error("Unable to load the i18n project."),
|
|
|
|
|
status: "error",
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
if (state.status !== "ready") {
|
|
|
|
|
return (
|
|
|
|
|
<main
|
|
|
|
|
className="devtool-app__state"
|
|
|
|
|
role={state.status === "error" ? "alert" : "status"}
|
|
|
|
|
>
|
|
|
|
|
{state.status === "error"
|
|
|
|
|
? state.error.message
|
|
|
|
|
: "Loading I18n Devtool…"}
|
|
|
|
|
</main>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 14:47:42 +08:00
|
|
|
const locale =
|
|
|
|
|
state.project.locales.find(
|
2026-09-20 15:52:31 +08:00
|
|
|
(candidateLocale) => candidateLocale !== state.project.sourceLocale
|
2026-07-30 14:47:42 +08:00
|
|
|
) ?? state.project.sourceLocale
|
2026-07-29 21:23:00 +08:00
|
|
|
|
2026-07-30 14:47:42 +08:00
|
|
|
if (!state.project.locales.includes(locale)) {
|
2026-07-29 21:23:00 +08:00
|
|
|
return (
|
|
|
|
|
<main className="devtool-app__state">
|
2026-07-30 14:47:42 +08:00
|
|
|
Add a target locale with <code>workspace-i18n new <locale></code>.
|
2026-07-29 21:23:00 +08:00
|
|
|
</main>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<I18nProvider locale={locale} locales={state.project.locales}>
|
2026-07-30 14:47:42 +08:00
|
|
|
<I18nDevtool
|
|
|
|
|
defaultOpen
|
|
|
|
|
mode="standalone"
|
|
|
|
|
sourceLocale={state.project.sourceLocale}
|
|
|
|
|
/>
|
2026-07-29 21:23:00 +08:00
|
|
|
</I18nProvider>
|
|
|
|
|
)
|
|
|
|
|
}
|