This repository was archived by the owner on Apr 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 12 files changed +147
-0
lines changed Expand file tree Collapse file tree 12 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ module.exports = {
18
18
'@arvinxu/user-panel' : '<rootDir>/packages/user-panel/src' ,
19
19
'@arvinxu/heatmap-calendar' : '<rootDir>/packages/heatmap-calendar/src' ,
20
20
'@arvinxu/utils' : '<rootDir>/packages/utils/src' ,
21
+ '@arvinxu/i18n' : '<rootDir>/packages/i18n/src' ,
21
22
'@arvinxu/float-label-input' : '<rootDir>/packages/float-label-input/src' ,
22
23
'@arvinxu/page-loading' : '<rootDir>/packages/page-loading/src' ,
23
24
'@arvinxu/mindflow' : '<rootDir>/packages/mindflow/src' ,
Original file line number Diff line number Diff line change
1
+ const base = require ( '../../.fatherrc' ) ;
2
+
3
+ module . exports = {
4
+ ...base ,
5
+ } ;
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ ) ;
Original file line number Diff line number Diff line change
1
+ export * from './Intl' ;
2
+ export * from './useI18n' ;
Original file line number Diff line number Diff line change
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
+ > ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 34
34
"@arvinxu/heatmap-calendar/*" : [" ./packages/heatmap-calendar/src/*" ],
35
35
"@arvinxu/utils" : [" ./packages/utils/src" ],
36
36
"@arvinxu/utils/*" : [" ./packages/utils/src/*" ],
37
+ "@arvinxu/i18n" : [" ./packages/i18n/src" ],
38
+ "@arvinxu/i18n/*" : [" ./packages/i18n/src/*" ],
37
39
"@arvinxu/float-label-input" : [" ./packages/float-label-input/src" ],
38
40
"@arvinxu/page-loading" : [" ./packages/page-loading/src" ],
39
41
"@arvinxu/mindflow" : [" ./packages/mindflow/src" ],
You can’t perform that action at this time.
0 commit comments