generated from arvinxx/monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require('../../.fatherrc'); | ||
|
||
module.exports = { | ||
...base, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Intl'; | ||
export * from './useI18n'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters