formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Top Related Projects
A JavaScript Internationalization Framework
Give your JavaScript the ability to speak many languages.
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
i18next: learn once - translate everywhere
ICU MessageFormat for Javascript - i18n Plural and Gender Capable Messages
🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
Quick Overview
FormatJS is a modular collection of JavaScript libraries for internationalization that are focused on formatting numbers, dates, and strings for displaying to people. It includes a set of core libraries that can be used individually or in concert, and integrates with several popular frameworks like React and Angular.
Pros
- Comprehensive internationalization solution with support for multiple frameworks
- Follows Unicode CLDR standards for accurate and consistent localization
- Offers both imperative and declarative APIs for flexibility
- Actively maintained with regular updates and improvements
Cons
- Learning curve can be steep for developers new to internationalization
- Some features may require additional configuration or setup
- Performance impact can be noticeable in large applications with many formatted elements
- Documentation, while extensive, can be overwhelming for beginners
Code Examples
- Basic message formatting:
import { IntlMessageFormat } from 'intl-messageformat'
const msg = new IntlMessageFormat('Hello, {name}!', 'en-US')
console.log(msg.format({ name: 'World' })) // Output: Hello, World!
- Number formatting:
import { NumberFormat } from '@formatjs/intl'
const nf = new NumberFormat('en-US', { style: 'currency', currency: 'USD' })
console.log(nf.format(1234.56)) // Output: $1,234.56
- Date formatting:
import { DateTimeFormat } from '@formatjs/intl'
const df = new DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
console.log(df.format(new Date())) // Output: May 15, 2023 (example date)
Getting Started
To start using FormatJS in your project:
-
Install the core package:
npm install @formatjs/intl
-
Import and use the desired formatters:
import { NumberFormat, DateTimeFormat, IntlMessageFormat } from '@formatjs/intl' // Use formatters as shown in the code examples above
-
For React integration, install the react-intl package:
npm install react-intl
-
Wrap your app with IntlProvider:
import { IntlProvider } from 'react-intl' function App() { return ( <IntlProvider messages={translations} locale={userLocale}> {/* Your app components */} </IntlProvider> ) }
For more detailed setup and usage instructions, refer to the official FormatJS documentation.
Competitor Comparisons
A JavaScript Internationalization Framework
Pros of FBT
- Designed for large-scale applications with complex internationalization needs
- Supports inline plurals and gender-aware translations
- Offers a powerful extraction tool for managing translations
Cons of FBT
- Steeper learning curve due to its unique syntax and concepts
- Limited community support compared to FormatJS
- Primarily designed for React applications, which may limit its use in other frameworks
Code Comparison
FormatJS:
import { FormattedMessage } from 'react-intl';
<FormattedMessage
id="welcome"
defaultMessage="Welcome, {name}!"
values={{ name: userName }}
/>
FBT:
import fbt from 'fbt';
<fbt desc="Welcome message">
Welcome, <fbt:param name="name">{userName}</fbt:param>!
</fbt>
Summary
FormatJS is a more widely adopted solution with broader framework support and a gentler learning curve. It's suitable for various project sizes and offers good documentation and community support. FBT, developed by Facebook, excels in complex internationalization scenarios and provides powerful features for large-scale applications, particularly those built with React. However, it may require more initial investment in learning and setup. The choice between the two depends on project requirements, team expertise, and the specific internationalization needs of the application.
Give your JavaScript the ability to speak many languages.
Pros of Polyglot.js
- Lightweight and simple to use, with a smaller learning curve
- No dependencies, making it easier to integrate into projects
- Supports basic pluralization rules out of the box
Cons of Polyglot.js
- Limited support for complex localization scenarios
- Lacks advanced features like date, number, and currency formatting
- Not as actively maintained as FormatJS
Code Comparison
FormatJS:
import { FormattedMessage } from 'react-intl';
<FormattedMessage
id="welcome"
defaultMessage="Welcome, {name}!"
values={{ name: 'John' }}
/>
Polyglot.js:
import Polyglot from 'node-polyglot';
let polyglot = new Polyglot();
polyglot.extend({"welcome": "Welcome, %{name}!"});
polyglot.t("welcome", { name: "John" });
FormatJS offers a more declarative approach with React components, while Polyglot.js uses a more traditional method-based API. FormatJS provides a more comprehensive solution for complex internationalization needs, including support for various data types and formatting options. Polyglot.js, on the other hand, focuses on simplicity and ease of use for basic translation tasks.
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Pros of formatjs
- More comprehensive internationalization (i18n) solution
- Actively maintained with regular updates
- Extensive documentation and community support
Cons of formatjs
- Larger bundle size due to more features
- Steeper learning curve for beginners
- May be overkill for simple projects
Code Comparison
formatjs:
import { FormattedMessage } from 'react-intl';
<FormattedMessage
id="welcome"
defaultMessage="Welcome, {name}!"
values={{ name: 'John' }}
/>
formatjs:
import { formatMessage } from '@formatjs/intl';
const message = formatMessage({
id: 'welcome',
defaultMessage: 'Welcome, {name}!',
}, { name: 'John' });
Summary
Both formatjs and formatjs are internationalization libraries, but formatjs is a more comprehensive solution with a wider range of features. It offers better support for complex localization scenarios but comes with a larger bundle size and steeper learning curve. formatjs, on the other hand, is more lightweight and easier to integrate for simpler projects.
The code comparison shows that formatjs uses a component-based approach for formatting messages, while formatjs uses a function-based approach. This difference in API design may influence which library is more suitable for a given project, depending on the preferred coding style and existing codebase structure.
i18next: learn once - translate everywhere
Pros of i18next
- More extensive ecosystem with plugins and integrations
- Supports multiple backends (e.g., file, HTTP, localStorage)
- Offers a simpler API for basic use cases
Cons of i18next
- Less focus on standardized message syntax
- May require additional setup for advanced features
- Performance can be impacted with heavy use of interpolation
Code Comparison
i18next:
import i18next from 'i18next';
i18next.init({
lng: 'en',
resources: {
en: {
translation: {
key: 'Hello {{name}}!'
}
}
}
});
console.log(i18next.t('key', { name: 'John' }));
FormatJS:
import { IntlProvider, FormattedMessage } from 'react-intl';
const messages = {
key: 'Hello {name}!'
};
<IntlProvider messages={messages} locale="en">
<FormattedMessage id="key" values={{ name: 'John' }} />
</IntlProvider>
Both libraries offer robust internationalization solutions, but i18next provides a more flexible ecosystem with various plugins and integrations. FormatJS, on the other hand, focuses on standardized message syntax and tight integration with React. i18next's simpler API makes it easier for basic use cases, while FormatJS shines in complex React applications with its component-based approach.
ICU MessageFormat for Javascript - i18n Plural and Gender Capable Messages
Pros of messageformat
- Lightweight and focused solely on message formatting
- Supports a wider range of plural forms for various languages
- Can be used in both browser and Node.js environments
Cons of messageformat
- Less comprehensive feature set compared to FormatJS
- Smaller community and ecosystem
- Limited built-in support for additional internationalization features
Code Comparison
messageformat:
import MessageFormat from 'messageformat';
const mf = new MessageFormat('en');
const message = mf.compile('You have {count, plural, one{# message} other{# messages}}.');
console.log(message({ count: 1 })); // "You have 1 message."
FormatJS:
import { IntlProvider, FormattedMessage } from 'react-intl';
const messages = {
myMessage: 'You have {count, plural, one {# message} other {# messages}}.'
};
<IntlProvider messages={messages} locale="en">
<FormattedMessage id="myMessage" values={{ count: 1 }} />
</IntlProvider>
Both libraries provide similar functionality for message formatting, but FormatJS offers a more comprehensive solution for internationalization in React applications. messageformat is more lightweight and focused on message formatting alone, while FormatJS includes additional features like date, number, and relative time formatting. The choice between the two depends on the specific needs of your project and whether you require a full-featured i18n solution or a more targeted message formatting library.
🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
Pros of js-lingui
- Simpler API and easier to learn for beginners
- Better TypeScript support out of the box
- More flexible pluralization rules
Cons of js-lingui
- Smaller community and ecosystem compared to FormatJS
- Less comprehensive documentation
- Fewer advanced features for complex localization scenarios
Code Comparison
js-lingui:
import { t } from "@lingui/macro";
function Welcome({ name }) {
return <h1>{t`Hello ${name}`}</h1>;
}
FormatJS:
import { FormattedMessage } from "react-intl";
function Welcome({ name }) {
return (
<h1>
<FormattedMessage id="welcome" values={{ name }} />
</h1>
);
}
Both libraries offer similar functionality for internationalization, but js-lingui provides a more straightforward API with its macro-based approach. FormatJS, on the other hand, uses a component-based system that may be more familiar to React developers.
While js-lingui excels in simplicity and TypeScript support, FormatJS offers a more mature ecosystem with extensive documentation and advanced features. The choice between the two often depends on project requirements, team expertise, and specific localization needs.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
FormatJS
This repository is the home of FormatJS and related libraries.
Slack: Join us on Slack at formatjs.slack.com for help, general conversation and more ð¬ðð You can sign-up using this invitation link.
Development
See CONTRIBUTING.md for instructions.
Published Packages
Big Thanks
Cross-browser Testing Platform and Open Source <3 Provided by Sauce Labs
Top Related Projects
A JavaScript Internationalization Framework
Give your JavaScript the ability to speak many languages.
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
i18next: learn once - translate everywhere
ICU MessageFormat for Javascript - i18n Plural and Gender Capable Messages
🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot