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')
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.
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.
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.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With