DocsReact Native

Quickstart - React Native and Expo

Add translations to a React Native or Expo app with GenericTranslationProvider, T and bundled locale files.

On this page

This adds Spanish and French to a React Native app using @generic-translation/react-native. The source is in generic-translation/generic-translation-react-native; the full API is on the reference page.

Not run on a device. This package was developed without a simulator or phone. Its behavior is covered by unit tests against a mocked react-native, and the example Expo app type-checks and bundles with Metro for Android (expo export). Nothing has been launched on iOS or Android. See what was verified.

1. Install

The @generic-translation/* packages are not published to npm yet, so pnpm add @generic-translation/react-native will not work as written. Clone the parent repository, build the packages and link them. The repository’s examples/expo-app shows the setup, including the Metro config for linked packages.

git clone --recurse-submodules https://github.com/generic-translation/generic-translation.git
for p in core react react-native cli; do (cd generic-translation/packages/$p && pnpm install && pnpm build); done
{
  "dependencies": {
    "@generic-translation/core": "link:../generic-translation/packages/core",
    "@generic-translation/react": "link:../generic-translation/packages/react",
    "@generic-translation/react-native": "link:../generic-translation/packages/react-native"
  },
  "devDependencies": {
    "@generic-translation/cli": "link:../generic-translation/packages/cli"
  }
}

Peer dependencies are react >= 18 and react-native >= 0.73. Optionally add @react-native-async-storage/async-storage (to remember the user’s choice) and expo-localization or react-native-localize (to read the device language); they are detected automatically when installed. Without AsyncStorage the choice is kept in memory for the app session only.

2. Wrap your app

import { GenericTranslationProvider, LocaleSelector, T, Var, Plural, Num } from "@generic-translation/react-native";
import { Text } from "react-native";

// Literal require() calls so Metro bundles the JSON; the functions load each map lazily.
const translations = {
  es: () => require("./locales/es.json"),
  fr: () => require("./locales/fr.json"),
};

export default function App() {
  return (
    <GenericTranslationProvider defaultLocale="en" locales={["es", "fr"]} translations={translations}>
      <Text>
        <T>
          Hello <Text style={{ fontWeight: "700" }}><Var name="user">Ada</Var></Text>, you have{" "}
          <Plural n={3} one="one message"><Num>{3}</Num> messages</Plural>.
        </T>
      </Text>
      <LocaleSelector />
    </GenericTranslationProvider>
  );
}

Put <T> inside a <Text>: React Native forbids bare strings outside <Text>. Nested <Text> elements become numbered tags and keep their own style and onPress. For props such as placeholder use useGenericTranslation(), which returns plain strings.

3. Extract and translate

Set "output": "locales/[locale].json" in generic-translation.config.json (generic-translation init --framework react-native suggests this), then:

npx generic-translation init --framework react-native --locales es,fr
npx generic-translation translate

The CLI treats imports from @generic-translation/react-native exactly like imports from @generic-translation/react, so the same messages and hashes are extracted. Alternatively set projectId on the provider to load maps from the Generic Translation CDN, with the last fetched map cached on the device. Until the packages are on npm, run the built CLI with node /path/to/generic-translation/packages/cli/dist/cli.js.

4. Check Intl support on your devices

React Native runs on Hermes, whose Intl coverage is partial. Run checkIntlSupport() on the devices you target, and see Intl on Hermes for the optional polyfills.

Next steps

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.