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

a-i18n

v0.1.5

Published

String interpolation. Supports pluralization and named parameters.

Downloads

2

Readme

a-i18n

Inspired by http://i18njs.com/

Install

npm install --save a-i18n

const i18n = require('a-i18n')

Usage

Simple

const en = i18n({ key: 'my message' })
const str = en('key')
console.log(str) // my message

Interpolation

You can interpolate numbers and strings using %n and %s.

const en = i18n({ 
  hello: 'Hello %s', 
  unread_messages: 'You have %n unread messages' 
})

en('hello', 'World') // Hello World
en('unread_messages', 4) // You have 4 unread messages

Interpolation will match by the first argument against the first token of the matching type.

const en = i18n({
  test: '%s %n %s %s %n'
})
en('test', 1, 2, 'a', 'b') // a 1 b %s 2

Note that the third "%s" was left because no argument matched it.

TODO Add option for user to specify how it should be handled (e.g. throw exception, replace with blank string, replace with default string specified by user)

Pluralization

As you noticed in the above example for unread messages, that message wouldn't look very nice if I only had 1 unread message.

const en = i18n({ 
  unread_messages: [
    [0, 0, 'You have no unread messages'], 
    [1, 1, 'You have %n unread message'], 
    [2, null, 'You have %n unread messages']] 
})
en('unread_messages', 1) // You have 1 unread message
en('unread_messages', 2) // You have 2 unread messages

Note that pluralization always assumes that the first argument controls pluralization.

const en = i18n({ 
  any: [
    [0, 0, '%s have no messages.'], 
    [1, null, '%s have messages.']
  ] 
})
en('any', 1, 'You') // This works
en('any', 'You', 1) // This would look at 'You' and since it is not a number default pluralization to 0

Names parameters

const en = i18n({ 
  welcome: 'Welcome %{name}! Your last visit was on %{lastVisit}.' 
})
en('welcome', { name: 'Andreas', lastVisit: '2017-01-01' }) // Welcome Andreas! Your last visit was on 2017-01-01.

Reference other messages

const en = i18n({ 
  hello: 'Hello %s! ${any_messages}', 
  any_messages: [
    [0, 0, 'You have no messages.'], 
    [1, 1, 'You have %n message.'], 
    [2, null, 'You have %n messages.']
  ]
})
en('hello', 1, 'Andreas') // Hello Andreas! You have 1 message.

Multiple languages

const en = i18n({ hello: 'Hello!' })
const sv = i18n({ hello: 'Hej!' })

Planned improvements

User can add multiple languages and receive correct language string based on browser language.

// Not final api, only example:

i18n.addLocale('en', { hello: 'Hello!' })
i18n.addLocale('sv', { hello: 'Hej! })

const fn = i18n.formatterForCurrentLocale()
fn('hello') // Hello! or Hej! depending on locale

const en = i18n.formatterForLocale('en')
en('hello') // Hello!

Add support for type agnostic interpolation

const en = i18n({ test: 'Hello {}! You are guest {} today.' })
en('test', 1, 2) // Hello 1! You are guest 2 today.