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

express-locale

v2.0.2

Published

Express middleware to determine locale

Downloads

34,070

Readme

express-locale CI

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.

Use version 1.x for Express 3 support and/or older Node versions.

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.

requestProperty

Type: String Default value 'locale'

By default, the locale is attached to req.locale but can be configured with the requestProperty option.

Custom lookups

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

createLocaleMiddleware({
  priority: ['custom'],
  lookups: {
    custom: (req) => req.ip === '127.0.0.1' ? 'en-US' : undefined
  }
});