diff --git a/jest.config.base.js b/jest.config.base.js index e6a1ec36..c3fc56c9 100644 --- a/jest.config.base.js +++ b/jest.config.base.js @@ -18,6 +18,7 @@ module.exports = { '@arvinxu/user-panel': '/packages/user-panel/src', '@arvinxu/heatmap-calendar': '/packages/heatmap-calendar/src', '@arvinxu/utils': '/packages/utils/src', + '@arvinxu/i18n': '/packages/i18n/src', '@arvinxu/float-label-input': '/packages/float-label-input/src', '@arvinxu/page-loading': '/packages/page-loading/src', '@arvinxu/mindflow': '/packages/mindflow/src', diff --git a/packages/i18n/.fatherrc.js b/packages/i18n/.fatherrc.js new file mode 100644 index 00000000..87f204d0 --- /dev/null +++ b/packages/i18n/.fatherrc.js @@ -0,0 +1,5 @@ +const base = require('../../.fatherrc'); + +module.exports = { + ...base, +}; diff --git a/packages/i18n/README.md b/packages/i18n/README.md new file mode 100644 index 00000000..68968a96 --- /dev/null +++ b/packages/i18n/README.md @@ -0,0 +1,14 @@ +# @arvinxu/i18n + +[![NPM version][version-image]][version-url] [![NPM downloads][download-image]][download-url] + +## License + +[MIT](../../LICENSE) ® Arvin Xu + + + +[version-image]: http://img.shields.io/npm/v/@arvinxu/i18n.svg?color=deepgreen&label=latest +[version-url]: http://npmjs.org/package/@arvinxu/i18n +[download-image]: https://img.shields.io/npm/dm/@arvinxu/i18n.svg +[download-url]: https://npmjs.org/package/@arvinxu/i18n diff --git a/packages/i18n/jest.config.js b/packages/i18n/jest.config.js new file mode 100644 index 00000000..8abec5f1 --- /dev/null +++ b/packages/i18n/jest.config.js @@ -0,0 +1,14 @@ +const base = require('../../jest.config.base'); + +const packageName = '@arvinxu/i18n'; + +const root = '/packages/i18n'; + +module.exports = { + ...base, + rootDir: '../..', + roots: [root], + name: packageName, + displayName: packageName, + collectCoverageFrom: [`${root}/src/**/*.tsx`, `${root}/src/**/*.ts`], +}; diff --git a/packages/i18n/package.json b/packages/i18n/package.json new file mode 100644 index 00000000..f775b528 --- /dev/null +++ b/packages/i18n/package.json @@ -0,0 +1,25 @@ +{ + "name": "@arvinxu/i18n", + "version": "1.0.0", + "files": [ + "lib", + "es" + ], + "main": "lib/index.js", + "module": "es/index.js", + "homepage": "https://github.com/arvinxx/components/tree/master/packages/i18n#readme", + "repository": "git+https://github.com/arvinxx/components.git", + "publishConfig": { + "registry": "https://registry.npmjs.org", + "access": "public" + }, + "scripts": { + "build": "father-build && yarn webpack", + "webpack": "webpack", + "test": "jest", + "test:update": "jest -u", + "prepublishOnly": "yarn build", + "cov": "jest --coverage", + "clean": "rm -rf es lib dist build coverage .umi" + } +} diff --git a/packages/i18n/src/Intl.tsx b/packages/i18n/src/Intl.tsx new file mode 100644 index 00000000..df8a74bd --- /dev/null +++ b/packages/i18n/src/Intl.tsx @@ -0,0 +1,7 @@ +import React from 'react'; +import { IntlProvider as IntlProvider_ } from 'react-intl'; +import type { IntlProviderProps } from './type'; + +export const IntlProvider: IntlProviderProps = (props) => ( + +); diff --git a/packages/i18n/src/index.ts b/packages/i18n/src/index.ts new file mode 100644 index 00000000..da141cea --- /dev/null +++ b/packages/i18n/src/index.ts @@ -0,0 +1,2 @@ +export * from './Intl'; +export * from './useI18n'; diff --git a/packages/i18n/src/type.ts b/packages/i18n/src/type.ts new file mode 100644 index 00000000..3959db61 --- /dev/null +++ b/packages/i18n/src/type.ts @@ -0,0 +1,12 @@ +import type { ComponentProps, FC } from 'react'; +import type { IntlProvider } from 'react-intl'; + +export type LocalesType = 'en-US' | 'zh-CN'; + +export type LocaleKey = string; + +export type IntlProviderProps> = FC< + Omit, 'messages'> & { + messages: T; + } +>; diff --git a/packages/i18n/src/useI18n.ts b/packages/i18n/src/useI18n.ts new file mode 100644 index 00000000..256b2a0b --- /dev/null +++ b/packages/i18n/src/useI18n.ts @@ -0,0 +1,39 @@ +import { useEffect, useState } from 'react'; +import { useIntl } from 'react-intl'; + +import type { PrimitiveType } from 'intl-messageformat'; +import type { LocalesType, LocaleKey } from './type'; + +/** + * + */ +export const useI18n = (inputMessage: T) => { + const [locale, setLocale] = useState('zh-CN'); + const [messages, setMessages] = useState(inputMessage); + + useEffect(() => { + switch (locale) { + case 'en-US': + setMessages(inputMessage); + break; + case 'zh-CN': + default: + setMessages(inputMessage); + break; + } + }, [locale]); + + const switchLocale = (l: LocalesType): void => { + setLocale(l); + }; + + return { locale, messages, switchLocale }; +}; + +export const useFormatMessage = (): (( + id: LocaleKey, + values?: Record, +) => string) => { + const intl = useIntl(); + return (id, values) => intl.formatMessage({ id }, values); +}; diff --git a/packages/i18n/tsconfig.json b/packages/i18n/tsconfig.json new file mode 100644 index 00000000..b61bb516 --- /dev/null +++ b/packages/i18n/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "declaration": true, + "jsx": "react-jsx", + "skipLibCheck": true, + /* babel 输出类型 */ + "moduleResolution": "Node", + "target": "ESNext", + "module": "ESNext", + /* 模块导入配置项 */ + "esModuleInterop": true, + "types": ["../../types", "@types/jest"] + } +} diff --git a/packages/i18n/webpack.config.js b/packages/i18n/webpack.config.js new file mode 100644 index 00000000..a5d9a71b --- /dev/null +++ b/packages/i18n/webpack.config.js @@ -0,0 +1,11 @@ +const path = require('path'); +const config = require('../../webpack.config'); + +module.exports = { + ...config, + output: { + ...config.output, + library: 'I18n', + path: path.resolve(__dirname, 'dist'), + }, +}; diff --git a/tsconfig.json b/tsconfig.json index 565735e4..becba3ae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -34,6 +34,8 @@ "@arvinxu/heatmap-calendar/*": ["./packages/heatmap-calendar/src/*"], "@arvinxu/utils": ["./packages/utils/src"], "@arvinxu/utils/*": ["./packages/utils/src/*"], + "@arvinxu/i18n": ["./packages/i18n/src"], + "@arvinxu/i18n/*": ["./packages/i18n/src/*"], "@arvinxu/float-label-input": ["./packages/float-label-input/src"], "@arvinxu/page-loading": ["./packages/page-loading/src"], "@arvinxu/mindflow": ["./packages/mindflow/src"],