rosetty

[![Maintainability](https://api.codeclimate.com/v1/badges/1ff0c28615640d86e758/maintainability)](https://codeclimate.com/github/flexper/rosetty/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/1ff0c28615640d86e758/test_coverage)](h

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
rosetty
141.4.1042 days agoa year agoMinified + gzip package size for rosetty in KB

Readme

Maintainability Test Coverage npm npm Snyk Vulnerabilities for npm package NPM
Rosetty
Complete Intl/I18n solution for browser and node

Usage

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?)

Options
| Field Name | Type | Description | | ----------- | ------------------------ | -------------------------------------------------------------- | | config | Record | Specify dictionnary and locale to use for each lang | | defaultLang | string? | Specify default language to use (should be the same as config) | | defaultLang | boolean? | Return fallback if translation is not defined |
Return
| Field Name | Type | Description | | ------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | changeLang | (newLang: string) => void | Change current lang | | languages | string | List of languages who can be selected | | getCurrentLang | () => string | Return current lang | | t | (key: string, params?: Record) => string OR undefined | Return translated text | | displayNames | Documentation | Consistent translation of language, region and script display names Objects/Intl/DisplayNames/DisplayNames> | | listFormat | Documentation | Language-sensitive list formatting Objects/Intl/ListFormat/ListFormat> | | numberFormat | Documentation | Language-sensitive list formatting Objects/Intl/NumberFormat/NumberFormat> | | pluralRules | Documentation | Plural-sensitive formatting and plural-related language rules Objects/Intl/PluralRules/PluralRules> | | format | Documentation | Return the formatted date string in the given format | | formatRelative | Documentation | Represent the date in words relative to the given base date. | | formatDistance | Documentation | Return the distance between the given dates in words. | | formatDistanceToNow | Documentation | Return the distance between the given date and now in words. | | formatDuration | Documentation | Return human-readable duration string i.e. "9 months 2 days" |

locales

Return: Record
Return Date-fns locale files.

WARNING FOR NODE JS ENVIRONMENT

You need to load polyfill on node environment because Intl API is not present. Please use below code to make it works.
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable no-empty */

export const loadPolyfill = () => {
  //@ts-ignore
  if (!Intl?.DisplayNames) {
    require(`@formatjs/intl-displaynames/polyfill`);
  }
  //@ts-ignore
  if (!Intl?.ListFormat) {
    require(`@formatjs/intl-listformat/polyfill-force`);
  }
  //@ts-ignore
  if (!Intl?.NumberFormat) {
    require(`@formatjs/intl-numberformat/polyfill`);
  }
  //@ts-ignore
  if (!Intl?.PluralRules) {
    require(`@formatjs/intl-pluralrules/polyfill`);
  }
};

export const loadPolyfillData = (lang: string) => {
  //Load Lang polyfill
  try {
    require(`@formatjs/intl-displaynames/locale-data/${lang}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-listformat/locale-data/${lang}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-numberformat/locale-data/${lang}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-pluralrules/locale-data/${lang}`);
  } catch (error) {}

  //Load Lang polyfill fallback
  try {
    require(`@formatjs/intl-displaynames/locale-data/${lang.split('-')[0]}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-listformat/locale-data/${lang.split('-')[0]}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-numberformat/locale-data/${lang.split('-')[0]}`);
  } catch (error) {}

  try {
    require(`@formatjs/intl-pluralrules/locale-data/${lang.split('-')[0]}`);
  } catch (error) {}
};

loadPolyfill();
loadPolyfillData('fr');
loadPolyfillData('en');

Maintain

This package use TSdx. Please check documentation to update this package.