← Back to React, page 5

react-0094 · Hard senior

How would you design internationalization for a large React app: message catalogs, plurals, formatting, right-to-left layouts and lazy loading of languages?

Keep all text in per-locale catalogs addressed by keys, use ICU-style messages for plurals and variables, and format numbers, dates and lists with the Intl APIs instead of string concatenation. Load catalogs on demand per locale (and per route for big apps), set lang and dir on the document, and use logical CSS properties for RTL. Use a library (react-intl, i18next, Lingui or FormatJS) and a translation workflow, because plural rules and word order differ per language.

Full answer

Internationalization (i18n) prepares code for many languages, and localization (l10n) provides the translations. A global product needs both from the start, because retrofitting is expensive.

Text. Extract every user-visible string into catalogs keyed by message id, one file per locale. Never build sentences by concatenation, because word order differs. Use placeholders (Welcome, {name}) so translators can move them. Give translators context with descriptions.

Plurals. English has two forms, but other languages have more. Polish has one, few, many and other, and Arabic has six categories. Messages should carry all forms and let Intl.PluralRules pick: ICU syntax {count, plural, one {# course} few {# courses} many {# courses} other {# courses}}, with the right words per language. Never write count === 1 ? ... : ... in components.

Formatting. Use Intl.NumberFormat (numbers, currency, units), Intl.DateTimeFormat and Intl.RelativeTimeFormat (dates and "3 days ago"), Intl.ListFormat and Intl.Collator for sorting. Time zones must be explicit for dates in shared data.

Direction. For right-to-left languages such as Arabic and Hebrew, set dir="rtl" on the root together with lang. Write CSS with logical properties (margin-inline-start, padding-inline, text-align: start) so layout flips without separate styles. Mirror directional icons, but not logos or media controls that have fixed meaning.

Delivery. Do not ship all languages to everyone. Load the catalog for the active locale with dynamic import(), and for large apps split catalogs by route or feature. Show a Suspense fallback while loading, and fall back to a default locale for missing keys. Choose the locale from a URL prefix, a cookie or the Accept-Language header, and keep it in a context provider. Server rendering needs the same catalog on the server to avoid hydration mismatches.

Process. Use a translation management system, run a check in CI for missing keys and unused keys, and test with pseudo-localization (long, accented text) to find layout breakage. Avoid text in images and fixed-width buttons.

The example has a small provider with lazy catalogs and plural selection for English and Polish.

jsx
import { createContext, useContext, useEffect, useState } from 'react';

const loaders = {
  en: () => Promise.resolve({ courses: { one: '{n} course', other: '{n} courses' } }),
  pl: () => Promise.resolve({ courses: { one: '{n} kurs', few: '{n} kursy', many: '{n} kursów', other: '{n} kursu' } }),
};

const I18nContext = createContext(null);

function I18nProvider({ children }) {
  const [requested, setLocale] = useState('en');
  const [loaded, setLoaded] = useState(null);

  useEffect(() => {
    let ignore = false;
    loaders[requested]().then((messages) => {
      if (!ignore) setLoaded({ locale: requested, messages });
    });
    return () => {
      ignore = true;
    };
  }, [requested]);

  if (!loaded) return <p>Loading...</p>;

  // The locale and its catalog are stored together, so they can never get out of sync.
  const { locale, messages } = loaded;

  function plural(key, n) {
    const category = new Intl.PluralRules(locale).select(n);
    return messages[key][category].replace('{n}', new Intl.NumberFormat(locale).format(n));
  }

  return (
    <I18nContext value={{ locale, setLocale, plural }}>
      <div lang={locale}>{children}</div>
    </I18nContext>
  );
}

function CourseCounts() {
  const { locale, setLocale, plural } = useContext(I18nContext);
  return (
    <div>
      <button onClick={() => setLocale('en')}>English</button>
      <button onClick={() => setLocale('pl')}>Polski</button>
      <ul>
        {[1, 2, 5, 22].map((n) => (
          <li key={n}>{plural('courses', n)}</li>
        ))}
      </ul>
      <p>{new Intl.NumberFormat(locale, { style: 'currency', currency: 'EUR' }).format(1234.5)}</p>
    </div>
  );
}

export default function App() {
  return (
    <I18nProvider>
      <CourseCounts />
    </I18nProvider>
  );
}

The check switched to English and then to Polish. The catalogs are loaded lazily through promises that stand in for dynamic imports. The Polish forms use the CLDR plural rules from Intl.PluralRules: 1 is one, 2 and 22 are few, 5 is many. The "other" form (used for fractions) is shown here only to complete the category set. Rendered result: <div lang="pl"><div><button>English</button><button>Polski</button><ul><li>1 kurs</li><li>2 kursy</li><li>5 kursów</li><li>22 kursy</li></ul><p>1234,50&nbsp;€</p></div></div>

Key points

  • Put text in per-locale catalogs with placeholders. Do not concatenate sentences.
  • Let Intl.PluralRules choose among the plural forms a language needs.
  • Format numbers, dates and lists with Intl APIs.
  • Set lang and dir, and use CSS logical properties for right-to-left languages.
  • Load catalogs lazily per locale or route, keep the server and client in sync, and automate missing-key checks.

Common mistakes

  • Concatenating translated fragments into sentences.
  • Using count === 1 for plurals and breaking languages with more forms.
  • Hard-coding date and number formats.
  • Using left and right CSS properties and breaking right-to-left layouts.
  • Shipping all locales in the main bundle.

Follow-ups

  • How do you keep server-rendered and client-rendered text identical?
  • How would you detect missing translation keys in CI?
  • How do you handle gendered or context-dependent translations?

Tips

  • Mention plural categories with a concrete language. It shows depth.
  • Use the Intl APIs. Interviewers like platform knowledge.
  • Cover delivery (lazy loading) and process (translation workflow), not only code.