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

sg-koa-i18n

v1.1.2

Published

Fork of https://github.com/shared-goals/sg-koa-i18n.git

Downloads

2

Readme

sg-koa-i18n

i18n middleware for [email protected], use with sg-i18n, you need to use together with koa-locale.

Installation

npm i sg-koa-i18n -S

Usage

var app = require('koa')();
var locale = require('koa-locale');
var i18n = require('sg-koa-i18n');

// the locale key name defaults to `locale`
locale(app, 'language');

// add i18n middleware
app.use(i18n(app, {
  directory: __dirname + '/fixtures/locales',
  locales: ['zh-CN', 'en', 'zh-TW'],
  defaultLocale: 'zh-CN',
  mappings: {
    'zh-HK': 'zh-TW'
  },
  modes: [
    'header', 
    'url',
    (ctx) => {
      return ctx.headers['locale'];
    }
  ]
}));

// the, u can ust the i18n
app.use(async (ctx, next) => {
  // i18n instance
  let i18n = ctx.i18n;

  // translate
  let msg = i18n.__('msg');
  let msg2 = ctx.__('msg2');

  // ctx.state.locale: object
  console.log(ctx.state.locale)

  ctx.body = ctx.i18n.__('locales.en')
});

ctx.state.locale

// ctx.state.locale
ctx.state.locale = {
  // the finnal used locale
  locale: locale,
  // the locale detected with modes, no mappings, no default,
  detected: localeDetected,
  // the detected locale of modes
  map: whiteMap,
  // the mode that locale detected from
  use: use,
}

options

{
  // support locales
  locales: ['zh-CN'],

  // default locale, must in locales
  defaultLocale: 'zh-CN',

  // the i18n data directory, absolute path
  directory: '',

  // the extension of the i18n data file,
  // the data of file must be JSON format
  extension: '.js',

  // the mode to get locale from,
  // search by order, use the first matched one
  // example: ['Subdomain', 'Cookie', 'Header', 'Query', 'Url', 'TLD']
  // https://github.com/koa-modules/locale
  modes: ['url'],

  // mappings other locales that not in locales to one of the locales
  // example: {'zh-HK': 'zh-CN'}
  mappings: {},

  // clear the locale in path, so can match router later
  // /zh-CN/xxx => /xxx
  rewrite: true,
}

Locale lookup priority

  1. modes
  2. locales
  3. mappings
  4. defaultLocale

translate