Skip to content

Instantly share code, notes, and snippets.

@klzns
Last active June 13, 2025 19:36
Show Gist options
  • Save klzns/98120d43c786f343cc7e90b1bae4d6a1 to your computer and use it in GitHub Desktop.
Save klzns/98120d43c786f343cc7e90b1bae4d6a1 to your computer and use it in GitHub Desktop.
Message IDs

The message IDs should be defined in one of these 3 ways:

// 1. In `formatMessage` directly
formatMessage({ id: 'message-id-here' })

// 2. In `FormattedMessage` directly
<FormattedMessage id="message-id-here" />

// 3. Or by defining them in a `defineMessages` or `defineMessage`
const messages = defineMessages({
  custom: { id: 'message-id-here' },
})

This is because, in the future, we can use a tool that will make a static analysis of the code to extract the used keys. Like this one: https://formatjs.github.io/docs/tooling/cli

// we can't do this
const message = condition ? 'foo.bar' : 'xpto.ada'

// message is a variable and cannot be read in static analysis 
formatMessage({ id: message })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment