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

@uxland/localization

v1.0.6

Published

Localization

Downloads

433

Readme

UXL Localization npm version

| Build Status | Statements | Branches | Functions | Lines | | ----------------------------------------------- | --------------------------------------------- | ----------------------------------------- | ------------------------------------------- | ----------------------------------- | | BuildStatus | Statements | Branches | Functions | Lines |

Installation

npm i @uxland/localization

Usage

A default mixin is provided in order to be able to use localization functionalities and subscribe to language and locales changes.

export class Klass implements locale(BaseKlass) {}

In order to use custom formats in localization, define a new mixin provinding a factory including the desired formats.

const customLocale = localeMixin(() =>
  localizerFactory('en', {}, { number: { EUR: { style: 'currency', currency: 'EUR' } } })
);

export class Klass implements customLocale(BaseKlass) {}

Update current language

Set the current language that will be used in order to retrieve proper localization. Each time locales are updated, the event LANGUAGE_UPDATED will be published in order to inform all subscribers that the language has changed

setLanguage('en');

To retrieve current language from anywhere in the code, use:

getLanguage();

It is possible to reset locales to its default. This action will publish a LANGUAGE_RESET event.

resetLanguage();

By default, language is set to english en.

Update current formatters

Set the current formatters that will be used in order to retrieve proper localization. Each time locales are updated, the event FORMATTERS_UPDATED will be published in order to inform all subscribers that the formatters has changed.

Formatters must follow

setFormatters({ number: { EUR: { style: 'currency', currency: 'EUR' } } });

To retrieve current formatters from anywhere in the code, use:

getFormatters();

It is possible to reset locales to its default. This action will publish a FORMATTERS_RESET event.

resetFormatters();

Set locales dictionary

Locales must follow the next format:

const locales = [language]: {
		foo: {
			title: 'Title',
			subtitle: 'Subtitle'
		}
	};

Each time locales are updated, the event LOCALES_UPDATED will be published in order to inform all subscribers that the locales dictionary has changed.

setLocales(locales);

To retrieve current locales dictionary, use:

getLocales();

It is possible to reset locales to its default dictionary. This action will publish a LOCALES_RESET event.

resetLocales();

Localize

When localizing, the current language is used to retrieve language's locales. Providing a path to the localize function, this will return the corresponding string defined in locales dictonary.

const locales = [language]: {
		foo: {
			title: 'Title',
			subtitle: 'Subtitle',
			greetings: 'Hi!'
		}
	};
setLocales(locales);
localize(`foo.title`); // => "Title"

Arguments can also be provided to localize in order to get dynamic locales as it is specified bellow:

const locales = [language]: {
		foo: {
			title: 'Title',
			subtitle: 'Subtitle',
			greetings: 'Hi {name}!'
		}
setLocales(locales);
localize(`foo.greetings`, 'id', 'John Doe'); // => "Hello John Doe"
localize(`foo.greetings`, {id: 'John Doe'}); // => "Hello John Doe"

It is also possible to use pluralization as follows:

const locales = [language]: {
		foo: {
			title: 'Title',
			subtitle: 'Subtitle',
			greetings: 'Hi {name}!',
			cats: '{cats, plural, =0 {No cats} =1 {A cat} other {# cats}}'
		}
setLocales(locales);
localize(`foo.cats`, 'cats', 0); // => "No cats"
localize(`foo.cats`, 'cats', 1); // => "A cat"
localize(`foo.cats`, 'cats', 3); // => "3 cats"

Also currency can be localized as follows:

const locales = [language]: {
		foo: {
			title: 'Title',
			subtitle: 'Subtitle',
			greetings: 'Hi {name}!',
			cats: '{cats, plural, =0 {No cats} =1 {A cat} other {# cats}}',
			salary: 'Your salary is {salary, number, EUR}'
		}
setLocales(locales);
setFormatters({
        number: { EUR: { style: 'currency', currency: 'EUR' } },
      });
localize(`foo.salary`, { salary: 2000 }); // => "Your salary is €2,000.00"