Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit bc0d1f1

Browse files
committed
🎉 chore: 初始化 i18n
1 parent fb9a613 commit bc0d1f1

File tree

12 files changed

+147
-0
lines changed

12 files changed

+147
-0
lines changed

jest.config.base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
'@arvinxu/user-panel': '<rootDir>/packages/user-panel/src',
1919
'@arvinxu/heatmap-calendar': '<rootDir>/packages/heatmap-calendar/src',
2020
'@arvinxu/utils': '<rootDir>/packages/utils/src',
21+
'@arvinxu/i18n': '<rootDir>/packages/i18n/src',
2122
'@arvinxu/float-label-input': '<rootDir>/packages/float-label-input/src',
2223
'@arvinxu/page-loading': '<rootDir>/packages/page-loading/src',
2324
'@arvinxu/mindflow': '<rootDir>/packages/mindflow/src',

packages/i18n/.fatherrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const base = require('../../.fatherrc');
2+
3+
module.exports = {
4+
...base,
5+
};

packages/i18n/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# @arvinxu/i18n
2+
3+
[![NPM version][version-image]][version-url] [![NPM downloads][download-image]][download-url]
4+
5+
## License
6+
7+
[MIT](../../LICENSE) ® Arvin Xu
8+
9+
<!-- npm url -->
10+
11+
[version-image]: http://img.shields.io/npm/v/@arvinxu/i18n.svg?color=deepgreen&label=latest
12+
[version-url]: http://npmjs.org/package/@arvinxu/i18n
13+
[download-image]: https://img.shields.io/npm/dm/@arvinxu/i18n.svg
14+
[download-url]: https://npmjs.org/package/@arvinxu/i18n

packages/i18n/jest.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const base = require('../../jest.config.base');
2+
3+
const packageName = '@arvinxu/i18n';
4+
5+
const root = '<rootDir>/packages/i18n';
6+
7+
module.exports = {
8+
...base,
9+
rootDir: '../..',
10+
roots: [root],
11+
name: packageName,
12+
displayName: packageName,
13+
collectCoverageFrom: [`${root}/src/**/*.tsx`, `${root}/src/**/*.ts`],
14+
};

packages/i18n/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@arvinxu/i18n",
3+
"version": "1.0.0",
4+
"files": [
5+
"lib",
6+
"es"
7+
],
8+
"main": "lib/index.js",
9+
"module": "es/index.js",
10+
"homepage": "https://github.com/arvinxx/components/tree/master/packages/i18n#readme",
11+
"repository": "git+https://github.com/arvinxx/components.git",
12+
"publishConfig": {
13+
"registry": "https://registry.npmjs.org",
14+
"access": "public"
15+
},
16+
"scripts": {
17+
"build": "father-build && yarn webpack",
18+
"webpack": "webpack",
19+
"test": "jest",
20+
"test:update": "jest -u",
21+
"prepublishOnly": "yarn build",
22+
"cov": "jest --coverage",
23+
"clean": "rm -rf es lib dist build coverage .umi"
24+
}
25+
}

packages/i18n/src/Intl.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { IntlProvider as IntlProvider_ } from 'react-intl';
3+
import type { IntlProviderProps } from './type';
4+
5+
export const IntlProvider: IntlProviderProps = (props) => (
6+
<IntlProvider_ {...props} />
7+
);

packages/i18n/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './Intl';
2+
export * from './useI18n';

packages/i18n/src/type.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ComponentProps, FC } from 'react';
2+
import type { IntlProvider } from 'react-intl';
3+
4+
export type LocalesType = 'en-US' | 'zh-CN';
5+
6+
export type LocaleKey = string;
7+
8+
export type IntlProviderProps<T = Record<string, string>> = FC<
9+
Omit<ComponentProps<typeof IntlProvider>, 'messages'> & {
10+
messages: T;
11+
}
12+
>;

packages/i18n/src/useI18n.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useEffect, useState } from 'react';
2+
import { useIntl } from 'react-intl';
3+
4+
import type { PrimitiveType } from 'intl-messageformat';
5+
import type { LocalesType, LocaleKey } from './type';
6+
7+
/**
8+
*
9+
*/
10+
export const useI18n = <T>(inputMessage: T) => {
11+
const [locale, setLocale] = useState<LocalesType>('zh-CN');
12+
const [messages, setMessages] = useState<T>(inputMessage);
13+
14+
useEffect(() => {
15+
switch (locale) {
16+
case 'en-US':
17+
setMessages(inputMessage);
18+
break;
19+
case 'zh-CN':
20+
default:
21+
setMessages(inputMessage);
22+
break;
23+
}
24+
}, [locale]);
25+
26+
const switchLocale = (l: LocalesType): void => {
27+
setLocale(l);
28+
};
29+
30+
return { locale, messages, switchLocale };
31+
};
32+
33+
export const useFormatMessage = (): ((
34+
id: LocaleKey,
35+
values?: Record<string, PrimitiveType>,
36+
) => string) => {
37+
const intl = useIntl();
38+
return (id, values) => intl.formatMessage({ id }, values);
39+
};

packages/i18n/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"jsx": "react-jsx",
6+
"skipLibCheck": true,
7+
/* babel 输出类型 */
8+
"moduleResolution": "Node",
9+
"target": "ESNext",
10+
"module": "ESNext",
11+
/* 模块导入配置项 */
12+
"esModuleInterop": true,
13+
"types": ["../../types", "@types/jest"]
14+
}
15+
}

packages/i18n/webpack.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const path = require('path');
2+
const config = require('../../webpack.config');
3+
4+
module.exports = {
5+
...config,
6+
output: {
7+
...config.output,
8+
library: 'I18n',
9+
path: path.resolve(__dirname, 'dist'),
10+
},
11+
};

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"@arvinxu/heatmap-calendar/*": ["./packages/heatmap-calendar/src/*"],
3535
"@arvinxu/utils": ["./packages/utils/src"],
3636
"@arvinxu/utils/*": ["./packages/utils/src/*"],
37+
"@arvinxu/i18n": ["./packages/i18n/src"],
38+
"@arvinxu/i18n/*": ["./packages/i18n/src/*"],
3739
"@arvinxu/float-label-input": ["./packages/float-label-input/src"],
3840
"@arvinxu/page-loading": ["./packages/page-loading/src"],
3941
"@arvinxu/mindflow": ["./packages/mindflow/src"],

0 commit comments

Comments
 (0)