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

inno-trans

v2.2.0

Published

simple localization library (inspired by laravel translation)

Downloads

14

Readme

inno-trans

📜 simple localization library (inspired by laravel translation)

npm downloads

Example

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            'hello': 'hello world!'
        }
    }
})

t.trans('hello') // => hello world!

Install

yarn add inno-trans
const InnoTrans = require('inno-trans');

browser

<script src="https://unpkg.com/inno-trans"></script>
<script>
 var t = InnoTrans({...});
</script>

Features

Interpolation

Interpolate using tag bracket ({}).

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            welcome: 'welcome, {name}!'
        }
    }
})
t.trans('welcome', {name: 'john'}) // => welcome, john!

Change interpolation tag bracket.

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            welcome: 'welcome, <?=name=>!'
        }
    }
})
t.tag(['<?=', '=>'])
t.trans('welcome', {name: 'john'}) // => welcome, john!

Pluralization

Choose a message that matches the quantity.

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            apples: 'one apple|many apples'
        }
    }
})
t.transChoice('apples', 1) // => one apple
t.transChoice('apples', 2) // => many apples

Complex pluralization

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            apples: '{0}none|[1,19]some|[20,*]many'
        }
    }
})
t.transChoice('apples', 0) // => none
t.transChoice('apples', 10) // => some
t.transChoice('apples', 20) // => many

Fallback

Fallback a another locale when there is no message.

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {},
        ko: {
            'index.hello': '안녕~'
        },
        ja: {
            'index.hello': 'こんにちは~',
            'index.welcome': '歓迎よ~'
        }
    }
})
t.trans('index.hello') // => index.hello
t.trans('index.welcome') // => index.welcome

t.fallbacks(['ko', 'ja'])

t.trans('index.hello') // => 안녕~
t.trans('index.welcome') // => 歓迎よ~

Filter

Converts interpolation values.

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            welcome: 'welcome, {name|upper}!',
            hello: 'hello, {name|lower}!'
        }
    }
})
t.addFilter('upper', str => str.toUpperCase())
t.addFilter('lower', str => str.toLowerCase())

t.trans('welcome', {name: 'John'}) // => welcome JOHN!
t.trans('hello', {name: 'John'}) // => hello john!

Common filter

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            abc: 'ab {0} {1}'
        }
    }
})
t.addFilter('*', str => str.toUpperCase())
t.trans('abc', {0: 'cd', 1: 'ef') // => ab CD EF

Formatter

Converts interpolated messages.

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            welcome: 'welcome, {name}!',
        }
    }
})
t.addFormatter((str, values, locale)=> {
    console.log(`str: ${str}, values: ${JSON.stringify(values)}, locale: ${locale}`)
    return '<p>' + str + '</p>'
})

t.trans('welcome', {name: 'john'})
// str: welcome, john!, values: {"name":"john"}, locale: en
// => <p>welcome, john!</p>

Event

const t = InnoTrans({
    locale: 'en',
    message: {
        en: {
            welcome: 'welcome, {name}!',
        },
        ko: {
            welcome: '안녕, {name}!',
        }
    }
})

t.trans('welcome', {name: 'john'}) // => welcome, john!

t.on('changeLocale', () => {
    t.trans('welcome', {name: 'john'}) // => 안녕, john!
})

t.locale('ko')

events

  • changeLocale
  • changeFallbacks
  • changeTag
  • addMessages
  • removeMessages
  • addFilter
  • removeFilter
  • addFormatter
  • removeFormatter
  • * - Global event.

API

InnoTrans(options)

Create InnoTrans instance.

const t = InnoTrans({
    locale: 'en',
    fallbacks: ['ko', 'ja'],
    messages: {
        en: {
            'welcome': 'welcome, {name}!'
        },
        ko: {
            'welcome': '환영해, {name}!'
        },
        ja: {
            'welcome': '歓迎よ, {name}!'
        }
    },
    tag: ['{', '}'],
    filters: {
        upper: str => str.toUpperCase(),
        lower: str => str.toLowerCase(),
    },
    formatters: [
        str => str + 😝
    ]
})

options

  • locale - Specifies current locale. If not, it is automatically inferred.
  • fallbacks - Specifies another locales to fallback.
  • messages - Message resources.
  • tag - Prefix and suffix for interpolation. Default {,}.
  • filters - Function for converting interpolation values.
  • formatters - Function for converting interpolated message.
  • plugins - Plugin to use.

t.trans(key [, values [, options]])

Returns a message that matches the key.

options

  • locale - Specifies the locale. The current locale is ignored.
  • defaults - String to return instead of the key when a message does not exist.
const t = InnoTrans({
    locale: 'en',
    messages: {
        en: { hello: 'hello!' },
        ko: { hello: '안녕!' }
    }
})

t.trans('welcome') // => welcome
t.trans('welcome', undefined, {defaults: 'Welcome~!'}) // => Welcome~!
t.trans('hello') // => hello!
t.trans('hello', undefined, {locale: 'ko'}) // => 안녕

Short method

t.t('hello', {name: 'john'})

t.transChoice(key, number [, values [, options]])

Returns a message that matches the key and the quantity number.

t.addMessages('en', {bought: 'I bought one|I bought many things!'})

t.transChoice('bought', 1) // => I bought one
t.transChoice('bought', 10) // => I bought many things!

Short mehotd

t.tc('apples', 3, {count: 3});

t.locale([locale])

t.locale('ko') // Change locale to "ko".
t.locale() // => 'ko'

t.fallbacks([fallbacks])

t.fallbacks(['ko', 'ja'])
t.fallbacks() // => ['ko', 'ja']

t.addMessages(locale, messages)

t.addMessages('en', {
    'welcome': 'Welcome, {name}!',
    'hello': 'hello, {name}!'
})

t.removeMessages(locale[, key])

If no key argument, remove all messages in the locale.

t.removeMessages('en', 'welcome')
t.removeMessages('en')

t.getAddedLocales()

Returns the added message locales.

t.getAddedLocales() // => ['en', 'ko', 'ja']
t.removeMessages('en')
t.getAddedLocales() // => ['ko', 'ja']

t.hasMessage(locale [, key])

t.hasMessage('ko') // => false

t.addMessages('ko', {hello: '안녕!'})

t.hasMessage('ko') // => true
t.hasMessage('ko', 'hello') // => true
t.hasMessage('ko', 'others') // => false

t.tag(tag)

Set prefix and suffix for interpolation.

t.tag(['<?=', '=>'])

t.addFilter(name, filter)

Add a filter function.

t.removeFilter(name[, filter])

Remove a filter function.

t.addFormatter(formatter)

Add a formatter function.

t.removeFormatter(formatter)

Remove a formatter function.

t.use(plugin)

Add a plugin.

License

MIT