Skip to content

format-message/format-message

Repository files navigation

format-message

Internationalize text, numbers, and dates using ICU Message Format.

npm Version Build Status

JS Standard Style MIT License

LOOKING FOR MAINTAINERS

The current maintainers are looking for someone to take over the maintaince of this package. If you are interested please reach out to vanwagonet

Internationalization Made Easy

Start simple. Wrap any user-facing message with formatMessage(). Don't forget to import/require format-message.

var formatMessage = require('format-message');
// ...
formatMessage('My Account Preferences')

Don't concatenate message pieces, use placeholders instead.

formatMessage('Hello, { name }!', { name: user.name })

You can even pick plural and gender forms with placeholders.

formatMessage(`{
  gender, select,
    male {His inbox}
  female {Her inbox}
   other {Their inbox}
 }`, { gender: user.gender })

 formatMessage(`{
   count, plural,
      =0 {No unread messages}
     one {# unread message}
   other {# unread messages}
 }`, { count: messages.unreadCount })

Need to provide extra information to translators? Add a message description. Need 2 translations to the same English message? Add a message id.

formatMessage({
  id: 'update_action_button',
  default: 'Update',
  description: 'Text displayed on the update resource button to trigger the update process'
})
formatMessage({
  id: 'update_label',
  default: 'Update',
  description: 'Label on each item that is an update to another item'
})

Extract all of the messages you've used in your source code.

$ npm i format-message-cli
$ format-message extract "src/**/*.js" > ./locales/en/messages.json

Check that the translators preserved placeholders and proper message formatting.

$ format-message lint -t ./locales/index.js "src/**/*.js"

Use the translations at runtime.

formatMessage.setup({
  generateId: require('format-message-generate-id/underscored_crc32'),
  translations: require('./locales'),
  locale: 'pt'
})

Make a locale-specific build.

$ format-message transform --inline --locale pt "src/**/*.js" > bundle.pt.js

Need more details?

Check out the many ways you can use format-message in your project:

License

This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.