ladjs/i18n

Name: i18n

Owner: Lad

Description: i18n wrapper and Koa middleware for Lad

Created: 2017-09-10 03:38:49.0

Updated: 2018-05-21 18:06:43.0

Pushed: 2018-05-21 18:06:45.0

Homepage: https://lad.js.org

Size: 200

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

@ladjs/i18n

build status code coverage code style styled with prettier made with lass license

i18n wrapper and Koa middleware for Lad

Table of Contents
Install

npm:

install @ladjs/i18n

yarn:

 add @ladjs/i18n
Usage
t I18N = require('@ladjs/i18n');
t phrases = { 'HELLO': 'Hello there!' };
t i18n = new I18N({ phrases });

..

use(i18n.middleware);
use(i18n.redirect);

.. routes go here ...

listen();
API
i18n.translate(key, locale)

Returns translation for phrase key with the given locale.

i18n.middleware(ctx, next)

This middleware uses custom locale detection (in order of priority):

  1. Check URL (e.g. if /de or /de/ then it's a de locale - as long as de is a supported locale)
  2. Check the "locale" cookie value (or whatever the cookie option is defined as)
  3. Check Accept-Language header

It also exposes the following:

If the given locale was not available then it will redirect the user to the detected (or default/fallback) locale.

i18n.redirect(ctx, next)

Inspired by node's language support.

Redirects user with permanent 302 redirect to their detected locale if a valid language was not found for them.

It also sets the cookie locale for future requests to their detected locale.

This also stores the last_locale for a user via ctx.state.user.save().

Options

We use i18n options per https://github.com/mashpie/i18n-node#list-of-all-configuration-options

Default options are as follows and can be overridden:

t i18n = new I18N({
rases: {},
gger: console,
rectory: resolve('locales'),
cales: ['en', 'es', 'zh'],
okie: 'locale',
dent: '  ',
faultLocale: 'en',
 `process.env.I18N_SYNC_FILES`
ncFiles: true,
 `process.env.I18N_AUTO_RELOAD`
toReload: false,
 `process.env.I18N_UPDATE_FILES`
dateFiles: true,
i: {
__: 't',
__n: 'tn',
__l: 'tl',
__h: 'th',
__mf: 'tmf'

gister: i18n.api

Note that we automatically bind logDebugFn, logWarnFn, and logErrorFn for i18n options to logger.debug, logger.warn, and logger.error respectively.

For a list of all available locales see i18n-locales.

Contributors

| Name | Website | | ————– | ————————– | | Nick Baugh | http://niftylettuce.com/ |

License

MIT © Nick Baugh


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.