# express-locale

> Express middleware to determine locale

Latest version **2.0.2** (published 2023-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-locale
pnpm add express-locale
yarn add express-locale
bun add express-locale
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-01-24 |
| First published | 2013-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/express-locale) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Sam Hauglustaine |
| Maintainers | smhg |
| Keywords | locale, express, i18n, l10n, culture |

## Links

- npm: https://www.npmjs.com/package/express-locale
- Repository: https://github.com/smhg/express-locale
- Homepage: https://github.com/smhg/express-locale#readme
- Issues: https://github.com/smhg/express-locale/issues
- npm.io page: https://npm.io/package/express-locale

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2023-01-24
- 2.0.1 — 2022-04-21
- 2.0.0 — 2019-08-30
- 1.2.0 — 2019-03-21
- 1.1.0 — 2019-03-21
- 1.0.5 — 2017-11-15
- 1.0.4 — 2017-10-10
- 1.0.2 — 2017-07-02
- 1.0.1 — 2016-12-06
- 1.0.0 — 2016-11-05
- 0.1.2 — 2014-07-28
- 0.1.1 — 2014-07-24
- 0.1.0 — 2013-03-12

## README

express-locale [![CI](https://github.com/smhg/express-locale/actions/workflows/ci.yml/badge.svg)](https://github.com/smhg/express-locale/actions/workflows/ci.yml)
==============

Express middleware to determine the [locale identifier](https://en.wikipedia.org/wiki/Locale_(computer_software)) 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
```javascript
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:
```json
{
	"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](#map) 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:
```json
{
  "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](#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](https://github.com/expressjs/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:
```javascript
createLocaleMiddleware({
  priority: ['custom'],
  lookups: {
    custom: (req) => req.ip === '127.0.0.1' ? 'en-US' : undefined
  }
});
```

---
_Source: https://npm.io/package/express-locale · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
