What is @intlify/core-base?
@intlify/core-base is a core library for internationalization (i18n) in JavaScript applications. It provides essential functionalities for handling translations, message formatting, and locale management, making it easier to build multilingual applications.
What are @intlify/core-base's main functionalities?
Basic Translation
This feature allows you to define translations for different locales and switch between them. The code sample demonstrates how to create a context with messages in English and French, and how to switch the locale to get the translated message.
const { createCoreContext, translate } = require('@intlify/core-base');
const ctx = createCoreContext({
locale: 'en',
messages: {
en: {
hello: 'Hello, world!'
},
fr: {
hello: 'Bonjour, le monde!'
}
}
});
console.log(translate(ctx, 'hello')); // Output: Hello, world!
ctx.locale = 'fr';
console.log(translate(ctx, 'hello')); // Output: Bonjour, le monde!
Message Formatting
This feature allows you to use placeholders in your messages and replace them with dynamic values. The code sample shows how to define a message with a placeholder and provide a value for it during translation.
const { createCoreContext, translate } = require('@intlify/core-base');
const ctx = createCoreContext({
locale: 'en',
messages: {
en: {
greeting: 'Hello, {name}!'
}
}
});
console.log(translate(ctx, 'greeting', { name: 'Alice' })); // Output: Hello, Alice!
Locale Management
This feature provides the ability to manage the current locale and fallback locales. The code sample demonstrates how to set and get the current locale and how to define a fallback locale.
const { createCoreContext } = require('@intlify/core-base');
const ctx = createCoreContext({
locale: 'en',
fallbackLocale: 'fr',
messages: {
en: {
hello: 'Hello, world!'
},
fr: {
hello: 'Bonjour, le monde!'
}
}
});
console.log(ctx.locale); // Output: en
ctx.locale = 'fr';
console.log(ctx.locale); // Output: fr
Other packages similar to @intlify/core-base
i18next
i18next is a popular internationalization framework for JavaScript. It offers a wide range of features including translation, pluralization, and interpolation. Compared to @intlify/core-base, i18next has a larger ecosystem with plugins and integrations for various frameworks.
react-intl
react-intl is a library specifically designed for React applications. It provides components and APIs for formatting dates, numbers, and strings, and for handling translations. While @intlify/core-base is more general-purpose, react-intl is tailored for React, making it easier to use in React projects.
vue-i18n
vue-i18n is an internationalization plugin for Vue.js. It offers similar functionalities to @intlify/core-base but is specifically designed for Vue.js applications. It integrates seamlessly with Vue components and provides a simple API for managing translations and locales.