Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change lang in code #525

Closed
ruanzy opened this issue Jul 8, 2016 · 1 comment
Closed

change lang in code #525

ruanzy opened this issue Jul 8, 2016 · 1 comment
Labels

Comments

@ruanzy
Copy link

ruanzy commented Jul 8, 2016

var langButton = React.createClass({ render: function () { return ( <button onClick={this.handleClick} >click</button> ) }, handleClick: function (event) { //How to change lang page's <FormattedMessage/> change auto } });

@ericf
Copy link
Collaborator

ericf commented Jul 8, 2016

By treating the current locale as application state.

Maybe something like this…

import React from 'react';
import ReactDOM from 'react-dom';
import {IntlProvider, FormattedMessage} from 'react-intl';

const messages = {
    'en': {
        'greeting': 'Hello',
    }

    'fr': {
        'greeting': 'Bonjour',
    }
};

class Root extends React.Component {
    constrcutor(props) {
        super(props);

        this.state = {
            locale: props.initialLocale,
        };

        this.updateLocale = this.updateLocale.bind(this);
    }

    updateLocale(locale) {
        this.setState({locale});
    }

    render() {
        const {locale} = this.state;

        return (
            <IntlProvider
                locale={locale}
                messages={messages[locale]}
            >
                <App updateLocale={this.updateLocale} />
            </IntlProvider>
        );
    }
}

Root.propTypes = {
    initialLocale: React.PropTypes.string.isRequired,
};

Root.defaultProps = {
    initialLocale: 'en',
};

const App = ({updateLocale}) => (
    <div>
        <p>
            <FormattedMessage id='greeting' />
        </p>
        <ul>
            <li>
                <a 
                    href="#" 
                    onClick={(e) => {
                        e.preventDefault();
                        updateLocale('fr');
                    }}
                >
                    French
                </a>
            </li>
            <li>
                <a 
                    href="#" 
                    onClick={(e) => {
                        e.preventDefault();
                        updateLocale('en');
                    }}
                >
                    English
                </a>
            </li>
        </ul>
    </div>
);

App.propTypes = {
    updateLocale: React.PropTypes.func.isRequired,
};

ReactDOM.render(
    <Root />,
    document.getElementsById('container')
);

@ericf ericf closed this as completed Jul 8, 2016
@ericf ericf added the support label Jul 8, 2016
longlho pushed a commit that referenced this issue Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants