@gravity-ui/i18n-core
v1.3.2
Published
The **i18n-core** library is the foundation for internationalization (i18n) libraries. Built on top of **[@formatjs/intl](https://formatjs.github.io/)**.
Readme
i18n-core
The i18n-core library is the foundation for internationalization (i18n) libraries. Built on top of @formatjs/intl.
Key Features:
Framework Agnostic: Designed to work with any JavaScript or TypeScript project, React-based or not.
Built on top of @formatjs/intl: Leverages the powerful formatting and customization capabilities of @formatjs/intl, providing industry-standard localization functionality.
Modular and Extensible: The architecture allows for extending or customizing functionality according to your project's requirements.
Usage Examples:
- React applications: easily integrates with React projects using the i18n-react library.
- Server-side solutions: easily integrates with server-side solutions using the i18n-node library.
Usage
Locale Fallbacks
Configuration:
fallbackLocales- fallbacks for defined locales.defaultFallback- default fallback. It will be used if no higher priority fallback is found. Available options:empty-string- fallback to empty string.key- fallback to translation key.- One of the locales used in the project
allowedLocales, or their list.
disableUseLocaleLangAsFallback- disable using locale language as its fallback. More details can be found in the fallback search section.
Fallback Search:
Fallbacks for locale='ru-kz':
- Use
fallbackLocales['ru-kz'], if present - If
disableUseLocaleLangAsFallback === false:- Detect locale using Intl.Locale. In our case
Intl.Locale('ru-kz').language === 'ru' - Use
allowedLocales[Intl.Locale('ru-kz').language], if present - Use fallback for
Intl.Locale('ru-kz').language. For search, use this same algorithm from the beginning.
- Detect locale using Intl.Locale. In our case
- Use
defaultFallback
Example:
{% list tabs %}
Using
fallbackLocalesanddefaultFallbacki18n instance config:
{ allowedLocales: ['en', 'ru', 'ru-kz'], fallbackLocales: { 'ru-kz': 'ru', 'ru': 'en' }, defaultFallback: 'empty-string' }Fallbacks by languages:
en- empty stringru-en, empty stringru-kz-ru,en, empty string
Using locale language as fallback
Internationalization instance configuration:
{ allowedLocales: ['en', 'ru', 'ru-kz'], fallbackLocales: {}, defaultFallback: 'empty-string', disableUseLocaleLangAsFallback: false }Fallbacks by languages:
en,ru- empty stringru-kz-ru, empty string
{% endlist %}
