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

tiny-i18n

v2.0.4

Published

Tiny yet useful i18n library.

Downloads

46

Readme

tiny-i18n

NPM version NPM Downloads

Tiny yet useful i18n library.

API

setDictionary

(dict: object, lang: string = getCurrentLanguage()) => object Set dictionary of lang.

NOTE: language field is non-case sensitivity.

'zh-CN' has same effect with 'zh-cn'

setDictionary(
  {
    cancel: 'Cancel',
    confirm: 'OK'
  },
  'zh-CN'
)

extendDictionary

(dict: object, lang: string = getCurrentLanguage()) => object Extend dictionary of lang.

extendDictionary(
  {
    cancel: 'Cancel',
    confirm: 'OK'
  },
  'zh-CN'
)

getWord(key: string, lang: string = getCurrentLanguage())

Get raw word (untranslated) of key in lang language environment.

setDictionary(
  {
    cancel: 'Cancel',
    confirm: 'OK ${1}'
  },
  'zh-CN'
)

getWord('confirm', 'zh-CN') === 'OK ${1}'

setLanguage(lang: string)

Set current language.

getDictionary: (lang: string = getCurrentLanguage()) => object

const dict = {
  cancel: 'Cancel',
  confirm: 'OK ${1}'
}
setDictionary(dict, 'zh-CN')

getDictionary('zh-CN') === dict

getCurrentLanguage: () => string

Note: tiny-i18n use isomorphic-language to set current language at first time

i18n: (key: string ...args: string[]) => string

Get translated words in current language.

setDictionary({
cancel: 'Cancel',
confirm: 'OK ${1}'
}, 'zh-CN')
setLanguage('zh-CN')
getCurrentLanguage() === 'zh-CN'

i18n('cancel') === 'Cancel'
i18n('confirm', ',Cuttle' /_ replaced by `${1}` _/) === 'OK ,Cuttle'

createIsolateI18n()

NOTE: It's useful for create the internal independent i18n environment

import tinyI18n from 'tiny-i18n'
const {createIsolateI18n, setDictionary, i18n} = tinyI18n
setDictionary({
  cancel: 'Cancel',
  confirm: 'OK ${1}'
}, 'zh-CN')
i18n('cancel') === 'Cancel' // true

const i = createIsolateI18n()
i.i18n('cancel') // throws error
// It should be call `i.setDictionary(...)` firstly.

Examples

const tinyI18n = require('tiny-i18n')
const { setDictionary, getDictionary, i18n, setLanguage } = tinyI18n

setDictionary(
  {
    a: '啊',
    b: '吧',
    c: '你好${1}'
  },
  'zh'
)
setDictionary(
  {
    a: 'a',
    b: 'b',
    c: 'c${1}'
  },
  'en'
)

expect(getDictionary('zh')).toEqual({
  a: '啊',
  b: '吧',
  c: '你好${1}'
})
expect(getDictionary('en')).toEqual({
  a: 'a',
  b: 'b',
  c: 'c${1}'
})

setLanguage('zh')
expect(i18n('a')).toBe('啊')
expect(i18n('c', '哈哈')).toBe('你好哈哈')

setLanguage('en')
expect(i18n('a')).toBe('a')
expect(i18n('c', '哈哈')).toBe('c哈哈')