One of the key features of any good mobile application is localization, which involves adapting the app to different languages and regions. This is where the react-native-i18n library comes in. This blog post will explore what react-native-i18n is, why it’s important, and how to use it in your React Native projects.
React Native Internationalization (react-native-i18n) is a library that provides support for internationalization and localization in React Native applications. It is designed to make it easy for React Native developers to add support for multiple languages and locales to their apps.
The library provides a simple API for managing translations, and it supports a wide range of features including plurals, gender-based translations, and right-to-left language support. Additionally, it integrates seamlessly with popular internationalization tools like ICU and gettext.
Localization is the process of adapting an application to meet the language and cultural needs of users in different regions. It is important because it allows your app to reach a wider audience and provide a better user experience for people who may not speak the same language as the app’s default language.
When you localize your app, you can translate the user interface, error messages, and other text elements into different languages. You can also adapt the app to regional preferences such as date and time formats, currency symbols, and measurement units. This can help you to increase user engagement and retention, and ultimately, drive more revenue for your app.
Related read: How to Implement React Native Multi Language Support?
To use react-native-i18n, you first need to install the library using npm:
npm install –save react-native-i18n
Next, you need to import the library and set it up in your app. This involves configuring the available languages and locales and defining the translation keys and values.
Related read: How To Create NPM Package For React Native?
Step 1: Create an i18n.js file at the same location where your app’s index.js is present
Here’s an example setup code:
import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import locales from './src/translations/locales'; // the translations // (tip move them in a JSON file and import them, // or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files) i18n .use(initReactI18next) // passes i18n down to react-i18next .init({ compatibilityJSON: 'v3', resources: { ...locales, // here added multiple lang JSON files }, fallbackLng: 'en', lng: 'en', // language to use, more information here: https://www.i18next.com/overview/configuration-options#languages-namespaces-resources // you can use the i18n.changeLanguage function to change the language manually: https://www.i18next.com/overview/api#changelanguage // if you're using a language detector, do not define the lng option interpolation: { escapeValue: false, // react already safes from xss }, }); export default i18n;
In this example, we’ve imported multiple lang JSON files and added them under the resources and set English as the current language.in the next step, we create multiple lang JSON files.
Step 2: Create a translations folder under your app’s src and add an index.js file
Here your index.js file looks like this:
import en from './en'; import hn from './hn'; export default { en, hn };
In this example, we’ve imported two files first one is en.js, and the other hn.js. en.js file contains English translation JSON and hn.js contains Hindi translation JSON file. here you can create multiple local translations JSON files ex. Kanada, Telugu, etc
Here’s an example code for en.js:
export default { translation: { all: { submit: 'submit', switchThemeText: 'Switch Theme', }, }, };
Here’s an example code for hn.js:-
export default { translation: { all: { submit: 'प्रस्तुत', switchThemeText: 'थीम स्विच करें', }, }, };
In this example, we’ve defined two languages (English and Hindi) and set English as the current language. We’ve also defined two translation keys (submit and switchThemeText) and assigned them different values for each language.
Step 3: Last and final step is to add our localization configuration to our application this can be done in one single step you just need to import our i18n.js configuration file into the App.js
Here’s an example code:
import React, { useEffect } from 'react'; import AppContainer from './src/containers/AppContainer'; import { Provider } from 'react-redux'; import configureStore from './src/redux/store'; import crashlytics from '@react-native-firebase/crashlytics'; import './i18n'; // here is the i18n config file added const App = () => { try { return ( <Provider store={configureStore()}> <AppContainer /> </Provider> ); } catch (error) { crashlytics().log('Error in App.'); crashlytics().recordError(error); } }; export default App;
Once you’ve set up react-native-i18n, you can use it to translate text in your app using the I18n.t() function or using useTranslation() hook.
Here’s an example:
import React from 'react'; import { Text } from 'react-native'; import { useTranslation } from 'react-i18next'; const Greeting = () => { const { t } = useTranslation(); // here is the useTranslation() hook that provides translation function 't' and using that function we can translate our key values return <Text>{t('translation:all:submit')}</Text>; }; export default Greeting;
In this example, we’re rendering a simple Text component with the translated value of the submit key. Also, we can provide a user option to change lang as per their choice and this can be done using the changeLanguage() function.
Here’s an example:
import React from 'react'; import { Text, TouchableOpacity, View } from 'react-native'; import { useTranslation } from 'react-i18next'; const Greeting = () => { const { t, i18n } = useTranslation(); // here is the useTranslation() hook that provides translation function 't' and using that function we can translate our key values const onChangeLang = () => { i18n.changeLanguage('hn'); // here I provided the 'hn' hind as lang option to changeLanguage(). here all your text converted to the Hindi language }; return ( <View> <Text>{t('translation:all:submit')}</Text> <TouchableOpacity onPress={() => onChangeLang()}> <Text>Change language</Text> </TouchableOpacity> </View> ); }; export default Greeting;
In this example, we change the language using the i18s changeLanguage() function and here I have provided a hard-coded ‘hn’(Hindi) language option, you can also create dynamic functionality for changing languages of the user’s choices and pass that value to changeLanguage() function. when we call the changeLanguage() function all your app text is translated to your selected lang.
React Native Internationalization (react-native-i18n) is a powerful library for adding support for multiple languages and locales to your React Native apps. By using it, you can provide a better user experience for people who speak different languages, and ultimately, increase engagement and revenue for your app. With a little setup and configuration, you can easily translate your app’s UI and text elements, and adapt them to different regional preferences.
Nandkishor Shinde is a React Native Developer with 5+ years of experience. With a primary focus on emerging technologies like React Native and React.js. His expertise spans across the domains of Blockchain and e-commerce, where he has actively contributed and gained valuable insights. His passion for learning is evident as he always remains open to acquiring new knowledge and skills.
React-native-i18n is a JavaScript library for internationalization (i18n) in React Native applications. It provides tools and utilities to make it easier to translate and localize your mobile app for different languages and regions.
Localization is important because it allows products and content to reach a global audience by adapting to different languages and cultures, enhancing user experience and trust while potentially increasing revenue and market competitiveness.
The three types of localization are Linguistic Localization, Cultural Localization and Technical Localization.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork