Skip to content

Commit

Permalink
♻️ build: 基于 react-i18next 重构国际化实现
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Mar 20, 2022
1 parent 0534903 commit 567bd21
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 48 deletions.
10 changes: 6 additions & 4 deletions packages/user-panel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"clean": "rm -rf es lib dist build coverage .umi"
},
"dependencies": {
"@arvinxu/i18n": "1.0.1",
"classnames": "^2.3.1",
"react-router": "^5.2.1",
"@ant-design/icons": "^4.x",
"@ant-design/pro-form": "^1.x",
"antd": "^4.x"
"antd": "^4.x",
"classnames": "^2.3.1",
"i18next": "^21.6.14",
"i18next-browser-languagedetector": "^6.1.3",
"react-i18next": "^11.16.1",
"react-router": "^5.2.1"
}
}
9 changes: 6 additions & 3 deletions packages/user-panel/src/components/Intl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import createI18n from '@arvinxu/i18n';
import localeMessageMaps from '../locales';
import { useTranslation } from 'react-i18next';

export const { IntlProvider, useFormatMessage } = createI18n(localeMessageMaps);
export const useFormatMessage = () => {
const { t } = useTranslation();

return (id, values?) => t(id, values);
};
32 changes: 16 additions & 16 deletions packages/user-panel/src/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { CSSProperties } from 'react';
import type { FC } from 'react';
import React from 'react';
import type { FC, CSSProperties } from 'react';
import cls from 'classnames';

import type { FooterProps, HeaderProps } from '../components';
import { IntlProvider, Header, Footer } from '../components';
import { Header, Footer } from '../components';
import '../locales';

import './style.less';

export interface LayoutProps extends FooterProps, HeaderProps {
Expand All @@ -27,19 +29,17 @@ const Layout: FC<LayoutProps> = ({
className,
}) => {
return (
<IntlProvider>
<div className={cls('avx-user-panel-container', className)} style={style}>
<Header type={type} logo={logo} logoUrl={logoUrl} />
{children}
{(onWechatLoginClick || onRegisterClick) && showFooter ? (
<Footer
type={type}
onWechatLoginClick={onWechatLoginClick}
onRegisterClick={onRegisterClick}
/>
) : null}
</div>
</IntlProvider>
<div className={cls('avx-user-panel-container', className)} style={style}>
<Header type={type} logo={logo} logoUrl={logoUrl} />
{children}
{(onWechatLoginClick || onRegisterClick) && showFooter ? (
<Footer
type={type}
onWechatLoginClick={onWechatLoginClick}
onRegisterClick={onRegisterClick}
/>
) : null}
</div>
);
};

Expand Down
3 changes: 0 additions & 3 deletions packages/user-panel/src/locales/en-US.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/user-panel/src/locales/en-US/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pages from './login';

export default pages;
26 changes: 23 additions & 3 deletions packages/user-panel/src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import type { LocaleMessageMaps } from '@arvinxu/i18n';
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

import zhCN from './zh-CN';
import en from './en-US';

const resources = {
en: { translation: en },
'zh-CN': { translation: zhCN },
};

i18n
.use(initReactI18next)

.use(LanguageDetector)
.init({
resources,

const localeMessageMaps: LocaleMessageMaps = { 'zh-CN': zhCN };
fallbackLng: 'zh-CN',
interpolation: {
escapeValue: false, // react already safes from xss
},
});

export default localeMessageMaps;
export default i18n;
4 changes: 0 additions & 4 deletions packages/user-panel/src/locales/zh-CN.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/user-panel/src/locales/zh-CN/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import login from './login';
import register from './register';

export default { ...login, ...register };
14 changes: 2 additions & 12 deletions packages/user-panel/src/login/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@ import React from 'react';
import { render, fireEvent, act } from '@testing-library/react';

import Login from './index';
import { IntlProvider } from '../components';

describe('Login 组件', () => {
it('默认状态', () => {
const { container } = render(
<IntlProvider>
<Login />
</IntlProvider>,
);
const { container } = render(<Login />);
expect(container).toMatchSnapshot();
});

it('获取验证码', async () => {
const {
// container,
findByText,
} = render(
<IntlProvider>
<Login />
</IntlProvider>,
);
} = render(<Login />);
const btn = await findByText('手机号登录');

await act(async () => {
fireEvent.click(btn);
const captcha = await findByText('获取验证码');
fireEvent.click(captcha);
});
// expect(container).toMatchSnapshot();
});
});
51 changes: 48 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 567bd21

Please sign in to comment.