Set it up
- 01
Install
lefine, the plural rules for Hermes, expo-localization for the device language, and AsyncStorage for the offline copy.
terminalbashnpm install lefine @formatjs/intl-pluralrules expo-localization @react-native-async-storage/async-storage - 02
Add the plural polyfill
Hermes has no Intl.PluralRules. Without the polyfill, lefine picks “one” for 1 and “other” for the rest, and in development the console names the package.
index.ts// index.ts, before anything else: Hermes has no Intl.PluralRules.// Add the polyfill and the rules of each language the app ships.import "@formatjs/intl-pluralrules/polyfill-force.js";import "@formatjs/intl-pluralrules/locale-data/en.js";import "@formatjs/intl-pluralrules/locale-data/de.js"; - 03
Load the texts, with a copy on the device
createStrings takes your app id and a storage ({ get, set }). After each download lefine saves the file on the device. On a cold start, load serves that copy at once and asks the edge in the background; the next load has the new texts.
App.tsx// App.tsximport { useEffect, useState } from "react";import AsyncStorage from "@react-native-async-storage/async-storage";import { getLocales } from "expo-localization";import { StringsProvider } from "lefine/react";import { createStrings, type StringsProps } from "lefine/server"; // runs anywhere: no Node or React inside// One per app. storage keeps the last manifest and language files on the device,// so a cold start shows texts at once, with or without a network.// MMKV works too: { get: async (k) => mmkv.getString(k), set: async (k, v) => mmkv.set(k, v) }const strings = createStrings({ project: "app_...", storage: { get: (k) => AsyncStorage.getItem(k), set: (k, v) => AsyncStorage.setItem(k, v) },});// The device's first language the app publishes ("de-DE" finds "de"), else the first published one.async function pickLocale() { const published = (await strings.languages()).map((l) => l.code); for (const { languageTag } of getLocales()) { const tag = languageTag.toLowerCase(); const hit = published.find((c) => c === tag) ?? published.find((c) => c === tag.split("-")[0]); if (hit) return hit; } return published[0]!;}export default function App() { const [props, setProps] = useState<StringsProps | null>(null); useEffect(() => { pickLocale().then((locale) => strings.load(locale, ["home"])).then(setProps); }, []); return props ? ( <StringsProvider {...props}> <Home /> </StringsProvider> ) : null;} - 04
Use them in a screen
The same useStrings as on the web. ICU plurals, selects and numbers work.
Home.tsx// Home.tsximport { Text } from "react-native";import { useStrings } from "lefine/react";export function Home() { const t = useStrings("home"); return <Text>{t("greeting", { name: "World" })}</Text>;}