Link Search Menu Expand Document

Supporting Multiple Locales

Built-in support for multiple languages or locales is inherent to messageformat at all levels. For the best results, you may want to separate the messages for each locale to its own file or use the locales’ identifiers (such as 'de' for German) as top-level keys in your message files.

When using either our Webpack loader or Rollup plugin to import messages into your JavaScript runtime, the locale used within a message file may be determined from its filename or path; if it includes a locale identifier, that is used for the messages. Examples: en/foo-messages.yaml, Within a hierarchical message file or bundle, using a locale identifier as a key sets the locale for each message under that key.

Note the “known locale” limit in the above; for both tools you’ll need to include an array like ['en', 'de'] in its configuration for the locale value. This is required as the set of supported locales includes literally hundreds of two- and three-letter identifiers, which would otherwise be accidentally matched with.

Setting the messages’ locale or locales correctly is important, as it’s used to control plural category selection as well as e.g. number and date formatting.

Dynamic Message Loading

For larger applications and/or wider ranges of supported languages, it can be useful for front-end applications to avoid loading all languages’ messages. To achieve this with messageformat, it’s important to realise that the message sources are compiled to ES modules during your application build. This allows for them to be configured to be dynamically loaded, just as may be done by your bundler for all other modules.

Depending on your use case, it may make sense to always bundle in a “default” language’s messages, if you can expect most of your users to use it. Alternatively, you may want to use e.g. <link rel="preload"> to load an appropriate language’s messages even before your JavaScript starts to execute.

To support this sort of use, both @messageformat/runtime/messages and @messageformat/react support messages being loaded after initialisation. Here’s an example using the latter:

# messages.en.yaml
  en: English
  fi: Finnish
select-locale: Select language

  title: App title
  body: >
    Some content here
  en: englanti
  fi: suomi
select-locale: Valitse kieli

  title: Sovellusotsikko
  body: >
    Jotain sisältöä tähän
// app.js
import React, { useEffect, useState } from 'react'
import {
} from '@messageformat/react'

const DEFAULT_LOCALE = 'en'

const SelectLocale = ({ setLocale }) => {
  const locales = useLocales()
  const localeName = useMessageGetter('locales')
  return (
      <select value={locales[0]} onChange={ev => setLocale(}>
        <option value="en">{localeName('en')}</option>
        <option value="fi">{localeName('fi')}</option>

const App = ({ setLocale }) => (
    <SelectLocale setLocale={setLocale} />
    <hr />

export default function Wrapper() {
  const [locale, setLocale] = useState(DEFAULT_LOCALE);
  const [messages, setMessages] = useState({});

  // In a real app, you may want to indicate message loading somehow
  useEffect(() => {
    if (messages[locale]) return;
    const get = locale === 'en' ? () => import('./messages.en.yaml')
              : locale === 'fi' ? () => import('./')
              : () => Promise.reject(`Unsupported locale ${locale}`)
      .then(module => {
        const next = Object.assign({}, messages)
        next[locale] = module.default
      .catch(error => {
  }, [locale, messages])

  return (
      <App setLocale={setLocale} />