What is @formatjs/intl-utils?
@formatjs/intl-utils is a utility library that provides various internationalization (i18n) utilities. It is part of the FormatJS suite and is designed to help with formatting dates, numbers, and messages in a way that is locale-aware.
What are @formatjs/intl-utils's main functionalities?
Date Formatting
This feature allows you to format dates according to the specified locale and options. The example formats the current date in the 'en-US' locale.
const { formatDate } = require('@formatjs/intl-utils');
const date = new Date();
const formattedDate = formatDate(date, { year: 'numeric', month: 'long', day: 'numeric' }, 'en-US');
console.log(formattedDate); // Output: 'October 5, 2023'
Number Formatting
This feature allows you to format numbers according to the specified locale and options. The example formats a number as a currency in the 'en-US' locale.
const { formatNumber } = require('@formatjs/intl-utils');
const number = 1234567.89;
const formattedNumber = formatNumber(number, { style: 'currency', currency: 'USD' }, 'en-US');
console.log(formattedNumber); // Output: '$1,234,567.89'
Message Formatting
This feature allows you to format messages with placeholders according to the specified locale and values. The example formats a message by replacing the placeholder with a value in the 'en-US' locale.
const { formatMessage } = require('@formatjs/intl-utils');
const message = 'Hello, {name}!';
const values = { name: 'John' };
const formattedMessage = formatMessage(message, values, 'en-US');
console.log(formattedMessage); // Output: 'Hello, John!'
Other packages similar to @formatjs/intl-utils
intl
The 'intl' package provides the ECMAScript Internationalization API as a polyfill. It offers similar functionalities for date, number, and message formatting but is more focused on providing a standard API that is part of the ECMAScript specification.
i18next
The 'i18next' package is a powerful internationalization framework for JavaScript. It provides extensive support for translation, formatting, and localization. Compared to @formatjs/intl-utils, it offers a more comprehensive solution for managing translations and localization in applications.
moment
The 'moment' package is a popular library for parsing, validating, manipulating, and formatting dates. While it is not specifically focused on internationalization, it provides robust date formatting capabilities that can be used in conjunction with locale settings.
Intl Utils
Provide i18n utilities.
![npm Version](https://img.shields.io/npm/v/@formatjs/intl-utils.svg?style=flat-square)
API
selectUnit
This function determines the best fit
unit based on a specific set of customizable thresholds.
function selectUnit(
from: Date | number,
to: Date | number = Date.now(),
thresholds = DEFAULT_THRESHOLDS
): {value: number; unit: Unit};
where thresholds
has the shape of:
interface Threshold {
second: number;
minute: number;
hour: number;
day: number;
}
month
& year
are based on calendar, thus not customizable.
Example:
import {selectUnit} from '@formatjs/intl-utils';
selectUnit(Date.now() - 1000);
selectUnit(Date.now() - 44000);
selectUnit(Date.now() - 50000);
Caveats
selectUnit
is meant to be a stepping stone from the old IntlRelativeFormat
to the officially spec-ed Intl.RelativeTimeFormat
. Therefore we don't recommend using this for an extended period of time because of ambiguous editorial issues such as:
-
From 2019/01/01 -> 2018/11/01 can technically be last year
, 2 months ago
or a quarter ago
.
-
From 2019/01/02 6am to 2019/01/01 11pm can also be 7 hours ago
or yesterday
. Timezone further complicates the issue.
The examples above have not even tackled the differences in non-Gregorian calendars. There is an issue opened upstream in the spec that potentially introduces a best fit
algorithm. Therefore, we recommend that you implement your own version of selectUnit
that matches your editorial expectation.