Table of contents
When building an application that needs to support multiple languages and locales, it is important to have a good internationalization (I18n) solution. This is where Rosetty comes in. Rosetty is a complete Intl/I18n solution for both browser and node environments. It provides a powerful set of tools that make it easy to build applications that can be easily localized for any language or locale.
Features
Some of the features of Rosetty include:
Simple API for translating text into any language
Support for React via Rosetty React
Consistent translation of language, region, and script display names via
displayNames
APILanguage-sensitive list formatting via
listFormat
APILanguage-sensitive number formatting via
numberFormat
APIPlural-sensitive formatting and plural-related language rules via
pluralRules
APIPowerful date and time formatting via the
format
,formatRelative
,formatDistance
,formatDistanceToNow
, andformatDuration
APIs
Usage
To get started with Rosetty, simply install it via npm:
npm install rosetty
Once you have installed Rosetty, you can use it in your code like this:
const { rosetty, locales } = require('rosetty');
const { enGB: enLocale } = locales;
const r = rosetty(
{
en: {
dict: {
test: 'This is a test',
},
locale: enLocale,
},
},
'en'
);
console.log(r.t('test')); // This is a test
API
rosetty(config, defaultLang?, fallback?)
rosetty
is the main API of Rosetty. It takes in a configuration object, an optional default language, and an optional fallback value, and returns a set of functions that can be used for internationalization.
Options
Field Name | Type | Description |
config | Record<string, Language> | Specify dictionary and locale to use for each language |
defaultLang | string? | Specify the default language to use (should be the same as config) |
fallback | boolean? | Return the fallback value if the translation is not defined |
Return
changeLang
: A function that takes a string argument and changes the current language being used by the library to the language specified in the argument.languages
: An array of strings containing all the languages available for translation.getCurrentLang
: A function that returns the current language being used by the library.t
: A function that takes a string key and an optional object of parameters, and returns the corresponding translation for the current language.displayNames
: A class that provides consistent translation of language, region, and script display names.listFormat
: A class that provides language-sensitive list formatting.numberFormat
: A class that provides language-sensitive number formatting.pluralRules
: A class that provides plural-sensitive formatting and plural-related language rules.format
: A function that takes a date and a string format and returns the formatted date string in the given format.formatRelative
: A function that takes a date and a base date, and returns the representation of the date in words relative to the base date.formatDistance
: A function that takes two dates and returns the distance between them in words.formatDistanceToNow
: A function that takes a date and returns the distance between the date and the current time in words.formatDuration
: A function that takes a duration and returns a human-readable duration string, such as "9 months 2 days".locales
: An object containing locale files from thedate-fns
library.
Conclusion
In conclusion, Rosetty is a comprehensive internationalization and localization solution for both Node.js and browser applications. Its features include support for message formatting, plurals, date and time formatting, and currency formatting, among others. It also offers support for multiple languages and locales and is highly customizable. Rosetty's lightweight design and simple API make it easy to use and integrate into existing projects. Overall, it is an excellent tool for developers looking to provide a seamless international user experience for their applications.