Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

resolve-accept-language

Resolve the preferred locale based on the value of an Accept-Language HTTP header.

accept-language, RFC 4647, locale, language tags, RFC 4646, BCP 47, RFC 2616, resolve, detect, intl, i18n, internationalization

readme

resolve-accept-language

License npm download Coverage Dependencies

Resolve the best locale based on the value of an Accept-Language HTTP header.

Usage

⚠ In March 2024, version 3 of this package was released, which includes breaking changes. Please refer to the upgrade guide before upgrading.

Add the package as a dependency:

npm install resolve-accept-language

Code example:

import { resolveAcceptLanguage } from 'resolve-accept-language'

/**
 * The API is well documented from within your IDE using TSDoc. The arguments are as follows:
 *
 * 1) The HTTP accept-language header.
 * 2) The available locales (they must contain the default locale).
 * 3) The default locale.
 */
console.log(
  resolveAcceptLanguage(
    'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
    // The `as const` is optional for TypeScript but gives better typing.
    ['en-US', 'fr-CA'] as const,
    'en-US'
  )
)

Output:

fr-CA

Advanced use cases

Match types

You may want to control exactly the behavior depending on the type of match. For example, you might want to display a language picker on your home page if the match is not satisfactory. In those cases, you will need to use the { returnMatchType: true } option. It offers more visibility into the selection process while matching a locale:

import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'

const { match, matchType } = resolveAcceptLanguage(
  'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
  ['en-US', 'fr-CA'],
  'fr-CA',
  { returnMatchType: true }
)

console.log(`A locale was matched: ${match}`)

if (matchType === MATCH_TYPES.locale) {
  console.log('The match is locale-based')
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
  console.log('The match is language specific locale-based')
} else if (matchType === MATCH_TYPES.language) {
  console.log('The match is language-based')
} else if (matchType === MATCH_TYPES.relatedLocale) {
  console.log('The match is related-locale-based')
} else if (matchType === MATCH_TYPES.languageCountry) {
  console.log('The match is language country-based')
} else if (matchType === MATCH_TYPES.defaultLocale) {
  console.log('The match is the default locale')
}

Country match

There may be cases where it is preferred to perform a "country match" before falling back to the default locale match. For example:

console.log(
  resolveAcceptLanguage('af-ZA', ['en-US', 'zu-ZA'] as const, 'en-US', {
    returnMatchType: true,
    matchCountry: true,
  })
)

Output:

{ "match": "zu-ZA", "matchType": "country" }

In this case, the header prefers af-ZA, which shares the same country as zu-ZA. Instead of falling back to the default en-US, zu-ZA is matched.

This behavior is not set by default because, in most cases, the quality of the default locale is better than the translations. Performing a country match could potentially lower the quality of the selection. However, there may be cases where this is not true, which is why the matchCountry option exists.

How does the resolver work?

As per RFC 4647, this package uses the "lookup" matching scheme. This means that it will always produce exactly one match for a given request.

The matching strategy follows these steps, in order:

  1. Start with the default locale, as it's considered the highest quality content available.
  2. Extract all locales and languages from the HTTP header, sorted by quality factor and position in the header. Each of these elements is referred to as a "directive".
  3. Perform matching in several stages:
    1. Locale Match: Look for an exact locale match in the directives.
    2. Language-Specific Locale Match: If no locale match is found, look for a locale that matches the language of the directive.
    3. Language Match: If no language-specific locale match is found, look for a locale that matches the language.
    4. Related-Locale Match: If no language match is found, look for a locale that matches the language in the next round of directives.
    5. Language Country Match: If no related-locale match is found, look for a locale that matches the default locale's language but in a country from a locale specified in a directive.
    6. Country Match: If the option is enabled and no language country match is found, look for a locale that matches the country in the next round of directives.
    7. Default Locale Match: If all else fails, fall back to the default locale.

Each stage only happens if the previous stage didn't find a match. This ensures the best possible match is found according to the given criteria.

Why another Accept-Language package?

The Accept-Language header has been around since 1999. Like many other standards that deal with languages, the header is based on BCP 47 language tags. Language tags can be as simple as fr (non-country specific French) or more complex, for example, sr-Latn-RS would represent Latin script Serbian.

One of the main challenges is that BCP 47 language tags can be either overly simple or too complex. This is one of the problems this library will try to address by focusing on locales identifiers using the language-country format instead of trying to provide full BCP 47 language tags support. The main reasons for this:

  • Using 2 letter language codes is rarely sufficient. Without being explicit about the targeted country for a given language, it is impossible to provide the right format for some content such as dates and numbers. Also, while languages are similar across countries, there are different ways to say the same thing. Our hypothesis is that by better targeting the audience, the user experience will improve.
  • About 99% of all cases can be covered using the language-country format. We could possibly extend script support in the future given a valid use case, but in the meantime, our goal is to keep this library as simple as possible, while providing the best matches.

Additional references

changelog

Changelog

3.1.11 (2025-03-21)

3.1.10 (2024-12-16)

3.1.9 (2024-11-23)

3.1.8 (2024-10-05)

3.1.7 (2024-09-15)

3.1.6 (2024-09-01)

3.1.5 (2024-08-03)

  • ESLint 9 cannot be upgraded yet because some dependencies are still incompatible.
  • TypeScript has been upgraded to the latest version, and non-ES5 compatible code has been identified and corrected.

3.1.4 (2024-04-01)

3.1.3 (2024-03-23)

3.1.2 (2024-03-14)

Bug Fixes

  • allow readonly locale variables as arguments and update packages (994249c)

3.1.1 (2024-03-10)

3.1.0 (2024-03-06)

Features

  • add "languageCountry" and optional "country" match types (1aae7fd)

3.0.0 (2024-03-05)

Features

  • major update for improved extensibility and simplicity (38011b3)

BREAKING CHANGES

  • This commit introduces several breaking changes to improve the library's extensibility and simplicity:

  • resolveAcceptLanguage is now a named import for better dual (CommonJS/ESM) compatibility.

  • resolveAcceptLanguage now accepts an options argument.
  • The ResolveAcceptLanguage class has been replaced with resolveAcceptLanguage used with the { returnMatchType: true } option.
  • Introduced a new match type, languageSpecificLocale, for more intelligent matches. See the README for more details.
  • Renamed match types for consistency: localeBased to locale, languageBased to language, and relatedLocaleBased to relatedLocale.

2.2.0 (2024-03-03)

Bug Fixes

  • correct ESM paths for dual CJS/ESM compatibility (9702fd3)

2.1.23 (2024-02-26)

Bug Fixes

  • eslint: resolve import issue for JS files (c6153ff)

2.1.19 to 2.1.22 (2022-02-25)

Bug Fixes

  • resolve dual CJS/ESM package compatibility that broke at version 2.1.18 (5d857f8)

2.1.18 (2024-02-25)

2.1.17 (2024-02-11)

2.1.16 (2024-01-28)

2.1.15 (2024-01-16)

2.1.14 (2024-01-07)

2.1.13 (2024-01-01)

2.1.12 (2023-12-24)

2.1.11 (2023-12-16)

2.1.10 (2023-12-09)

2.1.9 (2023-12-02)

2.1.8 (2023-11-25)

2.1.7 (2023-11-18)

2.1.6 (2023-11-11)

2.1.5 (2023-11-04)

2.1.4 (2023-10-28)

2.1.3 (2023-10-21)

2.1.2 (2023-10-13)

2.1.1 (2023-10-09)

2.1.0 (2023-09-30)

Features

  • add support for es-419 when used in the HTTP header (c4491aa)

2.0.19 (2023-09-23)

2.0.18 (2023-09-16)

2.0.17 (2023-09-09)

2.0.16 (2023-09-02)

2.0.15 (2023-08-26)

2.0.14 (2023-08-18)

2.0.13 (2023-08-11)

2.0.12 (2023-08-05)

2.0.11 (2023-07-29)

2.0.10 (2023-07-22)

2.0.9 (2023-07-15)

2.0.8 (2023-07-09)

2.0.7 (2023-07-01)

2.0.6 (2023-06-24)

2.0.5 (2023-06-17)

2.0.4 (2023-06-09)

2.0.3 (2023-06-02)

2.0.2 (2023-05-26)

2.0.1 (2023-05-20)

2.0.0 (2023-05-13)

Features

  • improved typing on the ResolveAcceptLanguage class (d033696), closes #7 #8

BREAKING CHANGES

  • The class constructor now requires a default locale and all methods have been changed (refer to the upgrade guide).

1.1.56 (2023-05-09)

Bug Fixes

  • fix inferred return type related to #7 (0f50fc6)

1.1.55 (2023-05-08)

Features

  • improve type inference (issue #7) (d40083f)

1.1.54 (2023-05-06)

1.1.53 (2023-04-30)

1.1.52 (2023-04-24)

1.1.51 (2023-04-16)

1.1.50 (2023-04-08)

1.1.49 (2023-04-01)

1.1.48 (2023-03-25)

1.1.47 (2023-03-18)

1.1.46 (2023-03-11)

1.1.45 (2023-03-04)

1.1.44 (2023-02-25)

1.1.43 (2023-02-18)

1.1.42 (2023-02-11)

1.1.41 (2023-02-03)

1.1.40 (2023-01-28)

1.1.39 (2023-01-21)

1.1.38 (2023-01-15)

1.1.37 (2023-01-10)

1.1.36 (2022-12-31)

1.1.35 (2022-12-26)

1.1.34 (2022-12-19)

Bug Fixes

  • correctly parse quality values of 1 as short floating points (2439a06)

1.1.33 (2022-12-19)

1.1.32 (2022-12-11)

1.1.31 (2022-12-04)

1.1.30 (2022-11-28)

1.1.29 (2022-11-18)

1.1.28 (2022-11-13)

1.1.27 (2022-11-06)

1.1.26 (2022-10-27)

1.1.25 (2022-10-20)

1.1.24 (2022-10-16)

1.1.23 (2022-10-09)

1.1.22 (2022-10-02)

1.1.21 (2022-09-25)

1.1.20 (2022-09-15)

1.1.19 (2022-09-09)

1.1.18 (2022-08-30)

1.1.17 (2022-08-23)

1.1.16 (2022-08-14)

Bug Fixes

  • add missing ESLint plugin:import/typescript (3ffb35f)
  • remove overly intrusive ESLint rules (86815d0)

1.1.15 (2022-08-10)

Bug Fixes

  • add end of line config for tsc (85a76d7)
  • associate the TypeScript ESLint config with its correct file types (98c3cfb)

1.1.14 (2022-08-01)

1.1.13 (2022-07-25)

1.1.12 (2022-07-23)

1.1.11 (2022-07-17)

1.1.10 (2022-07-10)

1.1.9 (2022-07-03)

1.1.8 (2022-06-30)

1.1.7 (2022-06-18)

1.1.6 (2022-06-11)

1.1.5 (2022-06-04)

1.1.4 (2022-05-29)

1.1.3 (2022-05-21)

1.1.2 (2022-05-14)

Bug Fixes

  • update the build script with the correct logical sequence (ddd61e6)

1.1.1 (2022-05-06)

1.1.0 (2022-05-01)

Features

  • allow to control exactly the behavior depending on the type of match (dee6429)

1.0.2 (2021-03-14) to 1.0.59 (2022-04-24)

  • minor changes while keeping dev dependencies up to date (commits)

1.0.1 Initial release (2021-03-14)