Skip to content

Commit

Permalink
🎉 chore: 初始化 i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 6, 2021
1 parent fb9a613 commit bc0d1f1
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
'@arvinxu/user-panel': '<rootDir>/packages/user-panel/src',
'@arvinxu/heatmap-calendar': '<rootDir>/packages/heatmap-calendar/src',
'@arvinxu/utils': '<rootDir>/packages/utils/src',
'@arvinxu/i18n': '<rootDir>/packages/i18n/src',
'@arvinxu/float-label-input': '<rootDir>/packages/float-label-input/src',
'@arvinxu/page-loading': '<rootDir>/packages/page-loading/src',
'@arvinxu/mindflow': '<rootDir>/packages/mindflow/src',
Expand Down
5 changes: 5 additions & 0 deletions packages/i18n/.fatherrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const base = require('../../.fatherrc');

module.exports = {
...base,
};
14 changes: 14 additions & 0 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# @arvinxu/i18n

[![NPM version][version-image]][version-url] [![NPM downloads][download-image]][download-url]

## License

[MIT](../../LICENSE) ® Arvin Xu

<!-- npm url -->

[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
14 changes: 14 additions & 0 deletions packages/i18n/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const base = require('../../jest.config.base');

const packageName = '@arvinxu/i18n';

const root = '<rootDir>/packages/i18n';

module.exports = {
...base,
rootDir: '../..',
roots: [root],
name: packageName,
displayName: packageName,
collectCoverageFrom: [`${root}/src/**/*.tsx`, `${root}/src/**/*.ts`],
};
25 changes: 25 additions & 0 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions packages/i18n/src/Intl.tsx
Original file line number Diff line number Diff line change
@@ -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) => (
<IntlProvider_ {...props} />
);
2 changes: 2 additions & 0 deletions packages/i18n/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Intl';
export * from './useI18n';
12 changes: 12 additions & 0 deletions packages/i18n/src/type.ts
Original file line number Diff line number Diff line change
@@ -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<T = Record<string, string>> = FC<
Omit<ComponentProps<typeof IntlProvider>, 'messages'> & {
messages: T;
}
>;
39 changes: 39 additions & 0 deletions packages/i18n/src/useI18n.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(inputMessage: T) => {
const [locale, setLocale] = useState<LocalesType>('zh-CN');
const [messages, setMessages] = useState<T>(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, PrimitiveType>,
) => string) => {
const intl = useIntl();
return (id, values) => intl.formatMessage({ id }, values);
};
15 changes: 15 additions & 0 deletions packages/i18n/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
11 changes: 11 additions & 0 deletions packages/i18n/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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'),
},
};
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit bc0d1f1

Please sign in to comment.