How to build multilingual React.js application
Creating a suitable React.js sample to try out Gitloc
If you want to create your project from scratch, then you can use the following step-by-step guide, where we will create a multilingual React.js sample project using i18next and Gitloc
Create a React Application (more at reactjs.org and create-react-app.dev)
Prerequisites
Make sure you have Node.js and npm installed. You’ll need to have Node >= 14 on your local development machine. You can use nvm (macOS/Linux) or nvm-windows to switch Node versions between different projects.
It's best, if you have some experience with simple HTML, JavaScript and basic React.js, before jumping to localization. This react localization example is not intended to be a React beginner tutorial.
Now let's create a new react project named "my-app" with create-react-app.
To create a new app, you may choose one of the following methods:
*(npx comes with npm 5.2+ and higher, see instructions for older npm versions)
*npm init <initializer>
is available in npm 6+
*yarn create
is available in Yarn 0.25+
Running any of these commands will create a directory called my-app
inside the current folder. Inside that directory, it will generate the initial project structure and install the transitive dependencies:
No configuration or complicated folder structures, only the files you need to build your app. Once the installation is done, you can open your project folder:
Inside the newly created project, you can run some built-in commands:
npm start
or yarn start
runs the app in development mode. Open http://localhost:3000 to view it in the browser. The page will automatically reload if you make changes to the code. You will see the build errors and lint warnings in the console.
npm test
or yarn test
Runs the test watcher in an interactive mode. By default, runs tests related to files changed since the last commit (Read more about testing).
npm run build
or yarn build
Builds the app for production to the build
folder. It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes. Your app is ready to be deployed.
Setup i18next (more at react.i18next.com)
In our example, we will create a project with 2 languages and separate files for translations.
Let's install some i18next dependencies:
Let's prepare an i18n.js
file:
Import this file in our index.js
file:
Place translations into dedicated .json
files in the public
folder:
Now define a language switcher component:
And place it in the app
Add translations usage (more at i18next.com and react.i18next.com)
Simple content can easily be translated using the provided t
function. For the first text we just use a simple test
key to directly invoke it.
You will get the t
function by using the useTranslation hook or withTranslation hoc.
Now let's see how you can work with different namespaces. For the second text we will use thetitle
key form welcome
namespace.
For the third text, we will interpolate description
key form common
namespace by using the <Trans>
component. These can be referenced as <1>{{someSlot}}</1>
in the translations
As long you have no React/HTML nodes integrated into a cohesive sentence (text formatting like strong
, em
, link components, maybe others), you won't need it - most of the times you will be using the good old t
function.
Finally, сonnect your local project folder to your new repository on GitHub.
Done! Now you can move to the next step - connect remote repository to Gitloc
Last updated