Skip to content

Commit 727d7ba

Browse files
author
Joao Moura
committed
Added nested translations to avoid name colision
1 parent a64a252 commit 727d7ba

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SomeComponent extends React.Component<Props, State> {
3838
const { translate } = this.props
3939
return (
4040
<div>
41+
{t('home.Title')}
4142
{t('Hello', {name: 'João'})}
4243
</div>
4344
)
@@ -51,53 +52,52 @@ export default translate(SomeComponent)
5152

5253
If using the Higher Order Component `translate(SomeComponent)`
5354

54-
#### t(key, params)
55-
56-
| Params | Type | Description |
57-
| ------------- |:-------------:| ------------ |
58-
| key | string | translation key that identifies the text |
59-
| params | object | {'param': 'value', ...} each param will be set on the string in its correct location |
55+
### t(path, params)
6056

57+
Params | Type | Description
58+
------ | ------ | ------------------------------------------------------------------------------------
59+
path | string | translation path that identifies the text
60+
params | object | {'param': 'value', ...} each param will be set on the string in its correct location
6161

6262
## Exported Methods
6363

64-
#### setDefaultTranslations(translations)
64+
### setDefaultTranslations(translations)
6565

6666
Sets the translations
6767

68-
| Params | Type | Description |
69-
| ------------- |:-------------:| ------------ |
70-
| translations | object | {'key': 'translations', ...} |
68+
Params | Type | Description
69+
------------ | ------ | ----------------------------
70+
translations | object | {'key': 'translations', ...}
7171

72-
#### setTranslations(translations)
72+
### setTranslations(translations)
7373

7474
Same as setDefaultTranslations, but this will update all components using translations
7575

76-
| Params | Type | Description |
77-
| ------------- |:-------------:| ------------ |
78-
| translations | object | {'key': 'translations', ...} |
76+
Params | Type | Description
77+
------------ | ------ | ----------------------------
78+
translations | object | {'key': 'translations', ...}
7979

80-
#### setDefaultLanguage(key)
80+
### setDefaultLanguage(key)
8181

8282
Sets the default application language
8383

84-
| Params | Type | Description |
85-
| ------------- |:-------------:| ------------ |
86-
| key | string | translation key, in this example 'en' or 'pt' |
84+
Params | Type | Description
85+
------ | ------ | ---------------------------------------------
86+
key | string | translation key, in this example 'en' or 'pt'
8787

88-
#### setLanguage(key)
88+
### setLanguage(key)
8989

9090
Same as setDefaultLanguage, but this will update all components using translations
9191

92-
| Params | Type | Description |
93-
| ------------- |:-------------:| ------------ |
94-
| key | string | translation key, in this example 'en' or 'pt' |
92+
Params | Type | Description
93+
------ | ------ | ---------------------------------------------
94+
key | string | translation key, in this example 'en' or 'pt'
9595

96-
#### t(key, params)
96+
### t(key, params)
9797

9898
Get text function, will return the translated string
9999

100-
| Params | Type | Description |
101-
| ------------- |:-------------:| ------------ |
102-
| key | string | translation key that identifies the text |
103-
| params | object | {'param': 'value', ...} each param will be set on the string in its correct location |
100+
Params | Type | Description
101+
------ | ------ | ------------------------------------------------------------------------------------
102+
key | string | translation key that identifies the text
103+
params | object | {'param': 'value', ...} each param will be set on the string in its correct location

example/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SomeComponent extends React.Component<Props, State> {
1919
<div>
2020
{t('About Us')}
2121
{t('Hello', {name: 'João'})}
22+
{t('home.Title')}
2223
</div>
2324
)
2425
}

example/translations/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"Operacional Forecast": "Operacional Forecast",
55
"About Us": "About Us",
66
"Share": "Share",
7-
"Hello": "Hello {name}"
7+
"Hello": "Hello {name}",
8+
"home": {
9+
"Title": "Translation level 2"
10+
}
811
}

example/translations/pt.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"Operacional Forecast": "Previsão operacional",
55
"About Us": "Sobre Nós",
66
"Share": "Pratilhar",
7-
"Hello": "Ola {name}"
7+
"Hello": "Ola {name}",
8+
"home": {
9+
"Title": "Tradução nivel 2"
10+
}
811
}

src/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ type Subscription = {
1515

1616
let subscribes: Subscription[] = []
1717

18+
type Translation = {
19+
[string]: string | Translation
20+
}
21+
1822
export type Translations = {
19-
[string]: {
20-
[string]: string
21-
}
23+
[string]: Translation
2224
}
2325

24-
export type T = (key: string, args?: {[string]: string}) => string
26+
export type T = (path: string, args?: {[string]: string}) => string
2527

2628
let translations: Translations = {}
2729

@@ -70,15 +72,30 @@ export function setLanguage (lang: string) {
7072
triggerSubscriptions()
7173
}
7274

73-
export function t (key: string, args?: {[string]: string}): string {
74-
let translation = translations[language][key]
75-
if (args) {
76-
Object.keys(args).forEach(
77-
key => {
78-
translation = translation.replace(`{${key}}`, args ? args[key]: '')
75+
export function t (path: string, args?: {[string]: string}): string {
76+
const translationKeys: string[] = path.split('.')
77+
let translation: string = ''
78+
let translationObj: Translation = translations[language]
79+
80+
translationKeys.forEach(
81+
(key: string) => {
82+
const temp: string | Translation = translationObj[key]
83+
if (typeof temp === 'string') {
84+
translation = temp
7985
}
80-
)
86+
}
87+
)
88+
89+
if (translation) {
90+
if (args) {
91+
Object.keys(args).forEach(
92+
key => {
93+
translation = translation.replace(`{${key}}`, args ? args[key]: '')
94+
}
95+
)
96+
}
8197
}
98+
8299
return translation
83100
}
84101

0 commit comments

Comments
 (0)