Expo Preset
Use-case guide: See the Expo translation workflow โ for a step-by-step setup focused on shipping faster.
The Expo preset helps you set up internationalization (i18n) in your Expo project with minimal configuration. It integrates with Expo's localization system and sets up all necessary files and configurations for managing translations.
Prerequisites
- โAn Expo project
- โGit initialized repository
- โNode.js and npm/yarn/pnpm
Getting Started
- โNavigate to your Expo project root and run:
npx @babely/cli@latest init --preset expo
- โConfigure your source and target languages when prompted, or create a
babely.jsonfile:
{ "locale": { "source": "en", "targets": ["es", "fr"] } }
What Gets Set Up
The preset will:
- โ
Install required dependencies:
- โ
i18n-js- For handling translations - โ
expo-localization- For detecting device locale
- โ
- โ
Create the following directory structure:
Structurelocales/ โโโ i18n.ts # i18n configuration โโโ en.json # Source language translations โโโ [lang].json # Target language translations โโโ native/ # App metadata translations โ โโโ en.json โ โโโ [lang].json โโโ README.md # Usage instructions - โ
Configure your
app.jsonfor localization support:- โEnables mixed localizations for iOS
- โAdds expo-localization plugin
- โSets up native localization paths
Usage
Basic Translation
Import the i18n instance in your components:
import i18n from './locales/i18n'; function Welcome() { return <Text>{i18n.t('welcome')}</Text>; }
Translation Files
The preset creates two types of translation files:
- โ
Regular translations (
locales/[lang].json):locales/[lang].json{ "welcome": "Welcome to my app", "hello": "Hello", "settings": "Settings" } - โ
Native translations (
locales/native/[lang].json):locales/native/[lang].json{ "CFBundleDisplayName": "My App", "NSContactsUsageDescription": "We need access to contacts..." }
Managing Translations
- โAdd new translation keys to your source language file (
locales/[source].json) - โRun the translation command:
Terminal
npx @babely/cli@latest translate
Best Practices
- โAlways use translation keys in your code instead of hardcoded strings
- โKeep translation keys organized and descriptive
- โUse the native translations for app store metadata and system permissions
- โCommit translation files to version control
Troubleshooting
- โ
If you see missing translations, check that:
- โThe translation key exists in the source language file
- โYou've run
babely translateafter adding new keys - โThe
i18n.localeis set correctly
- โ
If native translations aren't working:
- โVerify your
app.jsonconfiguration - โRebuild your app after making changes to native translation files
- โVerify your
Additional Configuration
The preset supports customization through the babely.json file:
{ "locale": { "source": "en", "targets": ["es", "fr"] }, "files": { "json": { "include": [ "locales/native/[locale].json", "locales/[locale].json" ] } } }
For more advanced configuration options, refer to the Babely configuration documentation.