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

@telefonica/i18n

v2.2.0

Published

This package provides a way of translating tokens based on a locale key. The mechanism is simple, you only need to provide an object with translations and a key to be applied. Tokens are allowed to have variables, which will be interpolated later. I.E.:

Downloads

9

Readme

I18N

This package provides a way of translating tokens based on a locale key. The mechanism is simple, you only need to provide an object with translations and a key to be applied. Tokens are allowed to have variables, which will be interpolated later. I.E.:

'%(hello_message)Hola, %(user_name)1$s, bienvenido a %(app_name)2$s'
    ^                       ^       ^                            ^
    1                       2       3                            4

In the above example, number 1 is the token's comment. Number 2 is a variable comment. Number 3 and 4 are variables, that will be replaced with the values passed to the translation funcion as we will explain in the sections below.

API

getI18n

getI18n(
  locale: string,
  translations: {[token: string]: {[localeKey: string]: string}}
): {
    translate: (token: string, ...variables: string | number[]) => string,
    translatePlural: (tokenSingular: string, tokenPlural: string, count: number, ...variables: string | number[]) => string
}

This is the default export, and has to be called to get the I18N object that will provide the functions declared below that will be used to translate tokens. translations keys are the tokens, and values are objects with the shape localeKey -> translationString. I.E.:

I18N.init('es-ES', {
  '%(account_management_page_title)Gestionar usuarios': {
    'es-ES': 'Gestionar usuarios',
    'en-GB': 'Manage users',
  },
  '%(account_management_button_primary)Aceptar': {
    'es-ES': 'Aceptar',
    'en-GB': 'OK',
  },
});

I18N.translate

I18N.translate(
    token: string,
    ...variables: Array<string | number>
): string

This is the main translation function. It receives a token to translate and an arbitrary number of variables to be interpolated. I.E.:

const userName = 'Manolo';

I18N.translate('%(hello_message)Hola, %(user_name)1$s, bienvenido a %(app_name)2$s', userName, 'RapidGator');

Here, translate function will first replace the token given the localeKey and translationsMap passed in upon initialization. Then it will replace the first token's variable for the value of userName, and the same for the second variable. An example return value could be:

Hello, Manolo, welcome to RapidGator

I18N.translatePlural

I18N.translatePlural(
    tokenSingular: string,
    tokenPlural: string,
    count: number,
    ...variables: Array<string | number>
): string

This functions helps translate texts when the token has a different values for its singular and plural variations. Its behaviour is simple, if count equals 1 it will call, I18N.translate passing in the tokenSingular, otherwise it will pass in the tokenPlural.

Local Development

Below is a list of commands you will probably find useful.

yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes.

yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.