Rosetty: A Complete Intl/I18n Solution for Browser and Node

Rosetty: A Complete Intl/I18n Solution for Browser and Node

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 API

  • Language-sensitive list formatting via listFormat API

  • Language-sensitive number formatting via numberFormat API

  • Plural-sensitive formatting and plural-related language rules via pluralRules API

  • Powerful date and time formatting via the format, formatRelative, formatDistance, formatDistanceToNow, and formatDuration 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 NameTypeDescription
configRecord<string, Language>Specify dictionary and locale to use for each language
defaultLangstring?Specify the default language to use (should be the same as config)
fallbackboolean?Return the fallback value if the translation is not defined

Return

  1. 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.

  2. languages: An array of strings containing all the languages available for translation.

  3. getCurrentLang: A function that returns the current language being used by the library.

  4. t: A function that takes a string key and an optional object of parameters, and returns the corresponding translation for the current language.

  5. displayNames: A class that provides consistent translation of language, region, and script display names.

  6. listFormat: A class that provides language-sensitive list formatting.

  7. numberFormat: A class that provides language-sensitive number formatting.

  8. pluralRules: A class that provides plural-sensitive formatting and plural-related language rules.

  9. format: A function that takes a date and a string format and returns the formatted date string in the given format.

  10. 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.

  11. formatDistance: A function that takes two dates and returns the distance between them in words.

  12. formatDistanceToNow: A function that takes a date and returns the distance between the date and the current time in words.

  13. formatDuration: A function that takes a duration and returns a human-readable duration string, such as "9 months 2 days".

  14. locales: An object containing locale files from the date-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.