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: locale moment bug [在 locale 插件中引入 moment 相关文件] #1029

Merged
merged 1 commit into from
Sep 9, 2018
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
1 change: 1 addition & 0 deletions packages/umi-plugin-locale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"umi"
],
"dependencies": {
"moment": "2.x",
"mustache": "^2.3.0",
"prop-types": "^15.6.2",
"react-intl": "^2.4.0",
Expand Down
25 changes: 23 additions & 2 deletions packages/umi-plugin-locale/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import {
import { winPath } from 'umi-utils';
import Mustache from 'mustache';

const momentLocation = require
.resolve('moment/locale/zh-cn')
.replace(/zh\-cn\.js$/, '');

function getMomentLocale(lang, country) {
if (
existsSync(
join(momentLocation, `${lang}-${country.toLocaleLowerCase()}.js`),
)
) {
return `${lang}-${country.toLocaleLowerCase()}`;
}
if (existsSync(join(momentLocation, `${lang}.js`))) {
return lang;
}
return '';
}

// export for test
export function getLocaleFileList(absSrcPath, singular) {
const localeList = [];
Expand All @@ -31,6 +49,7 @@ export function getLocaleFileList(absSrcPath, singular) {
country: fileInfo[2],
name: `${fileInfo[1]}-${fileInfo[2]}`,
path: winPath(fullname),
momentLocale: getMomentLocale(fileInfo[1], fileInfo[2]),
});
}
}
Expand All @@ -57,15 +76,17 @@ export default function(api, options = {}) {
'utf-8',
);
const defaultLocale = options.default || 'zh-CN';
const [lang, country] = defaultLocale.split('-');
const wrapperContent = Mustache.render(wrapperTpl, {
localeList: localeFileList,
antd: options.antd === undefined ? true : options.antd,
baseNavigator:
options.baseNavigator === undefined ? true : options.baseNavigator,
useLocalStorage: true,
defaultLocale,
defaultLang: defaultLocale.split('-')[0],
defaultAntdLocale: defaultLocale.replace('-', '_'),
defaultLang: lang,
defaultAntdLocale: `${lang}_${country}`,
defaultMomentLocale: getMomentLocale(lang, country),
});
const wrapperPath = join(paths.absTmpDirPath, './LocaleWrapper.jsx');
writeFileSync(wrapperPath, wrapperContent, 'utf-8');
Expand Down
16 changes: 15 additions & 1 deletion packages/umi-plugin-locale/template/wrapper.jsx.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ const InjectedWrapper = injectIntl(function(props) {
})
{{/localeList.length}}

{{#localeList}}
{{#antd}}
{{#momentLocale}}
import 'moment/locale/{{momentLocale}}';
{{/momentLocale}}
{{/antd}}
{{/localeList}}

const baseNavigator = {{{baseNavigator}}};
const useLocalStorage = {{{useLocalStorage}}};

{{#antd}}
import { LocaleProvider } from 'antd';
import moment from 'moment';
{{#defaultMomentLocale}}
import 'moment/locale/{{defaultMomentLocale}}';
{{/defaultMomentLocale}}
const defaultAntd = require('antd/lib/locale-provider/{{defaultAntdLocale}}');
{{/antd}}

Expand All @@ -24,14 +36,16 @@ const localeInfo = {
locale: '{{name}}',
{{#antd}}antd: require('antd/lib/locale-provider/{{lang}}_{{country}}'),{{/antd}}
data: require('react-intl/locale-data/{{lang}}'),
momentLocale: '{{momentLocale}}',
},
{{/localeList}}
};

let appLocale = {
locale: '{{defaultLocale}}',
messages: {},
data: require('react-intl/locale-data/{{defaultLang}}')
data: require('react-intl/locale-data/{{defaultLang}}'),
momentLocale: '{{defaultMomentLocale}}',
};
if (useLocalStorage && localStorage.getItem('umi_locale') && localeInfo[localStorage.getItem('umi_locale')]) {
appLocale = localeInfo[localStorage.getItem('umi_locale')];
Expand Down
6 changes: 4 additions & 2 deletions packages/umi-plugin-locale/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('test plugin', () => {
expect(ret).toEqual(
expect.stringContaining('antd/lib/locale-provider/en_US'),
);

expect(ret).toEqual(expect.stringContaining('moment/locale/zh-cn'));
unlinkSync(wrapperFile);
});
});
Expand All @@ -58,7 +58,7 @@ test('antd is false', () => {
expect(ret).not.toEqual(
expect.stringContaining('antd/lib/locale-provider/zh_CN'),
);

expect(ret).not.toEqual(expect.stringContaining('moment/locale/zh-cn'));
unlinkSync(wrapperFile);
});

Expand All @@ -71,12 +71,14 @@ describe('test func with singular true', () => {
country: 'US',
name: 'en-US',
path: `${absSrcPath}/locale/en-US.js`,
momentLocale: '',
},
{
lang: 'zh',
country: 'CN',
name: 'zh-CN',
path: `${absSrcPath}/locale/zh-CN.js`,
momentLocale: 'zh-cn',
},
]);
});
Expand Down