Published: November 3, 2022
7 min read
In this article, you'll learn:
1
👍 Reasons to localize your app
2
🛠 Main approaches to implementing localization
3
⚙️ Implementation with Google Sheets, React Native, and i18n
4
💡 Takeaways
With each year, the number of people using mobile and web apps is rising. Because of this, there’s an increasing need for app content translated into different languages to reach as many people as possible.
This article will review some of the non-trivial reasons for localizing an app, several tools and approaches to it, and offer an easy solution for React Native using Google Sheets.
While the most obvious reason for translating the app content into different languages is to reach new audiences, reasons to localize your app are not limited just to this. Moreover, there are some benefits of app localization even if you’re using only 1 language within the application:
Image by Oleksandr Kaliberda
If you understand what benefits the app localization will give you, head over to the next section, where we’re going to outline some of the main ways to implement localization.
The simplest solution to implement localization is to store the locale files inside the repository itself, for example, in the JSON format. This way, you’ll have files like en.json
, nl.json
, de.json
inside some folder.
A common approach to localization is to use a localization platform that will manage & store the texts on a remote server. Most of the platforms already provide such functionality as repository linking, collaboration tools, 2-way synchronization of the translations, cross-platform integrations etc.
In this section, we’re going to review some of the most popular platforms and give their specific features:
Localazy's interface
Lokalise's interface
Phrase's interface
POEditor's interface
So, let’s outline some of the general pros & cons of using a localization platform:
Another solution is to use Google Sheets or something similar to store & edit the translations there.
Example of localization using Google Sheets
In some of our recent projects, we started using Google Sheets for localization — take a look at our Norsk Guardian project, which is a React Native app for checking the status of BLE-integrated batteries. To make this possible, we developed a library called [google-sheets-localization-helpers]{target=_blank rel="nofollow noopener"}, which you can also use in your own projects.
Now, we’re going to outline the main steps to implement localization with Google Sheets & React/React Native.
First of all, we need to create the spreadsheet itself, and get an API key in Google Console for that account (to get the key follow these steps). Let’s set up the following sheet structure with three languages and one tag column (will be used in the code):
Structure example
To use the data from the sheet in your React Native app you first need to install the dependencies:
Then, we’re going to set up a script that we can use to pull the translations from the sheet. Let’s call it syncLocales.ts and put it into the root of the project:
Here, you’ll need to configure the folder where the locale jsons will be saved. For our example, it would be in /src/strings. Also, you need to add GCP_API_KEY to your .env variables, as well as provide a value to the sheetId constant (the id can be found in the sheet URL).
Next, let’s set up calling this script automatically when the project is built. Go to package.json and add the following lines to the scripts field:
If you already have the postinstall key, you can just add && yarn localize to it. This way, the script will be called every time the project is built or a new dependency is installed, while also being able to pull the texts ad-hoc (i.e. after updating the sheet) by calling yarn localize.
The last remaining thing is to actually use these locales in the app. To do that, we’re going to use react-i18next. Let’s set up a file called i18next.ts and put the following code into it:
Here, you’ll probably want to configure the languages that you import, and the fallback language.
Now, import this file in App.tsx and we’re all set!
You can now use the translations in your components by getting the t function from useTranslation hook in the following way:
For more information on how to change the languages, as well as to get the language of the user device, please refer to react-i18next documentation.
Now, let’s summarize some of the main ideas of the article:
So, this is it! We hope you’ve liked the article, and in case you need any help with building localized Mobile or Web apps, feel free to reach out!
Was it helpful?
Read also
Our clients say
They were a delight to work with. And they delivered the product we wanted. Stormotion fostered an enjoyable work atmosphere and focused on delivering a bug-free solution.
David Lesser, CEO
Numina