Skip to content

Commit 94b3b8b

Browse files
committed
Merge branch 'master' of github.com:JWebCoder/react-multi-lang
2 parents c5df8c5 + 5ac41e1 commit 94b3b8b

File tree

5 files changed

+108
-74
lines changed

5 files changed

+108
-74
lines changed

.vscode/settings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"spellright.language": [
3-
"en"
3+
"English (American)"
44
],
55
"spellright.documentTypes": [
6-
"markdown",
76
"latex",
8-
"plaintext"
7+
"plaintext",
8+
"markdown"
99
]
1010
}

README.md

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,56 +73,81 @@ class SomeComponent extends React.Component<Props> {
7373
export default withTranslation(SomeComponent)
7474
```
7575

76-
## Translation Method t(path, params)
76+
## Auto update functions
7777

78-
Params | Type | Description
79-
------ | ------ | ------------------------------------------------------------------------------------
80-
path | string | translation path that identifies the text
81-
params | object | {'param': 'value', ...} each param will be set on the string in its correct location
78+
### useTranslation(basePath)
79+
80+
React hook that returns the t function
81+
82+
Params | Type | Description | Required
83+
---- | ---- | ---- | ----
84+
basePath | string | translation base path used to identify all the next requested translations | no
85+
86+
### withTranslation(component, basePath)
87+
88+
HOC that injects the translation function into the component
89+
90+
Params | Type | Description | Required
91+
---- | ---- | ---- | ----
92+
component | React Component | React component that requires the translation function | yes
93+
basePath | string | translation base path used to identify all the next requested translations | no
94+
95+
## Translation Method
96+
97+
t(path, params)
98+
99+
Returns the translation for the requested path
100+
101+
Params | Type | Description | Required
102+
---- | ---- | ---- | ----
103+
path | string | translation path that identifies the text | yes
104+
params | object | {'param': 'value', ...} each parameter will be set on the string in its correct location | no
82105

83106
## Exported Methods
84107

85108
### setDefaultTranslations(translations)
86109

87110
Sets the translations
88111

89-
Params | Type | Description
90-
------------ | ------ | ----------------------------
91-
translations | object | {'key': 'translations', ...}
112+
Params | Type | Description | Required
113+
---- | ---- | ---- | ----
114+
translations | object | {'key': 'translations', ...} | yes
92115

93116
### setTranslations(translations)
94117

95118
Same as setDefaultTranslations, but this will update all components using translations
96119

97-
Params | Type | Description
98-
------------ | ------ | ----------------------------
99-
translations | object | {'key': 'translations', ...}
120+
Params | Type | Description | Required
121+
---- | ---- | ---- | ----
122+
translations | object | {'key': 'translations', ...} | yes
100123

101124
### setDefaultLanguage(key)
102125

103126
Sets the default application language
104127

105-
Params | Type | Description
106-
------ | ------ | ---------------------------------------------
107-
key | string | translation key, in this example 'en' or 'pt'
128+
Params | Type | Description | Required
129+
---- | ---- | ---- | ----
130+
key | string | translation key, in this example 'en' or 'pt' | yes
108131

109132
### setLanguage(key)
110133

111134
Same as setDefaultLanguage, but this will update all components using translations
112135

113-
Params | Type | Description
114-
------ | ------ | ---------------------------------------------
115-
key | string | translation key, in this example 'en' or 'pt'
136+
Params | Type | Description | Required
137+
---- | ---- | ---- | ----
138+
key | string | translation key, in this example 'en' or 'pt' | yes
116139

117140
### getLanguage()
118141

119142
Returns the current selected language
120143

121144
### t(key, params)
122145

123-
Get text function, will return the translated string
146+
t(path, params)
147+
148+
Returns the translation for the requested path
124149

125-
Params | Type | Description
126-
------ | ------ | ------------------------------------------------------------------------------------
127-
key | string | translation key that identifies the text
128-
params | object | {'param': 'value', ...} each param will be set on the string in its correct location
150+
Params | Type | Description | Required
151+
---- | ---- | ---- | ----
152+
path | string | translation path that identifies the text | yes
153+
params | object | {'param': 'value', ...} each parameter will be set on the string in its correct location | no

package-lock.json

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@types/react": "^16.8.20",
3737
"react": "^16.8.6",
3838
"rimraf": "^2.6.3",
39-
"tslint": "^5.17.0",
39+
"tslint": "^6.1.0",
4040
"typescript": "^3.5.2",
4141
"utility-types": "^3.7.0"
4242
},

src/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface ITranslation {
2020
[key: string]: string | ITranslation
2121
}
2222

23+
export interface ITranslationParams {[key: string]: string}
24+
2325
export interface ITranslations {
2426
[key: string]: ITranslation
2527
}
@@ -80,7 +82,7 @@ export function getLanguage(): string {
8082
return language
8183
}
8284

83-
export function t(path: string, args?: {[key: string]: string}): string {
85+
export function t(path: string, args?: ITranslationParams): string {
8486
const translationKeys: string[] = path.split('.')
8587
let translation: string = ''
8688
if (translations[language]) {
@@ -118,12 +120,13 @@ export function useTranslation(basePath?: string) {
118120
const subId = subscribe(() => forceUpdate())
119121
return () => unsubscribe(subId)
120122
}, [forceUpdate])
121-
return (path: string, args?: {[key: string]: string}) =>
123+
return (path: string, args?: ITranslationParams) =>
122124
t(basePath ? (basePath + '.' + path) : path, args)
123125
}
124126

125127
export function withTranslation<P extends ITranslate>(
126-
Component: React.ComponentType<Pick<P, SetDifference<keyof P, 't'>>>
128+
Component: React.ComponentType<Pick<P, SetDifference<keyof P, 't'>>>,
129+
basePath?: string
127130
): React.ComponentType<Subtract<P, ITranslate>> {
128131
class TranslatedComponent extends React.Component<Subtract<P, ITranslate>> {
129132
public id: number | undefined
@@ -139,7 +142,13 @@ export function withTranslation<P extends ITranslate>(
139142
}
140143

141144
public render() {
142-
return <Component {...this.props as P} t={t}/>
145+
return <Component
146+
{...this.props as P}
147+
t={
148+
(path: string, args?: ITranslationParams) =>
149+
t(basePath ? (basePath + '.' + path) : path, args)
150+
}
151+
/>
143152
}
144153
}
145154

0 commit comments

Comments
 (0)