Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resolve-accept-language

Package Overview
Dependencies
Maintainers
1
Versions
173
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolve-accept-language - npm Package Compare versions

Comparing version 2.2.0 to 3.0.0

lib/cjs/directives.d.ts

20

package.json
{
"name": "resolve-accept-language",
"version": "2.2.0",
"version": "3.0.0",
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",

@@ -29,14 +29,14 @@ "keywords": [

"import": {
"types": "./lib/esm/resolve-accept-language.d.ts",
"default": "./lib/esm/resolve-accept-language.js"
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
},
"require": {
"types": "./lib/cjs/resolve-accept-language.d.ts",
"default": "./lib/cjs/resolve-accept-language.js"
"types": "./lib/cjs/index.d.ts",
"default": "./lib/cjs/index.js"
}
}
},
"main": "lib/cjs/resolve-accept-language.js",
"module": "lib/esm/resolve-accept-language.js",
"types": "lib/esm/resolve-accept-language.d.ts",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"types": "lib/esm/index.d.ts",
"files": [

@@ -58,4 +58,4 @@ "lib"

"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "7.1.0",
"@typescript-eslint/parser": "7.1.0",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
"depcheck": "^1.4.7",

@@ -62,0 +62,0 @@ "dotenv-cli": "7.3.0",

@@ -12,2 +12,4 @@ # resolve-accept-language

> ⚠ In March 2024, version 3 of this package was released, which includes breaking changes. Please refer to the [upgrade guide](./V2-TO-V3-UPGRADE-GUIDE.md) before upgrading.
Add the package as a dependency:

@@ -22,3 +24,3 @@

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

@@ -50,22 +52,25 @@ /**

You may want to control exactly the behavior depending on the type of match. For example, you could 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 `ResolveAcceptLanguage` class instead. It offers more visibility into the selection process while matching a locale:
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:
```ts
import { MATCH_TYPES, ResolveAcceptLanguage } from 'resolve-accept-language'
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
const resolveAcceptLanguage = new ResolveAcceptLanguage(
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'
'fr-CA',
{ returnMatchType: true }
)
console.log(`A locale was matched: ${resolveAcceptLanguage.getMatch()}`)
console.log(`A locale was matched: ${match}`)
if (resolveAcceptLanguage.getMatchType() === MATCH_TYPES.localeBased) {
if (matchType === MATCH_TYPES.locale) {
console.log('The match is locale-based')
} else if (resolveAcceptLanguage.getMatchType() === MATCH_TYPES.languageBased) {
} 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 (resolveAcceptLanguage.getMatchType() === MATCH_TYPES.relatedLocaleBased) {
} else if (matchType === MATCH_TYPES.relatedLocale) {
console.log('The match is related-locale-based')
} else if (resolveAcceptLanguage.getMatchType() === MATCH_TYPES.defaultLocale) {
} else if (matchType === MATCH_TYPES.defaultLocale) {
console.log('The match is the default locale')

@@ -81,10 +86,15 @@ }

1. The default locale (when provided) will always be put as the first locale being evaluated since it is considered the highest quality content available. Otherwise, the locales will be evaluated in the order provided, where the first is the highest quality and the last the lowest.
2. All locales and languages are extracted from the HTTP header and sorted by quality factor. Locales and languages that are in the HTTP header but not in scope are discarded.
3. Three different matching patterns (based on the HTTP header's quality factor and order of the provided locales):
1. If there were any matches, get the highest-ranked (quality factor) locale or language code:
1. **Locale-based match**: Is the highest-ranked a locale? If yes, this is the best match.
2. **Language-based match**: Otherwise, find the first locale that matches the highest-ranked language.
2. **Related-locale-based match**: If there is no match, find the first locale with a language that matches the highest-ranked language of locales that were not in scope. This is a bit of a "fuzzy match", but the presumption is that it's better to show content in a language that can be understood even if the country is wrong.
4. When using `resolveAcceptLanguage` return the default locale as a last resort option.
1. The default locale will always be placed as the first locale being evaluated since it is considered the highest quality content available. Otherwise, the locales will be evaluated in the order provided.
2. All locales and languages are extracted from the HTTP header and sorted by quality factor and position in the header.
3. The following matching logic will be performed:
1. Perform a first loop on the directives
1. If the directive is a locale, then try to find an exact locale match (**locale match**)
2. If the directive is a language, then
1. Create a new list of directives that have locales matching the language
2. Loop through this new list, and try to find an exact locale match (**language-specific locale match**)
3. If no match is found, then try to find a locale that matches the language (**language match**)
2. Perform a second loop on the directives
1. Create a new list of languages from the locales
2. Loop through this new list, then try to find a locale that matches the language (**related-locale match**)
3. If everything fails, then the fallback is the default locale (**default locale match**)

@@ -91,0 +101,0 @@ ## Why another `Accept-Language` package?

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc