Skip to content

Commit 8b87b67

Browse files
author
Joao Moura
committed
Added variables to translations, updated readme, updated example, version bump
1 parent 1eb91a8 commit 8b87b67

File tree

7 files changed

+89
-28
lines changed

7 files changed

+89
-28
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,55 @@ export default translate(SomeComponent)
4848

4949
## Injected Method
5050

51-
#### translate(key)
51+
If using the Higher Order Component `translate(SomeComponent)`
52+
53+
#### t(key, params)
5254

5355
| Params | Type | Description |
5456
| ------------- |:-------------:| ------------ |
55-
| key | string | translation key that identifies the text |
57+
| key | string | translation key that identifies the text |
58+
| params | object | {'param': 'value', ...} each param will be set on the string in its correct location |
59+
60+
61+
## Exported Methods
5662

63+
#### setDefaultTranslations(translations)
5764

58-
## Methods
65+
Sets the translations
66+
67+
| Params | Type | Description |
68+
| ------------- |:-------------:| ------------ |
69+
| translations | object | {'key': 'translations', ...} |
5970

6071
#### setTranslations(translations)
6172

73+
Same as setDefaultTranslations, but this will update all components using translations
74+
6275
| Params | Type | Description |
6376
| ------------- |:-------------:| ------------ |
6477
| translations | object | {'key': 'translations', ...} |
6578

6679
#### setDefaultLanguage(key)
6780

81+
Sets the default application language
82+
83+
| Params | Type | Description |
84+
| ------------- |:-------------:| ------------ |
85+
| key | string | translation key, in this example 'en' or 'pt' |
86+
87+
#### setLanguage(key)
88+
89+
Same as setDefaultLanguage, but this will update all components using translations
90+
6891
| Params | Type | Description |
6992
| ------------- |:-------------:| ------------ |
7093
| key | string | translation key, in this example 'en' or 'pt' |
94+
95+
#### t(key, params)
96+
97+
Get text function, will return the translated string
98+
99+
| Params | Type | Description |
100+
| ------------- |:-------------:| ------------ |
101+
| key | string | translation key that identifies the text |
102+
| params | object | {'param': 'value', ...} each param will be set on the string in its correct location |

example/component.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import * as React from 'react'
44

55
// Translation Higher Order Component
66
import { translate } from 'react-multi-lang'
7+
import type { T } from 'react-multi-lang'
78

89
type Props = {
9-
translate:(string) => string
10+
t: T
1011
}
1112

1213
type State = {}
1314

1415
class SomeComponent extends React.Component<Props, State> {
1516
render () {
16-
const { translate } = this.props
17+
const { t } = this.props
1718
return (
1819
<div>
19-
{translate('About Us')}
20+
{t('About Us')}
21+
{t('Hello', {name: 'João'})}
2022
</div>
2123
)
2224
}

example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import * as React from 'react'
44
import ReactDOM from 'react-dom'
55

66
// Translations
7-
import { setTranslations, setDefaultLanguage } from 'react-multi-lang'
7+
import { setDefaultTranslations, setDefaultLanguage } from 'react-multi-lang'
88
import pt from './translations/pt.json'
99
import en from './translations/en.json'
1010

1111
// Translated Component
1212
import TranslatedComponent from './component'
1313

14-
setTranslations({pt, en})
14+
setDefaultTranslations({pt, en})
1515
setDefaultLanguage('en')
1616

1717
ReactDOM.render(

example/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"Tides and Activity": "Tides and Activity",
44
"Operacional Forecast": "Operacional Forecast",
55
"About Us": "About Us",
6-
"Share": "Share"
6+
"Share": "Share",
7+
"Hello": "Hello {name}"
78
}

example/translations/pt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"Tides and Activity": "Marés e Actividade",
44
"Operacional Forecast": "Previsão operacional",
55
"About Us": "Sobre Nós",
6-
"Share": "Pratilhar"
6+
"Share": "Pratilhar",
7+
"Hello": "Ola {name}"
78
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-multi-lang",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "React Multilanguage Higher-Order Component",
55
"main": "lib/index.js",
66
"files": [

src/index.js

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ export type Translations = {
1818
}
1919
}
2020

21-
let translations: Translations = {}
22-
23-
export function setDefaultLanguage (lang: string): void {
24-
language = lang
25-
}
26-
27-
export function setTranslations (userTranslations: Translations): void {
28-
translations = userTranslations
29-
}
21+
export type T = (key: string, args?: {[string]: string}) => string
3022

31-
export function getText (key: string): string {
32-
return translations[language][key]
33-
}
23+
let translations: Translations = {}
3424

3525
export function subscribe (cb:() => mixed, id?: string): string {
3626
const newId = id || uuid.v1()
@@ -47,13 +37,46 @@ export function unsubscribe (id: string) {
4737
)
4838
}
4939

50-
export function setLanguage (lang: string) {
51-
language = lang
40+
function triggerSubscriptions (): void {
5241
subscribes.forEach(
5342
item => item.cb()
5443
)
5544
}
5645

46+
export function setDefaultLanguage (lang: string): void {
47+
language = lang
48+
}
49+
50+
export function setDefaultTranslations (userTranslations: Translations): void {
51+
if (Object.keys(translations).length !== 0) {
52+
setTranslations(userTranslations)
53+
return
54+
}
55+
translations = userTranslations
56+
}
57+
58+
export function setTranslations (userTranslations: Translations): void {
59+
translations = userTranslations
60+
triggerSubscriptions()
61+
}
62+
63+
export function setLanguage (lang: string) {
64+
language = lang
65+
triggerSubscriptions()
66+
}
67+
68+
export function t (key: string, args?: {[string]: string}): string {
69+
let translation = translations[language][key]
70+
if (args) {
71+
Object.keys(args).forEach(
72+
key => {
73+
translation = translation.replace(`{${key}}`, args[key])
74+
}
75+
)
76+
}
77+
return translation
78+
}
79+
5780
export function translate (Component: React$ComponentType<*>):React$ComponentType<*> {
5881
class TranslatedComponet extends React.Component<{}, *> {
5982
id: string
@@ -70,7 +93,7 @@ export function translate (Component: React$ComponentType<*>):React$ComponentTyp
7093

7194
render () {
7295
return (
73-
<Component {...this.props} translate={(key: string) => getText(key)}/>
96+
<Component {...this.props} t={(key: string, args?:{[string]: string}) => t(key, args)}/>
7497
)
7598
}
7699
}
@@ -79,10 +102,12 @@ export function translate (Component: React$ComponentType<*>):React$ComponentTyp
79102
}
80103

81104
export default {
105+
setDefaultLanguage,
82106
setLanguage,
107+
setDefaultTranslations,
108+
setTranslations,
83109
translate,
84110
subscribe,
85111
unsubscribe,
86-
getText,
87-
setTranslations
112+
t
88113
}

0 commit comments

Comments
 (0)