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

@cephalomax/koa-i18next

v1.1.2

Published

koa middleware for i18next

Downloads

2

Readme

This repo is deprecated, please use koa-i18next-middleware.

Introduction

A middleware to use i18next in koajs.

Getting started

install dependencies

npm install koa-i18next

working with backend

const koa = require('koa')
const i18next = require('i18next')
const Backend = require('i18next-sync-fs-backend') // or i18next-node-fs-backend
const koaI18next = require('koa-i18next')

i18next
  .use(Backend)
  .init({
    backend: {
      // translation resources
      loadPath: path.resolve('./locales/{{lng}}/{{ns}}.json'),
      addPath: path.resolve('./locales/{{lng}}/{{ns}}.missing.json')
    },
    preload: ['zh', 'en'], // must know what languages to use
    fallbackLng: 'en'
  })

const app = koa()
app.use(koaI18next(i18next, {
  lookupCookie: 'i18next', // detecting language in cookie
  /**
  * Detecting language in path params, need third part route middleware.
  * Example
  * path: `/api/:lng/hello
  */
  lookupPath: 'lng',
  lookupFromPathIndex: 0, // detecting language in path, like `/api/zh/hello` which `zh` is the language and the index is 1
  lookupQuerystring: 'lng', // detect language in query,
  lookupSession: 'lng', // detect language in session
  /**
  * support querystring, cookie, header, session, path
  * default order: ['querystring', 'cookie', 'header']
  */
  order: ['querystring'],
  next: true // if koa is version 2
}))
// [email protected]
app.use(function* (next) {
  this.body = {message: this.t('lalala')}
})

language detection

Support for:

  • querystring
  • cookie
  • header
  • session
  • path

If you don't config this, it will use ['querystring', 'cookie', 'header'] as default detecting order.

options

{
  // order and from where user language should be detected
  order: ['querystring', 'cookie', 'header'/*, 'path', 'session'*/],
  // keys or params to lookup language from
  lookupQuerystring: 'lng',
  lookupCookie: 'i18next',
  lookupSession: 'lng',
  lookupPath: 'lng',
  lookupFromPathIndex: 0,
  // if koa is v2
  next: true
}

api

you can use i18next in koa middleware, as:

this.t('balabala', options)

i18next.t arguments are all supported. i18next.t

Resources middleware

inspired by i18next-express-middleware

If needed, you can serve the translations with the resources middleware.
As Koa doesn't come with built-in routing system, you'll have to handle the request path matching by a routing library or by specifying a path in the options.

const koaI18next = require('koa-i18next')
...
// Exemple without router
app.use(koaI18next.getResourcesHandler(i18next, {path: '/locales/resources.json'}));

// Exemple with koa-router
app.get('/locales/resources.json', koaI18next.getResourcesHandler(i18next));

Requesting /locales/resources.json?lng=en&ns=fr will return the translations of the common namespace of the en language.
Note: Multiple languages and namespaces are supported.

Available options (with default) are :

{
  // Serve resources only if ctx.path match given path (handle every request by default)
  path: false, 
  // Where to look for lng & ns parameters on the context (query, params, ...)
  propertyParam: 'query',   
  // Name of the lng param
  lngParam: 'lng',
  // Name of the ns param
  nsParam: 'ns'
}

Missing Keys middleware

inspired by i18next-express-middleware

You can handle missing keys with the Missing Keys middleware. It'll need the bodyparser in order to get the submitted missing translations.

const koaI18next = require('koa-i18next')
...
// Exemple without router
app.use(koaI18next.getMissingKeysHandler(i18next, {path: '/locales/add'}));

// Exemple with koa-router
app.post('/locales/add', koaI18next.getMissingKeysHandler(i18next));

Posting on /locales/add?lng=en&ns=common with an array of missing message as body will perform a save missing for the common namespace and the en language.

Available options (with default) are :

{
  // Handle request only if ctx.path match given path 
  path: false, 
  // Where to look for lng & ns parameters on the context (query, params, ...)
  propertyParam: 'query',   
  // Name of the lng params
  lngParam: 'lng',
  // Name of the ns param
  nsParam: 'ns'

}

License

MIT