Skip to content

Commit

Permalink
feat(I18n): 添加国际化 开关 的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lingting committed Oct 21, 2021
1 parent e6cd052 commit f829d38
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type ProjectSetting = LayoutSettings & {
pwa?: boolean;
logo?: string;
historyType: 'browser' | 'hash' | 'memory';
// 是否开启国际化
i18n: boolean;
// 默认语言
defaultLocal: 'zh-CN' | 'en-US';
// 是否展示水印
Expand All @@ -29,6 +31,7 @@ const Settings: ProjectSetting = {
pwa: false,
logo: './logo.svg',
historyType: 'hash',
i18n: true,
defaultLocal: 'zh-CN',
waterMark: true,
multiTab: true,
Expand Down
3 changes: 2 additions & 1 deletion src/components/RightContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Avatar from './AvatarDropdown';
import styles from './index.less';
import SettingDrawer from '@/components/SettingDrawer';
import FullScreen from '../FullScreen';
import { settings } from '@/utils/ConfigUtils';

const GlobalHeaderRight: React.FC = () => {
const { initialState } = useModel('@@initialState');
Expand Down Expand Up @@ -40,7 +41,7 @@ const GlobalHeaderRight: React.FC = () => {

{/* 如果不需要退出确认. 移除 exitConfirm 即可 */}
<Avatar exitConfirm />
<SelectLang className={styles.action} />
{settings.i18n && <SelectLang className={styles.action} />}

<span
className={styles.action}
Expand Down
7 changes: 7 additions & 0 deletions src/layouts/BasicLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
const [collapsed, setCollapsed] = useState(false);
const [reload, setReload] = useState(false);
I18n.setIntl(useIntl());

// 国际化关闭, 当前语言与默认语言不符
if (!settings.i18n && I18n.getLocal() !== settings.defaultLocal) {
// 切换语言
I18n.setLocal(settings.defaultLocal);
}

const { initialState, setInitialState } = useModel('@@initialState');
const [keepAlivePropsState, keepAlivePropsDispatch] = useReducer(
(_state: any, newVal: any) => newVal,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/user/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const Login: React.FC = () => {

return (
<div className={styles.container}>
<div className={styles.lang} data-lang>
<div hidden={!settings.i18n} className={styles.lang} data-lang>
{SelectLang && <SelectLang />}
</div>
<VerifySlide
Expand Down
4 changes: 4 additions & 0 deletions src/utils/I18nUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IntlShape } from 'react-intl';
import { message } from 'antd';
import type { ConfigOnClose } from 'antd/lib/message';
import { settings } from './ConfigUtils';
import { setLocale } from 'umi';

export type I18nParams =
| string
Expand Down Expand Up @@ -29,6 +30,9 @@ const I18n = {
}
return settings.defaultLocal;
},
setLocal: (local: string) => {
setLocale(local);
},
text: (key: string, params?: Record<string, string>, defaultMessage = key) => {
return I18n.getIntl().formatMessage({ id: key, defaultMessage }, params);
},
Expand Down

0 comments on commit f829d38

Please sign in to comment.