13

I want to use ICU standard for my i18n in my react app. I want to store my language files like http://userguide.icu-project.org/locale/localizing#TOC-.txt-resource-bundles :

de {
  key1 { "Deutsche Sprache "
         "schwere Sprache" }
  key2 { "Düsseldorf" }
}

I've found this library http://formatjs.io/react/. http://formatjs.io/ supports ICU, however I can't find any good example how to wire my language files with my app.

I was going through their tutorial and it seems I could use the component <FormattedMessage>. So e.g.

var intlData = {
    "locales": "en-US",
    "messages": {
        "photos": "{name} took {numPhotos, plural,\n  =0 {no photos}\n  =1 {one photo}\n  other {# photos}\n} on {takenDate, date, long}.\n"
    }
};

React.render(
    <Component {...intlData} />,
    document.getElementById('example')
);

then in some component I have

...
render: function () {
        return (
            <p>
                <FormattedMessage
                    message={this.getIntlMessage('photos')}
                    name="Annie"
                    numPhotos={1000}
                    takenDate={Date.now()} />
            </p>
        );
    }

My biggest problem is how to convert my language file e.g.

en-US {
  photos { "{name} took {numPhotos, plural,\n  =0 {no photos}\n  =1 {one photo}\n  other {# photos}\n} on {takenDate, date, long}.\n" }
}

into format:

var intlData = {
    "locales": "en-US",
    "messages": {
        "photos": "{name} took {numPhotos, plural,\n  =0 {no photos}\n  =1 {one photo}\n  other {# photos}\n} on {takenDate, date, long}.\n"
    }
};

Is there any parser/converter at all?

1
  • why struggling with icu message format if you can have react.i18next.com ;) Commented Sep 18, 2017 at 12:38

1 Answer 1

6

You should check this repository https://github.com/gpbl/isomorphic500. In the intl subdirectory there are the input files for the different languages.

You can also see which type of parsing they adopt! Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing this example application out. It's a very good example of how to utilise FormatJS in a Flux/Fluxible application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.