Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ladjs/i18n

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ladjs/i18n

i18n wrapper and Koa middleware for Lad

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
32K
decreased by-65.22%
Maintainers
2
Weekly downloads
 
Created
Source

@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:

npm install @ladjs/i18n

yarn:

yarn add @ladjs/i18n

Usage

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

// ...

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

// ... routes go here ...

app.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:

  • ctx.pathWithoutLocale - the ctx.path without the locale in it (this is used by koa-meta)
  • ctx.req - with all of i18n API methods (e.g. ctx.req.t, ctx.req.tn, ...)
  • ctx.state - with all of i18n API methods (e.g. ctx.req.t, ctx.req.tn, ...)
  • ctx.state.availableLanguages (Array) - which is useful for adding a dropdown to select from an available language
  • ctx.state.currentLanguage (String) - the current locale's language in native language using country-language's getLanguage method.
  • ctx.translate (Function) - a helper function for calling i18n.api.t to translate a given phrase (same as i18n.translate except it throws a ctx.throw error using Boom)

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:

const i18n = new I18N({
  phrases: {},
  logger: console,
  directory: resolve('locales'),
  locales: ['en', 'es', 'zh'],
  cookie: 'locale',
  indent: '  ',
  defaultLocale: 'en',
  syncFiles: true,
  autoReload: true,
  updateFiles: true,
  api: {
    __: 't',
    __n: 'tn',
    __l: 'tl',
    __h: 'th',
    __mf: 'tmf'
  },
  register: 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

NameWebsite
Nick Baughhttp://niftylettuce.com/

License

MIT © Nick Baugh

Keywords

FAQs

Package last updated on 22 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc