Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom translations #8175

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/provider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const ConfigProviderContainer: React.FC<{
hashed?: boolean;
dark?: boolean;
prefixCls?: string;
intl?: IntlType;
}> = (props) => {
const {
children,
Expand All @@ -212,6 +213,7 @@ const ConfigProviderContainer: React.FC<{
autoClearCache = false,
token: propsToken,
prefixCls,
intl,
} = props;
const { locale, getPrefixCls, ...restConfig } = useContext(
AntdConfigProvider.ConfigContext,
Expand Down Expand Up @@ -246,10 +248,11 @@ const ConfigProviderContainer: React.FC<{
const localeName = locale?.locale;
const key = findIntlKeyByAntdLocaleKey(localeName);
// antd 的 key 存在的时候以 antd 的为主
const intl =
localeName && proProvide.intl?.locale === 'default'
const resolvedIntl =
intl ??
(localeName && proProvide.intl?.locale === 'default'
? intlMap[key! as 'zh-CN']
: proProvide.intl || intlMap[key! as 'zh-CN'];
: proProvide.intl || intlMap[key! as 'zh-CN']);

return {
...proProvide,
Expand All @@ -260,7 +263,7 @@ const ConfigProviderContainer: React.FC<{
themeId: tokenContext.theme.id,
layout: proLayoutTokenMerge,
}),
intl: intl || zhCNIntl,
intl: resolvedIntl || zhCNIntl,
};
}, [
locale?.locale,
Expand All @@ -271,6 +274,7 @@ const ConfigProviderContainer: React.FC<{
proComponentsCls,
antCls,
proLayoutTokenMerge,
intl,
]);

const finalToken = {
Expand Down Expand Up @@ -374,6 +378,7 @@ export const ProConfigProvider: React.FC<{
dark?: boolean;
hashed?: boolean;
prefixCls?: string;
intl?: IntlType;
}> = (props) => {
const { needDeps, dark, token } = props;
const proProvide = useContext(ProConfigContext);
Expand Down
26 changes: 25 additions & 1 deletion tests/provider/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProConfigProvider, useStyle } from '@ant-design/pro-components';
import { ProConfigProvider, ProForm, ProFormMoney, createIntl, useStyle } from '@ant-design/pro-components';
import { cleanup, render } from '@testing-library/react';
import { ConfigProvider } from 'antd';

Expand Down Expand Up @@ -36,4 +36,28 @@ describe('ProConfigProvider', () => {
</ConfigProvider>,
);
});

it('custom translations should be respected', () => {
const { container } = render(
<ConfigProvider
>
<ProConfigProvider intl={createIntl(
'en',
{
moneySymbol: '!?'
}
)}>
<ProForm
>
<ProFormMoney name="amount" initialValue={44.33} />
</ProForm>
</ProConfigProvider>
</ConfigProvider>,
);

expect(container.querySelectorAll('input#amount')[0]).toHaveAttribute(
'value',
'!? 44.33',
);
});
});
Loading