GithubHelp home page GithubHelp logo

nachaos / express-locale Goto Github PK

View Code? Open in Web Editor NEW

This project forked from smhg/express-locale

0.0 0.0 0.0 69 KB

Express middleware to determine locale

License: MIT License

JavaScript 100.00%

express-locale's Introduction

Build Status express-locale

Express middleware to determine the locale identifier of the incomming request.

It returns (only) full locale identifiers based on the middleware's configuration. Configuration defines possible sources, their order and, optionally, a whitelist. For performance reasons, on each request, remaining lookups are ignored as soon as a match is found.

Installation

npm install --save express-locale

Usage

import express from 'express';
import createLocaleMiddleware from 'express-locale';

express()
  .use(createLocaleMiddleware())
  .use((req, res) => {
    res.end(`Request locale: ${req.locale}`);
  })
  .listen(3000);

The locale property on the request object will contain an object with these properties:

{
	source: 'default',
	language: 'en',
	region: 'GB'
}

When using this object in a string context, its toString method returns the locale identifier (en_GB in the example above).

Note: only full locales (language_REGION) are returned, but a mapping of languages to a default locale can be provided as a lookup.

Configuration

You can pass a configuration object to createLocaleMiddleware() with the default being:

{
  "priority": ["accept-language", "default"],
  "default": "en_GB"
}

This tells the middleware to use 2 sources in order: accept-language, which has no configuration, and default which is set to en_GB.

The name of the lookup used in the priority list always matches the configuration key.

priority

Type: Array Default value ['accept-language', 'default']

Defines the order of lookups. The first lookup to return a full locale will be the final result.

Built-in lookups:

  • cookie
  • query
  • hostname
  • accept-language
  • map
  • default

Read below on how to add custom lookups.

cookie

Type: Object Default value '{name: 'locale'}'

The name of the cookie that contains the locale for the cookie lookup.

Use with cookie-parser middleware.

Note: you are responsible for writing the locale to the cookie.

query

Type: Object Default value '{name: 'locale'}'

The name of the query string parameter that contains the locale for the query lookup.

hostname

Type: Object Default value {}

A mapping of hostnames to locales for the hostname lookup.

map

Type: Object Default value {}

Maps lookup results that return only a language to a full locale.

default

Type: String Default value 'en_GB'

The default locale for the default lookup.

allowed

Type: Array Default value undefined

Lookup results are validated against this list of allowed locales if provided.

Custom lookups

Add custom lookups or overwrite the default ones by using the lookups property:

let localeMiddleware = createLocaleMiddleware({
  priority: 'custom',
  lookups: {
    custom: (req) => req.ip === '127.0.0.1' ? 'en_US' : undefined;
  }
});

express-locale's People

Contributors

smhg avatar marionebl avatar stutrek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.