All Projects → formatjs → Intl Messageformat

formatjs / Intl Messageformat

Licence: other
[MIGRATED] Format a string with placeholders, including plural and select support to create localized messages.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Intl Messageformat

Grow
A declarative website generator designed for high-quality websites, with a focus on easy maintenance and localization.
Stars: ✭ 360 (-32.96%)
Mutual labels:  localization, internationalization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (-52.33%)
Mutual labels:  localization, internationalization
django-autotranslate
A simple Django app to automatically translate the pot (.po) files generated by django's makemessages command using google translate.
Stars: ✭ 59 (-89.01%)
Mutual labels:  internationalization, localization
react-translator-component
React language translation module for developing a multilingual project.
Stars: ✭ 13 (-97.58%)
Mutual labels:  internationalization, localization
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (-16.01%)
Mutual labels:  localization, internationalization
python-fluent
Python implementation of Project Fluent
Stars: ✭ 142 (-73.56%)
Mutual labels:  internationalization, localization
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: ✭ 18 (-96.65%)
Mutual labels:  internationalization, localization
localized-countries
🌐 Country code to name mappings for several languages
Stars: ✭ 18 (-96.65%)
Mutual labels:  internationalization, localization
Js Lingui
🌍📖 A readable, automated, and optimized (5 kb) internationalization for JavaScript
Stars: ✭ 3,249 (+505.03%)
Mutual labels:  localization, internationalization
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (-46%)
Mutual labels:  localization, internationalization
React Localize Redux
Dead simple localization for your React components
Stars: ✭ 384 (-28.49%)
Mutual labels:  localization, internationalization
Easy localization
Easy and Fast internationalizing your Flutter Apps
Stars: ✭ 407 (-24.21%)
Mutual labels:  localization, internationalization
flutter-internationalization
Flutter Internationalization by Using JSON Files
Stars: ✭ 18 (-96.65%)
Mutual labels:  internationalization, localization
i18n
Package i18n is for app Internationalization and Localization.
Stars: ✭ 79 (-85.29%)
Mutual labels:  internationalization, localization
plate
Internationalization library for Python
Stars: ✭ 31 (-94.23%)
Mutual labels:  internationalization, localization
blazor-ui-messages
Localization messages for Telerik UI for Blazor components: https://www.telerik.com/blazor-ui
Stars: ✭ 24 (-95.53%)
Mutual labels:  internationalization, localization
Angular-Gulp-Boilerplate
Clean but full-featured AngularJS boilerplate using Gulp workflow and best practices
Stars: ✭ 30 (-94.41%)
Mutual labels:  internationalization, localization
rosetta
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
Stars: ✭ 23 (-95.72%)
Mutual labels:  internationalization, localization
Icu4x
Solving i18n for client-side and resource-constrained environments.
Stars: ✭ 275 (-48.79%)
Mutual labels:  localization, internationalization
Fluent Rs
Rust implementation of Project Fluent
Stars: ✭ 503 (-6.33%)
Mutual labels:  localization, internationalization

This repo was migrated to the monorepo

Intl MessageFormat

Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.

npm Version Build Status Dependency Status

gzip size

Sauce Test Status

Overview

Goals

This package aims to provide a way for you to manage and format your JavaScript app's string messages into localized strings for people using your app. You can use this package in the browser and on the server via Node.js.

This implementation is based on the Strawman proposal, but there are a few places this implementation diverges.

Note: This IntlMessageFormat API may change to stay in sync with ECMA-402, but this package will follow semver.

How It Works

Messages are provided into the constructor as a String message, or a pre-parsed AST object.

var msg = new IntlMessageFormat(message, locales, [formats]);

The string message is parsed, then stored internally in a compiled form that is optimized for the format() method to produce the formatted string for displaying to the user.

var output = msg.format(values);

Common Usage Example

A very common example is formatting messages that have numbers with plural labels. With this package you can make sure that the string is properly formatted for a person's locale, e.g.:

var MESSAGES = {
    'en-US': {
        NUM_PHOTOS: 'You have {numPhotos, plural, ' +
            '=0 {no photos.}' +
            '=1 {one photo.}' +
            'other {# photos.}}'
    },

    'es-MX': {
        NUM_PHOTOS: 'Usted {numPhotos, plural, ' +
            '=0 {no tiene fotos.}' +
            '=1 {tiene una foto.}' +
            'other {tiene # fotos.}}'
    }
};

var output;

var enNumPhotos = new IntlMessageFormat(MESSAGES['en-US'].NUM_PHOTOS, 'en-US');
output = enNumPhotos.format({numPhotos: 1000});
console.log(output); // => "You have 1,000 photos."

