Open Source library to guess locale of a user
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src Prevent JavaScript error on malformed URI Jan 18, 2018
.gitignore Add initial setup Jul 10, 2017
LICENSE Add LICENSE file Sep 22, 2017
package.json Update version to 0.2.2 Apr 27, 2018


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 and underscores so only the first part gets returned (en_GB -> en).


npm i --save express-simple-locale


Name Type Default Description
key String locale  the key to save locale to on the request
supportedLocales String[] []  available locales for the app
defaultLocale String en  locale to fallback to
cookieName String locale cookie to try getting the locale from
queryParams String|String[] ['locale'] the query parameter(s) to look the locale from


import locale from 'express-simple-locale'

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

  .use((request, response, next) => {
    // request.userLocale