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

i18nshell

v0.0.0-beta.9

Published

Tiny i18n shell

Downloads

52

Readme

I18nshell

size dm

Tiny i18n shell

English | 中文说明


Install

yarn add i18nshell
# or
npm install i18nshell --save

Basic Usage

import I18n from 'i18nshell'

const i18n = new I18n({
  types: {
    default: {
      resources: {
        en: {
          greet: 'Hello, {{name}}'
        },
        zh: {
          greet: '你好,{{name}}'
        }
      }
    }
  }
})

test()
async function test() {
  await I18n.applyLng('en');
  i18n.t('greet', {
    name: 'CJY'
  }) // get string 'Hello, CJY'

  await I18n.applyLng('zh');
  i18n.t('greet', {
    name: 'CJY'
  }) // get string '你好,CJY' 
}

Add customized i18n types such as currency, time, etc.

i18nshell has simple text translation built in

Currency, time and other nationalization functions need to be implemented by expanding types

Use @ to select the type of translation you want to use

import I18n from 'i18nshell'

const i18n = new I18n({
  types: {
    price: {
      resource: false, // Not using resources
      format: (value) => {
        const unit = {
          en: '$',
          zh: '¥'
        }[I18n.lng]

        return `${unit}${value}`
      }
    },
    jsx: { 
      resources: {
        en: {
          clickHere: 'click {{here}}'
        },
        zh: {
          clickHere: '点击 {{here}}'
        }
      },
      format: (value, data) => (
        <Fragment>
          {I18n.template(value, data, { split: true }).map((item, idx) => (
            <Fragment key={idx}>{item}</Fragment>
          ))}
        </Fragment>
      )
    }
  }
})

test()
async function test() {
  await I18n.applyLng('en');
  i18n.t('1000@price') // get string '$1000'

  await I18n.applyLng('zh');
  i18n.t('1000@price') // get string '¥1000'

  // get jsx content
  i18n.t('clickHere@jsx', {
    here: (
      <a href="https://www.google.com">google</a>
    )
  })
}

Lazyload resources

The following settings work with the import (...) syntax, en language packs are only loaded when needed

// en.js
export default {
  greet: 'Hello, {{name}}'
}
// ./i18n.js
import I18n from 'i18nshell'

new I18n({
  types: {
    default: {
      resources: {
        en: I18n.load(() => import('./en.js')),
        zh: {
          greet: '你好,{{name}}'
        }
      }
    }
  }
})

Split and inherit other language packs

Implemented using the fallback configuration item

import I18n from 'i18nshell'

const i18n_1 = new I18n({
  types: {
    default: {
      resources: {
        zh: {
          test1: '测试1'
        }
      }
    }
  }
})

const i18n_2 = new I18n({
  fallback: [i18n_1],
  types: {
    default: {
      resources: {        
        zh: {
          test2: '测试2'
        }
      }
    }
  }
})

const i18n_3 = new I18n({
  fallback: [i18n_2],
  types: {
    default: {
      resources: {        
        zh: {
          test3: '测试3'
        }
      }
    }
  }
})

test()
async function test() {
  await I18n.applyLng('zh')
  i18n_3.t('test1') // get string '测试1' fallback i18n_2 -> i18n_1
  i18n_3.t('test2') // get string '测试2' fallback i18n_2
  i18n_3.t('test3') // get string '测试3'
}