var esNumPhotos = new IntlMessageFormat(MESSAGES['es-MX'].NUM_PHOTOS, 'es-MX');
output = esNumPhotos.format({numPhotos: 1000});
console.log(output); // => "Usted tiene 1,000 fotos."

Message Syntax

The message syntax that this package uses is not proprietary, in fact it's a common standard message syntax that works across programming languages and one that professional translators are familiar with. This package uses the ICU Message syntax and works for all CLDR languages which have pluralization rules defined.

Features

  • Uses industry standards: ICU Message syntax and CLDR locale data.

  • Supports plural, select, and selectordinal message arguments.

  • Formats numbers and dates/times in messages using Intl.NumberFormat and Intl.DateTimeFormat, respectively.

  • Optimized for repeated calls to an IntlMessageFormat instance's format() method.

  • Supports defining custom format styles/options.

  • Supports escape sequences for message syntax chars, e.g.: "\\{foo\\}" will output: "{foo}" in the formatted output instead of interpreting it as a foo argument.

Usage

Modern Intl Dependency

This package assumes that the Intl global object exists in the runtime. Intl is present in all modern browsers (IE11+) and Node (with full ICU). The Intl methods we rely on are:

  1. Intl.NumberFormat for number formatting (can be polyfilled using Intl.js)
  2. Intl.DateTimeFormat for date time formatting (can be polyfilled using Intl.js)
  3. Intl.PluralRules for plural/ordinal formatting (can be polyfilled using intl-pluralrules)

Loading Intl MessageFormat in a browser

<script src="intl-messageformat/intl-messageformat.min.js"></script>

Loading Intl MessageFormat in Node.js

Simply require() this package:

var IntlMessageFormat = require('intl-messageformat');

NOTE: Your Node has to include full ICU

Public API

IntlMessageFormat Constructor

To create a message to format, use the IntlMessageFormat constructor. The constructor takes three parameters:

  • message - {String | AST} - String message (or pre-parsed AST) that serves as formatting pattern.

  • locales - {String | String[]} - A string with a BCP 47 language tag, or an array of such strings. If you do not provide a locale, the default locale will be used. When an array of locales is provided, each item and its ancestor locales are checked and the first one with registered locale data is returned. See: Locale Resolution for more details.

  • [formats] - {Object} - Optional object with user defined options for format styles.

var msg = new IntlMessageFormat('My name is {name}.', 'en-US');

Locale Resolution

IntlMessageFormat uses Intl.NumberFormat.supportedLocalesOf() to determine which locale data to use based on the locales value passed to the constructor. The result of this resolution process can be determined by call the resolvedOptions() prototype method.

resolvedOptions() Method

This method returns an object with the options values that were resolved during instance creation. It currently only contains a locale property; here's an example:

var msg = new IntlMessageFormat('', 'en-us');
console.log(msg.resolvedOptions().locale); // => "en-US"

Notice how the specified locale was the all lower-case value: "en-us", but it was resolved and normalized to: "en-US".

format(values) Method

Once the message is created, formatting the message is done by calling the format() method on the instance and passing a collection of values:

var output = msg.format({name: "Eric"});
console.log(output); // => "My name is Eric."

Note: A value must be supplied for every argument in the message pattern the instance was constructed with.

User Defined Formats

Define custom format styles is useful you need supply a set of options to the underlying formatter; e.g., outputting a number in USD:

var msg = new IntlMessageFormat('The price is: {price, number, USD}', 'en-US', {
    number: {
        USD: {
            style   : 'currency',
            currency: 'USD'
        }
    }
});

var output = msg.format({price: 100});
console.log(output); // => "The price is: $100.00"

In this example, we're defining a USD number format style which is passed to the underlying Intl.NumberFormat instance as its options.

Examples

Plural Label

This example shows how to use the ICU Message syntax to define a message that has a plural label; e.g., "You have 10 photos":

You have {numPhotos, plural,
    =0 {no photos.}
    =1 {one photo.}
    other {# photos.}
}
var MESSAGES = {
    photos: '...', // String from code block above.
    ...
};

var msg = new IntlMessageFormat(MESSAGES.photos, 'en-US');

console.log(msg.format({numPhotos: 0}));    // => "You have no photos."
console.log(msg.format({numPhotos: 1}));    // => "You have one photo."
console.log(msg.format({numPhotos: 1000})); // => "You have 1,000 photos."

Note: how when numPhotos was 1000, the number is formatted with the correct thousands separator.

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].