-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): add translations of titles (#750)
- Loading branch information
Showing
8 changed files
with
159 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,35 @@ | ||
import React from "react"; | ||
import React, { FC } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { HashRouter, Route, Switch } from "react-router-dom"; | ||
import { message } from "antd"; | ||
import { LastLocationProvider } from "react-router-last-location"; | ||
import { RouteConfig, routeConfig } from "../route-config"; | ||
import { AppRouteContainer } from "./AppRouteContainer"; | ||
import { I18nextProvider } from "react-i18next"; | ||
import { i18n } from "../utils/i18n"; | ||
|
||
export class AppRoutes extends React.Component { | ||
public componentDidCatch(error: any): void { | ||
void message.error(`网页加载发生错误:${error}`); | ||
} | ||
|
||
public render(): React.ReactElement { | ||
return ( | ||
<HashRouter> | ||
<I18nextProvider i18n={i18n}> | ||
<LastLocationProvider watchOnlyPathname> | ||
<Switch> | ||
{Object.keys(routeConfig).map(((name: keyof RouteConfig) => { | ||
const { path, component, title } = routeConfig[name]; | ||
return ( | ||
<Route | ||
key={name} | ||
exact={true} | ||
path={path} | ||
render={routeProps => ( | ||
<AppRouteContainer | ||
Comp={component} | ||
title={title} | ||
routeProps={routeProps} | ||
/> | ||
)} | ||
export const AppRoutes: FC = () => { | ||
const { t } = useTranslation(); | ||
return ( | ||
<HashRouter> | ||
<LastLocationProvider watchOnlyPathname> | ||
<Switch> | ||
{Object.keys(routeConfig).map(((name: keyof RouteConfig) => { | ||
const { path, component, title } = routeConfig[name]; | ||
return ( | ||
<Route | ||
key={name} | ||
exact={true} | ||
path={path} | ||
render={routeProps => ( | ||
<AppRouteContainer | ||
Comp={component} | ||
title={t("title-" + title)} | ||
routeProps={routeProps} | ||
/> | ||
); | ||
}) as (name: string) => React.ReactElement)} | ||
</Switch> | ||
</LastLocationProvider> | ||
</I18nextProvider> | ||
</HashRouter> | ||
); | ||
} | ||
} | ||
)} | ||
/> | ||
); | ||
}) as (name: string) => React.ReactElement)} | ||
</Switch> | ||
</LastLocationProvider> | ||
</HashRouter> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,33 +1,70 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import "flat-components/build/style.css"; | ||
import "../theme.less"; | ||
|
||
import { ConfigProvider } from "antd"; | ||
import enUS from "antd/lib/locale/en_US"; | ||
import zhCN from "antd/lib/locale/zh_CN"; | ||
|
||
import React, { useEffect, useMemo } from "react"; | ||
import ReactDOM from "react-dom"; | ||
|
||
import { I18nextProvider } from "react-i18next"; | ||
import { useUpdate } from "react-use"; | ||
|
||
import { i18n } from "../utils/i18n"; | ||
import { AppRoutes } from "../AppRoutes"; | ||
import { StoreProvider } from "../components/StoreProvider"; | ||
|
||
import "../theme.less"; | ||
import "flat-components/build/style.css"; | ||
|
||
/** configure right after import */ | ||
import { configure } from "mobx"; | ||
configure({ | ||
isolateGlobalState: true, | ||
}); | ||
|
||
const initUI = (): void => { | ||
ReactDOM.render( | ||
<ConfigProvider | ||
autoInsertSpaceInButton={false} | ||
locale={zhCN} | ||
// let popups scrolls with container parent | ||
getPopupContainer={trigger => trigger?.parentElement || document.body} | ||
> | ||
<StoreProvider> | ||
<AppRoutes /> | ||
</StoreProvider> | ||
</ConfigProvider>, | ||
document.getElementById("root") as HTMLElement, | ||
const App: React.FC = () => { | ||
const forceUpdate = useUpdate(); | ||
|
||
const antdLocale = useMemo( | ||
() => (i18n.language.startsWith("zh") ? zhCN : enUS), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[i18n.language], | ||
); | ||
|
||
useEffect(() => { | ||
const onLangChanged = (): void => { | ||
forceUpdate(); | ||
}; | ||
|
||
i18n.on("languageChanged", onLangChanged); | ||
|
||
return () => { | ||
i18n.off("languageChanged", onLangChanged); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return ( | ||
<I18nextProvider i18n={i18n}> | ||
<ConfigProvider | ||
autoInsertSpaceInButton={false} | ||
locale={antdLocale} | ||
// let popups scrolls with container parent | ||
getPopupContainer={getPopupContainer} | ||
> | ||
<StoreProvider> | ||
<AppRoutes /> | ||
</StoreProvider> | ||
</ConfigProvider> | ||
</I18nextProvider> | ||
); | ||
}; | ||
|
||
function getPopupContainer(trigger?: HTMLElement): HTMLElement { | ||
return trigger?.parentElement || document.body; | ||
} | ||
|
||
const initUI = (): void => { | ||
ReactDOM.render(<App />, document.getElementById("root")); | ||
}; | ||
|
||
export default initUI; |
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
Oops, something went wrong.