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

@universal-packages/localization

v1.3.16

Published

Dynamic localization with fallbacks and variable replacement

Downloads

628

Readme

Localization

npm version Testing codecov

Dynamic localization with fallbacks and variable replacement.

Install

npm install @universal-packages/localization

Localization

Localization class is the high level representation of the localization object, it provides tools to internationalize your app.

import { Localization } from '@universal-packages/localization'

const localization = new Localization()

console.log(localization.translate('hello'))
//> Hello
console.log(localization.translate('world'))
//> World
console.log(localization.translate('name.hello', null, { name: 'John', emoji: '👋' }))
//> Hello John 👋

console.log(localization.translate('hello', 'en-US'))
//> Howdy
console.log(localization.translate('world', 'en-US'))
//> World
console.log(localization.translate('name.hello', 'en-US', { name: 'John', emoji: '👋' }))
//> Howdy John 👋

console.log(localization.translate('hello', 'es'))
//> Hola
console.log(localization.translate('world', 'es'))
//> Mundo
console.log(localization.translate('name.hello', 'es', { name: 'Juan', emoji: '👋' }))
//> Hola Juan 👋

console.log(localization.translate('hello', 'es-MX'))
//> Que onda
console.log(localization.translate('world', 'es-MX'))
//> Mundo
console.log(localization.translate('name.hello', 'es-MX', { name: 'Juanito', emoji: '👋' }))
//> Que onda Juanito 👋

Options

  • dictionary LocalizationDictionary The dictionary to use for the localization, in case you aleady have a dictionary, you can pass it here.
  • useFileName boolean default: true If you want your translations to be namespaced by the name of the file they are in, you can set this to true.
  • localizationsLocation string default: './src' The path to the folder where the localizations are located. Files can be json, yaml, js, ts and can be nested deeply in the folder structure and should be prefixed with the locale they are for alongside local
    - src
      |- general.en-US.local.yaml
      |- general.es-MX.local.yaml
      |- mailers
        |- welcome.en-US.local.yaml
        |- welcome.es-MX.local.yaml
      |- pages
        |- home.en-US.local.yaml
        |- home.es-MX.local.yaml
  • defaultLocale Locale default: 'en The default locale to use when no locale is provided.

Instance methods

.translate(subject: string | string[], locale: string, [locales: Object])

  • subject string | string[] The path to the translation you want to get, Use dot notation to access nested translations or an array of string.
  • locale string A valid locale string, it will be used to look for the translation in the dictionary.
  • locales Object If the target translation needs variables to be replaced, you can pass them as an object here. use {{ <variableName> }} in the translation to indicate where the variable should be placed.

Looks for a translation in the current locale, if it doesn’t find it, it will look for it in the fallback locale, if it doesn’t find it there either, it will return the subject.

Events

Warning and errors related to missing dictionaries and translations are emitted as events.

const localization = new Localization()

localization.on('waring', (event) => console.log(event))
localization.on('error', (event) => console.log(event))

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.