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

think-i18n

v1.3.2

Published

i18n solution in thinkjs 3.0

Downloads

15

Readme

think-i18n

Build Status Coverage Status npm

简体中文文档

i18n solution in thinkjs 3.0, implement base on Jed, Moment and Numeral.

Features

  • I18N solution cover translation, datetime and number display.
  • Support per locale custom format for datetime and number.
  • Easy to use, just defined your expect format and translation in each locale file and the library will help to apply locale settings on time, no more annoying locale switching!
  • Easy to debug
  • Locale switch process is Customizable to suit your case.
  • moment and numeral are isolated, so you can use them for free.

How to Use

npm install think-i18n --save

Configure extends.js

// thinkjs config/extend.js

const createI18n = require('think-i18n');
const path = require('path');

module.exports = [
  createI18n({
    i18nFolder: path.resolve(__dirname, '../i18n'),
    localesMapping(locales) {return 'en';}

  })
];

complete options

Locale Settings

Each locale settings is a javascript file. see example

module.exports = {
    app: think.app, // if not passed in, __ will not be auto `assign` into `think-view` instance
    localeId,
    translation,
    dateFormat, // optional
    numeralFormat, // optional
}
  • localeId: The unique name of your locale.

  • dateFormat: Will apply to moment.local(localeId, dateFormat); if empty you will get a moment instance with 'en' locale.

  • numeralFormat: Will apply numeral.locales[localeId] = numeralFormat; if empty you will get numeral instance with 'en' locale.

  • translation: Equivalent to Jed locale_data, if you use .po file, jed suggest use po2json which support jed format transform.

Controller and View (nunjucks)

Controller

You can get i18n instance or current locale in controller.


    async indexAction(){
      this.assign(this.i18n(/*forceLocale*/));
      ...
    }

View

If you used think-view , think-i18n will auto-inject an i18n instance into __ field, like this.assign('__', this.getI18n()).


{{ __('some key') }} translation
{{ __.jed.dgettext('domain', 'some key') }} translation in specify domain
{{ __.moment().format('llll') }} datetime format
{{ __.numeral(1000).format('currency') }} number format (see numberFormat.formats)

Complete Options

  • i18nFolder:string Directory path where you put all locale settings files

  • localesMapping:function(locales){return localeId;} Given a list of possible locales, return a localeId

  • getLocale default logic is to extract header['accept-language'] if set to falsy value. To get from url query locale, set to {by: 'query', name: 'locale'}. To get from cookie of locale, set to {by: 'cookie', name: 'locale'} To implement your own logic, function(ctx) {return locale;}

  • debugLocale set to value of localeId

  • jedOptions You can set domain and missing_key_callback, for details refer to jed doc.

    default value is {}, the final jed options will be

   Object.assign(jedOptions, {locale_data: <your locale translation>})

Some Thoughs Behine

Why combine all there libraries' i18n config into one? the answer is for transparent but most importantly, flexibility to compose you date, number and message's behavior per locale, for example, you have a website in China and want to provide English translation, but keep the chinese currency symbol, so you can compose english translation and chinese date and currency in your en.js locale setting.

// locale setting of en.js
module.exports = {
  localeId: 'en_ch',
  translation: require('../english.po.json'),
  dateFormat: require('../moment/en.json'),
  numeralFormat: require('../numeral/en.json')
};

We should always use customize format.

  • use __.moment().format('llll') instead of moment().format('YYYY-MM-dd HH:mm').
  • use __.numeral(value).format('customFormat') instead of numeral(value).format('00.00$'),

Notice

  • locale id must be lower case.
  • If you defined en locale, witch will override the default en locale in Numeral.
  • In case you also want to use moment and numeral in your system, we isolated the Moment and Numeral by use bundledDependencies in package.json.
  • Numeral doesn't not support per locale custom format, this is done using some tricks and you can config each locale's customFormat in config.numeralFormat.formats.