npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openagenda/intl

v1.1.3

Published

## Installation

Downloads

61

Readme

@openagenda/intl

Installation

yarn add @openagenda/intl

Usage

import {
  getLocaleValue,
  mergeLocales,
} from '@openagenda/intl';

// or

import getLocaleValue from '@openagenda/intl/getLocaleValue';
import mergeLocales from '@openagenda/intl/mergeLocales';

getLocaleValue

function getLocaleValue(
  labels,
  lang,
  defaultLang = DEFAULT_LANG,
  fallbackMap = DEFAULT_FALLBACK_MAP
)

See Default constants.

If labels is not an object then it is returned as is.

The function attempts to find the message in the following order:

  • the argument lang
  • the fallbacks (if exists)
  • the default lang
  • the first key of the object

Example:

const multiLabel = {
  fr: 'Titre',
  en: 'Title',
};

getLocaleValue(multiLabel, 'fr');

mergeLocales

Like an Object.assign with a depth of 2, to combine multiple locale objects.

Example:

const filtersLocales = {
  fr: {
    title: 'Titre',
    desc: 'Description',
  },
  en: {
    title: 'Title',
    desc: 'Description',
  },
};
const userLocales = {
  fr: {
    other: 'Autre',
  },
  en: {
    other: 'Other',
  },
};

const locales = mergeLocales(filtersLocales, userLocales);

// result:

expect(locales).toEqual({
  fr: {
    title: 'Titre',
    desc: 'Description',
    other: 'Autre',
  },
  en: {
    title: 'Title',
    desc: 'Description',
    other: 'Other',
  }
});

getFallbackedMessages

function getFallbackedMessages(
  messagesMap,
  fallbackMap = DEFAULT_FALLBACK_MAP,
  defaultLang = DEFAULT_LANG
)

See Default constants.

Complete the messages with the fallback languages then the default language, useful when there is no locale compilation step.

Example:

const messagesMap = {
  en: {
    signup: 'Signup',
    signin: 'Signin',
  },
  fr: {
    signup: 'S\'inscrire',
  },
  br: {}
};

const fallbackedMessages = getFallbackedMessages(messagesMap, userLocales);

// result:

expect(fallbackedMessages).toEqual({
  en: {
    signup: 'Signup',
    signin: 'Signin',
  },
  fr: {
    signup: 'S\'inscrire',
    signin: 'Signin',
  },
  br: {
    signup: 'S\'inscrire',
    signin: 'Signin',
  }
});

getSupportedLocale

function getSupportedLocale(
  lang,
  fallbackMap = DEFAULT_FALLBACK_MAP,
  defaultLang = DEFAULT_LANG
)

See Default constants.

Returns the first language supported by Intl following the chain of fallbacks.

Example:

const defaultLocale = getSupportedLocale('oc');

// result:

expect(defaultLocale).toBe('fr');

getFallbackChain

function getFallbackChain(
  lang,
  fallbackMap = DEFAULT_FALLBACK_MAP,
  defaultLang = DEFAULT_LANG
)

See Default constants.

Returns an array of languages in the order they should be used in case of missing data.

Example:

const fallbacks = getFallbackChain('br');

// result:

expect(fallbacks).toEqual(['br', 'fr', 'en']);

Default constants

const DEFAULT_LANG = 'en';

const DEFAULT_LANGS = ['en', 'fr', 'de', 'it', 'es', 'br', 'ca', 'eu', 'oc', 'io'];

const DEFAULT_FALLBACK_MAP = {
  br: 'fr',
  ca: 'es',
  eu: 'fr',
  oc: 'fr',
};

These constants are used for getLocaleValue and getFallbackedMessages.

Bin oa-intl

To extract messages to translate from your sources you have to add a script in your package.json:

{
  "scripts": {
    "extract-messages": "yarn oa-intl"
  }
}

Usage

Default command, extract and compile:

oa-intl [files]

Extract and compile locales.

Positionals:
  files  Glob path to extract translations from, the source files.  [default: "src/**/*.js"]

Options:
      --version                 Show version number  [boolean]
      --help                    Show help  [boolean]
  -o, --output                  The target path where the script will output an aggregated `.json` file per lang of all the translations from the `files` supplied.  [default: "src/locales/%lang%.json"]
      --compiled                The target path where the script will output the compiled version of the translation files, completed with the fallback langs.  [default: "src/locales-compiled/%lang%.json"]
  -c, --compileOnly             Compile only, skip extraction.  [boolean]
      --defaultLang             Default language, the one that is filled in for the default messages in the files.  [default: "en"]
      --langs                   The target languages of the translations.  [default: "en,fr,de,it,es,br,ca,eu,oc,io"]
      --definedDefault          Languages that are populated with messages set to "" for ease of translation.  [default: "fr"]
      --idInterpolationPattern  If certain message descriptors don't have id, this `pattern` will be used to automatically generate IDs for them,
                                where `contenthash` is the hash of `defaultMessage` and `description`.  [default: "[sha512:contenthash:base64:6]"]
      --fallbackMap             A fallback object (json) to complete each key language with the value language. For `{ "br": "fr" }`, the French will complement the Breton.  [default: "{"br":"fr","ca":"es","eu":"fr","oc":"fr"}"]
      --skipIndex               Does not create index js file.  [boolean]
      --ast                     Whether to compile message into AST instead of just string.  [boolean] [default: true]

Extract:

oa-intl extract [files]

Extract messages.

Positionals:
  files  Glob path to extract translations from, the source files.  [default: "src/**/*.js"]

Options:
      --version                 Show version number  [boolean]
      --help                    Show help  [boolean]
  -o, --output                  The target path where the script will output an aggregated `.json` file per lang of all the translations from the `files` supplied.  [default: "src/locales/%lang%.json"]
      --defaultLang             Default language, the one that is filled in for the default messages in the files.  [default: "en"]
      --langs                   The target languages of the translations.  [default: "en,fr,de,it,es,br,ca,eu,oc,io"]
      --definedDefault          Languages that are populated with messages set to "" for ease of translation.  [default: "fr"]
      --idInterpolationPattern  If certain message descriptors don't have id, this `pattern` will be used to automatically generate IDs for them,
                                where `contenthash` is the hash of `defaultMessage` and `description`.  [default: "[sha512:contenthash:base64:6]"]
      --skipIndex               Does not create index js file.  [boolean]

Compile:

oa-intl compile [locales]

Compile locales.

Positionals:
  locales  Glob path to compile locales from.  [default: "src/locales/%lang%.json"]

Options:
      --version      Show version number  [boolean]
      --help         Show help  [boolean]
  -o, --output       The target path where the script will output the compiled version of the translation files, completed with the fallback langs.  [default: "src/locales-compiled/%lang%.json"]
      --defaultLang  Default language, the one that is filled in for the default messages in the files.  [default: "en"]
      --langs        The target languages of the translations.  [default: "en,fr,de,it,es,br,ca,eu,oc,io"]
      --fallbackMap  A fallback object (json) to complete each key language with the value language. For `{ "br": "fr" }`, the French will complement the Breton.  [default: "{"br":"fr","ca":"es","eu":"fr","oc":"fr"}"]
      --skipIndex    Does not create index js file.  [boolean]
      --ast          Whether to compile message into AST instead of just string.  [boolean] [default: true]