Skip to content

Commit 0d6a240

Browse files
committed
added readme for locale switch
1 parent 3fca5d4 commit 0d6a240

File tree

1 file changed

+103
-16
lines changed

1 file changed

+103
-16
lines changed

README.md

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,85 @@ The result will be
420420
### Locale switch
421421

422422
We are providing a component for the locale switch. It is a select with everything what do you need.
423+
You can use it in two ways. Here is first example.
423424

424425
```js
425-
import React from 'react';
426+
import React, { Component } from 'react';
426427
import Translate { LocaleProvider, LocaleSwitch } from 'react-translate-maker';
427428

428-
const currentLocale = 'en_US';
429+
const data = {
430+
en_US: {
431+
language: 'Language',
432+
button: {
433+
login: 'Log In',
434+
signup: 'Sign Up'
435+
}
436+
},
437+
sk_SK: {
438+
language: 'Jazyk',
439+
button: {
440+
login: 'Prihlasit sa',
441+
signup: 'Odhlasit sa'
442+
}
443+
}
444+
};
445+
446+
const locales = [{
447+
label: 'English',
448+
value: 'en_US'
449+
}, {
450+
label: 'Slovenčina',
451+
value: 'sk_SK'
452+
}];
453+
454+
const DEFAULT_LOCALE = 'en_US';
455+
456+
class App extends Component {
457+
constructor(props, context) {
458+
super(props, context);
459+
460+
this.state = {
461+
locale: DEFAULT_LOCALE
462+
};
463+
}
464+
465+
handleLocaleChange(locale) {
466+
this.setState({
467+
locale: locale
468+
});
469+
}
470+
471+
render() {
472+
const { data, locales } = this.props;
473+
const currentLocale = this.state.locale;
474+
475+
return (
476+
<LocaleProvider adapter={data} locale={currentLocale}>
477+
<nav>
478+
<ul>
479+
<li><Translate path="button.login" /></li>
480+
<li><Translate path="button.signup" /></li>
481+
<li>
482+
<Translate path="language" />
483+
<LocaleSwitch locales={locales} onChange={this.handleLocaleChange.bind(this)}/>
484+
</li>
485+
</ul>
486+
</nav>
487+
</LocaleProvider>
488+
);
489+
}
490+
}
491+
492+
React.render(<App data={data} locales={locales}/>);
493+
```
494+
495+
As you can see in previous example. We are using onChange event. The main reason for that is that LocaleProvider is a [controlled](https://facebook.github.io/react/docs/forms.html#why-controlled-components) component. That means if you wanted to change property named locale of the LocaleProvider you need to change it directly in the render function.
496+
497+
If you want to use LocaleProvider as uncontrolled component you can do that. But all properties of the LocaleProvider will be used only as initialisation properties. Here is a second example (please take a loon on the property named controlled of the LocaleProvider).
498+
499+
```js
500+
import React, { Component } from 'react';
501+
import Translate { LocaleProvider, LocaleSwitch } from 'react-translate-maker';
429502

430503
const data = {
431504
en_US: {
@@ -452,22 +525,35 @@ const locales = [{
452525
value: 'sk_SK'
453526
}];
454527

455-
React.render(
456-
<LocaleProvider adapter={data} locale={currentLocale}>
457-
<nav>
458-
<ul>
459-
<li><Translate path="button.login" /></li>
460-
<li><Translate path="button.signup" /></li>
461-
<li>
462-
<Translate path="language" />
463-
<LocaleSwitch locales={locales} />
464-
</li>
465-
</ul>
466-
</nav>
467-
</LocaleProvider>
468-
);
528+
const DEFAULT_LOCALE = 'en_US';
529+
530+
class App extends Component {
531+
render() {
532+
const { data, locales } = this.props;
533+
const currentLocale = this.state.locale;
534+
535+
return (
536+
<LocaleProvider adapter={data} locale={DEFAULT_LOCALE} controlled={false}>
537+
<nav>
538+
<ul>
539+
<li><Translate path="button.login" /></li>
540+
<li><Translate path="button.signup" /></li>
541+
<li>
542+
<Translate path="language" />
543+
<LocaleSwitch locales={locales}/>
544+
</li>
545+
</ul>
546+
</nav>
547+
</LocaleProvider>
548+
);
549+
}
550+
}
551+
552+
React.render(<App data={data} locales={locales}/>);
469553
```
470554

555+
The main difference is that you are not able to change locale of the LocaleProvider with property named locale after first render.
556+
471557
#### Properties of the LocaleSwitch
472558

473559
- **onChange** (function): Callback witch a new locale
@@ -528,6 +614,7 @@ The result will be
528614
- **combinations** (Boolean): You can turn on/off combinations. (Default true)
529615
- **defaultValue** (Function): What you will see for missing translations (Default (path, attrs) => `Missing default translation for: ${path}`)
530616
- **filters** (Object): Object with custom filters
617+
- **controlled** (Boolean): You can set component as uncontrolled (default true). More [information](https://facebook.github.io/react/docs/forms.html#why-controlled-components).
531618

532619
### More examples
533620

0 commit comments

Comments
 (0)