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

@sapphirejs/intl

v0.0.14

Published

Internationalization for Sapphire Framework

Downloads

2

Readme

Internationalization

An Internationalization (Intl for short) package that formats locale strings. It is a thin wrapper on top of formatjs that provides a few facilities like deep nested object paths and changing the locale dynamically.

Locale Object

Before diving into its usage, you need to have a correctly formatted plain Javascript object with the locale keys and messages. It may look something like the one below, but it can be as nested as you need.

let locales = {
  'en-us': {
    welcome: 'Welcome {name}',
    messages: `You have {messages, plural, =0 {no messages} one {1 message} other {{messages} messages}}`,
    first: { second: { third: `Hi {name} from deep nested key` }}
  },
  'sq-al': {
    welcome: 'Miresevjen {name}',
    messages: `Ju {messages, plural, =0 {s'keni asnje mesazh} one {keni 1 mesazh} other {keni {messages} mesazhe}}`
  }
}

Usage

$ npm install --save @sapphirejs/intl

For the simplest and probably most typical case, you may have a greeting message in your website:

const Intl = require('@sapphirejs/intl')

new Intl(locales, 'en-us')
  .format('welcome', { name: 'John' })
// Welcome John

For the plural forms, the message format is more contrived with different cases of occurrence, but it's usage is exactly the same:

new Intl(locales, 'en-us')
  .format('messages', { messages: 0 })
// You have no messages

No matter how deep objects are nested, they can be easily accessed as a path:

new Intl(locales, 'en-us')
  .format('first.second.third', { name: 'John' })
// Hi John  from deep nested key

Language can be set dynamically after initialization:

let intl = new Intl(locales, 'en-us')
intl.locale = 'sq-al'
intl.format('welcome', { name: 'Xhon' })
// Miresevjen Xhon

Or even temporarly for a single call of format():

let intl = new Intl(locales, 'en-us')
intl.in('sq-al').format('welcome', { name: 'Xhon' })
// Miresevjen Xhon

Finally, you can set a fallback locale during initialization. That locale will be used to search the key if it doesn't exist in the main one.

new Intl(locales, 'sq-al', 'en-us')
  .format('first.second.third', { name: 'John' })
// Hi John from deep nested key