From 224a08c05756f29d53f9faa51dc2029ef6efbba0 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 17 Jan 2022 12:23:32 +0800 Subject: [PATCH] feat: optimize antd-config-orovider --- .../src/antd-config-provider/demos/demo1.tsx | 25 +++++++++++++++++++ .../client/src/antd-config-provider/index.md | 4 ++- .../client/src/antd-config-provider/index.tsx | 25 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 packages/client/src/antd-config-provider/demos/demo1.tsx diff --git a/packages/client/src/antd-config-provider/demos/demo1.tsx b/packages/client/src/antd-config-provider/demos/demo1.tsx new file mode 100644 index 000000000..dd79a424e --- /dev/null +++ b/packages/client/src/antd-config-provider/demos/demo1.tsx @@ -0,0 +1,25 @@ +import React from 'react'; +import { Table } from 'antd'; +import { i18n, compose, APIClient, APIClientProvider, AntdConfigProvider } from '@nocobase/client'; +import MockAdapter from 'axios-mock-adapter'; +import { I18nextProvider } from 'react-i18next'; + +const apiClient = new APIClient({ + baseURL: `${location.protocol}//${location.host}/api/`, +}); + +const mock = new MockAdapter(apiClient.axios); + +mock.onGet('/app:getLang').reply(200, { + data: { lang: 'zh-CN' }, +}); + +const providers = [ + [APIClientProvider, { apiClient }], + [I18nextProvider, { i18n }], + [AntdConfigProvider, { remoteLocale: true }], +]; + +export default compose(...providers)(() => { + return ; +}); diff --git a/packages/client/src/antd-config-provider/index.md b/packages/client/src/antd-config-provider/index.md index 8ec194f78..c066a8f68 100644 --- a/packages/client/src/antd-config-provider/index.md +++ b/packages/client/src/antd-config-provider/index.md @@ -7,4 +7,6 @@ group: # AntdConfigProvider -为 antd 组件提供统一的全局化配置 \ No newline at end of file +为 antd 组件提供统一的全局化配置 + + diff --git a/packages/client/src/antd-config-provider/index.tsx b/packages/client/src/antd-config-provider/index.tsx index 4cb2df6cd..f2ddeec9f 100644 --- a/packages/client/src/antd-config-provider/index.tsx +++ b/packages/client/src/antd-config-provider/index.tsx @@ -3,8 +3,31 @@ import { I18nextProvider, useTranslation } from 'react-i18next'; import { ConfigProvider, Spin } from 'antd'; import enUS from 'antd/lib/locale/en_US'; import zhCN from 'antd/lib/locale/zh_CN'; +import { useRequest } from '../api-client'; export function AntdConfigProvider(props) { + const { remoteLocale, ...others } = props; const { i18n } = useTranslation(); - return {props.children}; + const { loading } = useRequest( + { + url: 'app:getLang', + }, + { + onSuccess(data) { + const locale = localStorage.getItem('locale'); + if (data?.data?.lang && locale !== data?.data?.lang) { + i18n.changeLanguage(data?.data?.lang); + } + }, + manual: !remoteLocale, + }, + ); + if (loading) { + return ; + } + return ( + + {props.children} + + ); }