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

fix: App locale no work #10969

Merged
merged 4 commits into from
Apr 20, 2023
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
6 changes: 5 additions & 1 deletion examples/with-antd-5/.umirc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export default {
plugins: ['@umijs/plugins/dist/antd'],
plugins: ['@umijs/plugins/dist/antd', '@umijs/plugins/dist/locale'],
locale: {
title: true,
default: 'zh-CN',
},
antd: {
// valid for antd5.0 only
theme: {
Expand Down
3 changes: 3 additions & 0 deletions examples/with-antd-5/locales/en-US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
HELLO: 'Hello',
};
3 changes: 3 additions & 0 deletions examples/with-antd-5/locales/zh-CN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
HELLO: '你好',
};
20 changes: 17 additions & 3 deletions examples/with-antd-5/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { App, Button, Space, version } from 'antd';
import React from 'react';
import React, { useState } from 'react';
import { getLocale, setLocale, useIntl } from 'umi';

export default function Page() {
const [isZh, setIsZh] = useState(true);
// 若要使用 useApp hook,须先在 antd 插件中配置 appConfig
const { message, modal } = App.useApp();

const locale = getLocale();
const showModal = () => {
modal.confirm({
title: 'Hai',
content: '注意 Modal 内的按钮样式应该和页面中的按钮样一致',
maskClosable: true,
});
};

const intl = useIntl();
const msg = intl.formatMessage({
id: 'HELLO',
});
const sayHai = () => {
// .umirc.ts 中配置了 appConfig.message.maxCount = 3
// app.txt 中配置了 appConfig.message.duration = 5
Expand All @@ -28,6 +33,15 @@ export default function Page() {
Open Modal
</Button>
</Space>
locale:{locale}
<Button
onClick={() => {
setIsZh(!isZh);
setLocale(isZh ? 'en-US' : 'zh-CN', false);
}}
>
{msg}
</Button>
</>
);
}
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"files": [
"dist",
"libs",
"tpls"
"templates"
],
"scripts": {
"build": "umi-scripts father build",
Expand Down
7 changes: 5 additions & 2 deletions packages/plugins/src/antd.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import assert from 'assert';
import { dirname, join } from 'path';
import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi';
import { deepmerge, Mustache, semver } from 'umi/plugin-utils';
import { deepmerge, Mustache, semver, winPath } from 'umi/plugin-utils';
import { TEMPLATES_DIR } from './constants';
import { resolveProjectDep } from './utils/resolveProjectDep';
import { withTmpPath } from './utils/withTmpPath';

const ANTD_TEMPLATES_DIR = join(TEMPLATES_DIR, 'antd');

export default (api: IApi) => {
let pkgPath: string;
let antdVersion = '4.0.0';
Expand Down Expand Up @@ -196,7 +199,7 @@ export default (api: IApi) => {
appConfig:
appComponentAvailable && JSON.stringify(api.config.antd.appConfig),
},
tplPath: join(__dirname, '../tpls/antd-runtime.ts.tpl'),
tplPath: winPath(join(ANTD_TEMPLATES_DIR, 'runtime.ts.tpl')),
});

api.writeTmpFile({
Expand Down
3 changes: 3 additions & 0 deletions packages/plugins/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { join } from 'path';

export const TEMPLATES_DIR = join(__dirname, '../templates');
10 changes: 6 additions & 4 deletions packages/plugins/src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, readFileSync } from 'fs';
import { dirname, join } from 'path';
import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi';
import { lodash, Mustache, winPath } from 'umi/plugin-utils';
import { TEMPLATES_DIR } from './constants';
import {
exactLocalePaths,
getAntdLocale,
Expand All @@ -13,6 +14,7 @@ import {
} from './utils/localeUtils';
import { withTmpPath } from './utils/withTmpPath';

const LOCALE_TEMPLATES_DIR = join(TEMPLATES_DIR, 'locale');
interface ILocaleConfig {
default?: string;
baseNavigator?: boolean;
Expand Down Expand Up @@ -106,7 +108,7 @@ export default (api: IApi) => {

api.onGenerateFiles(async () => {
const localeTpl = readFileSync(
join(__dirname, '../libs/locale/locale.tpl'),
join(LOCALE_TEMPLATES_DIR, 'locale.tpl'),
'utf-8',
);
// moment2dayjs
Expand Down Expand Up @@ -176,7 +178,7 @@ export default (api: IApi) => {
});

const localeExportsTpl = readFileSync(
join(__dirname, '../libs/locale/localeExports.tpl'),
join(LOCALE_TEMPLATES_DIR, 'localeExports.tpl'),
'utf-8',
);
const localeDirName = 'locales';
Expand Down Expand Up @@ -209,7 +211,7 @@ export default (api: IApi) => {
});
// runtime.tsx
const runtimeTpl = readFileSync(
join(__dirname, '../libs/locale/runtime.tpl'),
join(LOCALE_TEMPLATES_DIR, 'runtime.tpl'),
'utf-8',
);
api.writeTmpFile({
Expand All @@ -221,7 +223,7 @@ export default (api: IApi) => {

// SelectLang.tsx
const selectLang = readFileSync(
join(__dirname, '../libs/locale/SelectLang.tpl'),
join(LOCALE_TEMPLATES_DIR, 'SelectLang.tpl'),
'utf-8',
);

Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/src/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { existsSync, opendirSync } from 'fs';
import { join } from 'path';
import type { IApi } from 'umi';
import { lodash, winPath } from 'umi/plugin-utils';
import { TEMPLATES_DIR } from './constants';
import { toRemotesCodeString } from './utils/mfUtils';

const { isEmpty } = lodash;

const mfSetupPathFileName = '_mf_setup-public-path.js';
const mfAsyncEntryFileName = 'asyncEntry.ts';
const MF_TEMPLATES_DIR = join(TEMPLATES_DIR, 'mf');

export default function mf(api: IApi) {
api.describe({
Expand Down Expand Up @@ -122,7 +124,7 @@ export default function mf(api: IApi) {
context: {
remoteCodeString: toRemotesCodeString(remotes),
},
tplPath: join(__dirname, '../tpls/mf-runtime.ts.tpl'),
tplPath: winPath(join(MF_TEMPLATES_DIR, 'runtime.ts.tpl')),
});

if (api.env === 'development' && api.config.mfsu) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ import {
import { ApplyPluginsType } from 'umi';
import { getPluginManager } from '../core/plugin';

let cacheAntdConfig = null;

const getAntdConfig = () => {
if(!cacheAntdConfig){
cacheAntdConfig = getPluginManager().applyPlugins({
key: 'antd',
type: ApplyPluginsType.modify,
initialValue: {
{{#configProvider}}
...{{{configProvider}}},
{{/configProvider}}
{{#appConfig}}
appConfig: {{{appConfig}}},
{{/appConfig}}
},
});
}
return cacheAntdConfig;
}

export function rootContainer(rawContainer) {
const {
appConfig: finalAppConfig = {},
appConfig,
...finalConfigProvider
} = getPluginManager().applyPlugins({
key: 'antd',
type: ApplyPluginsType.modify,
initialValue: {
{{#configProvider}}
...{{{configProvider}}},
{{/configProvider}}
{{#appConfig}}
appConfig: {{{appConfig}}},
{{/appConfig}}
},
});

} = getAntdConfig();
let container = rawContainer;

{{#appConfig}}
// The App component should be under ConfigProvider
container = <App {...finalAppConfig}>{container}</App>;
{{/appConfig}}

{{#configProvider}}
if (finalConfigProvider.prefixCls) {
Modal.config({
Expand All @@ -61,3 +63,14 @@ export function rootContainer(rawContainer) {

return container;
}

{{#appConfig}}
// The App component should be under ConfigProvider
// plugin-locale has other ConfigProvider
export function innerProvider(container: any) {
const {
appConfig: finalAppConfig = {},
} = getAntdConfig();
return <App {...finalAppConfig}>{container}</App>;
}
{{/appConfig}}