1.0.0 • Published 2 years ago

express-simple-locale v1.0.0

Weekly downloads
909
License
MIT
Repository
github
Last release
2 years ago

Note
This project has been deprecated and will not be receiving any updates anymore.
Please, consider using an alternative package.

express-simple-locale

A simple Express middleware to guess the short-locale of a user. It then saves the found locale on the request for further usage.

It tries, in that order:

  1. The locale query parameter.
  2. The cookie specified by cookieName option.
  3. The first entry of accept-language header.
  4. The acceptedLanguages key from the request.
  5. The hostname.locale nested key from the request.

When found, the locale is split on spaces, hyphens, commas, and underscores so only the first part gets returned (en_GB -> en).

Install

npm i --save express-simple-locale

Options

NameTypeDefaultDescription
keyStringlocale the key to save locale to on the request
supportedLocalesString[][] available locales for the app
defaultLocaleStringen locale to fallback to
cookieNameStringlocalecookie to try getting the locale from
queryParamsString|String[]['locale']the query parameter(s) to look the locale from

Example

import locale from "express-simple-locale";

const localeMiddlewareOptions = {
  key: "userLocale",
  supportedLocales: ["en", "fr", "it", "es", "de"],
  defaultLocale: "en",
  cookieName: "c00ki3z",
  queryParams: ["locale", "lang"],
};

express()
  .use(locale(localeMiddlewareOptions))
  .use((request, response, next) => {
    // request.userLocale
  });
1.0.0

2 years ago

0.3.4

4 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.0

7 years ago