Skip to content

Commit 06b46ef

Browse files
update example and docs with new variable insertion info
1 parent 5f2e304 commit 06b46ef

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ a property for each supported language, where the property name would match the
9292
```json
9393
{
9494
"en": {
95-
"greeting": "Hello",
95+
"greeting": "Hello ${ firstName }",
9696
"farwell": "Goodbye"
9797
},
9898
"fr": {
99-
"greeting": "Bonjour",
99+
"greeting": "Bonjour ${ firstName }",
100100
"farwell": "Au revoir"
101101
},
102102
"es": {
103-
"greeting": "Hola",
103+
"greeting": "Hola ${ firstName }",
104104
"farwell": "Adiós"
105105
}
106106
}
@@ -110,15 +110,15 @@ all components decorated with `localize` have access to `global` translations. I
110110
component class, but return a new localized component with additional props `translate` and `currentLanguage`.
111111

112112
The `translate` prop is a function that takes the unique id from the transaltion file as a param,
113-
and will return the localized string based on `currentLanguage`.
113+
and an optional `data` param for variable substitutions. The function will return the localized string based on `currentLanguage`.
114114

115115
```javascript
116116
import React from 'react';
117117
import { localize } from 'react-localize-redux';
118118

119119
const Greeting = ({ translate, currentLanguage }) => (
120120
<div>
121-
<h1>{ translate('greeting') }</h1>
121+
<h1>{ translate('greeting', { name: 'Ryan' }) }</h1>
122122
<p>The current language is { `${ currentLanguage }` }</p>
123123
<button>{ translate('farwell') }</button>
124124
</div>
@@ -213,11 +213,22 @@ The following additional props are provided to localized components:
213213

214214
The current language set in your application. See [updateLanguage]() on how to update current language.
215215

216-
#### translate( id )
216+
#### translate( id, data )
217217

218218
The translate will be used to insert translated copy in your component. The `id` param will need to match the property of the string you wish
219219
to retrieve from your json translaion data.
220220

221+
The `data` param is optional and can be used to insert dynamic data into your translations. The syntax follows the Javascript template
222+
literal format.
223+
224+
```javascript
225+
// Here is a string where I want to dynamically insert the user's name, and country
226+
{ "greet": "Hi here is my ${ name } and ${ country }" }
227+
228+
// With translate you'd do the following
229+
translate('greet', { name: 'Ryan', country: 'Canada' })
230+
```
231+
221232
For example if the below json file was added using either [setGlobalTranslations](#setglobaltranslationsjson) or [setLocalTranslations](#setlocaltranslationsid-json).
222233

223234
```json
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"en": {
3-
"title": "Hello from Welcome Page!",
3+
"title": "Hello ${ name } from Welcome Page!",
44
"body": "Here is some body copy for the welcome page that needs to be translated. You can add <strong>strong</strong>, <u>underline</u>, or any other HTML tags you want to include in translations."
55
},
66
"fr": {
7-
"title": "Bonjour de la Page d’accueil !",
7+
"title": "Bonjour ${ name } de la Page d’accueil !",
88
"body": "Voici une copie de corps pour la page d’accueil qui doit se traduire. Vous pouvez ajouter fort, soulignement, ou autre éléments HTML tags vous souhaitez inclure dans les traductions."
99
},
1010
"es": {
11-
"title": "Hola desde la página de bienvenida!",
11+
"title": "Hola ${ name } desde la página de bienvenida!",
1212
"body": "Aquí le damos algunas copia del cuerpo de la página de bienvenida que necesita ser traducido. Usted puede Agregar fuerte, subrayado, o cualquier otro HTML tags te desea incluir en las traducciones."
1313
}
1414
}

examples/dynamic-routes/components/WelcomeView.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
const WelcomeView = ({ translate }) => {
44
return (
55
<div>
6-
<h1>{ translate('title') }</h1>
6+
<h1>{ translate('title', { name: 'Ryan Johsnon' }) }</h1>
77
<p>{ translate('body') }</p>
88
<button>{ translate('click-here') }</button>
99
</div>

0 commit comments

Comments
 (0)