Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
resolve-accept-language
Advanced tools
Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.
Resolve the preferred locale based on the value of an Accept-Language
HTTP header.
Add the package as a dependency:
npm install resolve-accept-language
Code example:
import resolveAcceptLanguage from 'resolve-accept-language'
console.log(
resolveAcceptLanguage('fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001', ['en-US', 'fr-CA'], 'en-US')
)
Output:
fr-CA
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:
import { ResolveAcceptLanguage } from 'resolve-accept-language'
/**
* If you are planning to have a "default locale", make sure to add it first in the provided locale list.
* By doing this, your match result will be identical to `resolveAcceptLanguage` as it always checks the
* default locale first.
*/
const resolveAcceptLanguage = new ResolveAcceptLanguage('fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001', [
'en-US',
'fr-CA',
])
if (resolveAcceptLanguage.hasMatch()) {
const locale = resolveAcceptLanguage.getBestMatch() as string
console.log(`A locale was matched: ${locale}`)
if (resolveAcceptLanguage.bestMatchIsLocaleBased()) {
console.log('The match is locale-based')
} else if (resolveAcceptLanguage.bestMatchIsLanguageBased()) {
console.log('The match is language-based')
} else if (resolveAcceptLanguage.bestMatchIsRelatedLocaleBased()) {
console.log('The match is related-locale-based')
}
}
if (resolveAcceptLanguage.hasNoMatch()) {
console.log('No match found :(')
}
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 will use the following logic:
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.
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.
Three different matching patterns (based on the HTTP header's quality factor and order of the provided locales):
If there were any matches, get the highest-ranked (quality factor) locale or language code:
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.
When using resolveAcceptLanguage
return the default locale as a last resort option.
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:
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.FAQs
Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.
The npm package resolve-accept-language receives a total of 36,646 weekly downloads. As such, resolve-accept-language popularity was classified as popular.
We found that resolve-accept-language demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.