Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multilingual website in ReactJS

I'm searching for a way to translate a complete website (Homepage, about us, product pages, contact, FAQ, ..) in multiple languages. The website is build in ReactJS with Firebase Database.

All the examples I found are just small translations like a greeting or How are you but what with complete websites? Is my best option making an JS object for each language and work with hundred of maybe thousand template strings? (That should definitely end up with tags like 'homepageContactSectionSubDiscription' or 'homepageProductSectionFeaturesItemTwo')

like image 354
Thore Avatar asked Aug 04 '17 17:08

Thore


People also ask

Can I use React for multi page website?

The use cases for having multiple pages in a single React app are pretty simple. You can create a website, and easily classify different types of content on different pages. But, it should also be understood that the default implementation of React is made to use a single HTML file, and this is by design.

How do I change the language on my website in React?

Changing the Language of the WebsiteCreate the file src/LocaleContext. js with the following content: import React from "react"; const defaultValue = { locale: 'en', setLocale: () => {} } export default React.

How do you serve a page with content in multiple languages React?

To change the language, just simply set the lang attribute. We can define it anywhere in the document, such as in the body, in the paragraph, in the heading, or in the span tag. But the best practice is to set the lang in the span tag.


1 Answers

You should look into using react-intl. Github

You can specify various language files:

en.json

{
    "greeting": "Hello"
}

es.json

{
    "greeting": "Hola"
}

And then use them within your components using FormattedMessage:

import { FormattedMessage } from 'react-intl';
...
<FormattedMessage id="greeting" />

React intl provides a wrapper for your application to allow specifying different languages and their associated messages.

ReactDOM.render(
    <IntlProvider locale="en">
        <App />
    </IntlProvider>,
    document.getElementById('app')
);

An important note, this will not perform translations for you, but provide you with a method of pulling from different language files based on the specified locale.

If you find yourself having a very large language file, you can split it up into different sections. For example, I may have two files, home.json and settings.json, which I can set up to access as home.{unique_id} or settings.{another_unique_id}

like image 113
Wolfie Avatar answered Nov 03 '22 00:11

Wolfie