Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
accept-language
Advanced tools
The 'accept-language' npm package is a utility for parsing and handling the 'Accept-Language' HTTP header. It helps in determining the preferred language(s) of the user based on the header values, which is useful for internationalization (i18n) and localization (l10n) in web applications.
Parsing Accept-Language Header
This feature allows you to parse the 'Accept-Language' header and get an array of language codes with their respective quality values.
const acceptLanguage = require('accept-language');
const header = 'en-US,en;q=0.9,fr;q=0.8,de;q=0.7';
const languages = acceptLanguage.parse(header);
console.log(languages); // Output: [ { code: 'en-US', quality: 1 }, { code: 'en', quality: 0.9 }, { code: 'fr', quality: 0.8 }, { code: 'de', quality: 0.7 } ]
Getting Best Language Match
This feature allows you to determine the best matching language from a list of supported languages based on the 'Accept-Language' header.
const acceptLanguage = require('accept-language');
acceptLanguage.languages(['en', 'fr', 'de']);
const header = 'en-US,en;q=0.9,fr;q=0.8,de;q=0.7';
const bestMatch = acceptLanguage.get(header);
console.log(bestMatch); // Output: 'en'
Setting Default Language
This feature allows you to set a default language that will be used if none of the languages in the 'Accept-Language' header match the supported languages.
const acceptLanguage = require('accept-language');
acceptLanguage.defaultLanguage = 'en';
const header = 'es-ES,es;q=0.9';
const bestMatch = acceptLanguage.get(header);
console.log(bestMatch); // Output: 'en'
The 'negotiator' package is a library for HTTP content negotiation. It can handle 'Accept', 'Accept-Charset', 'Accept-Encoding', and 'Accept-Language' headers. Compared to 'accept-language', 'negotiator' offers broader functionality for content negotiation but may be more complex to use if you only need to handle language preferences.
The 'locale' package is another library for parsing and handling the 'Accept-Language' header. It provides similar functionality to 'accept-language' but also includes additional features for working with locales and regions. It may be a better choice if you need more comprehensive locale handling.
accept-language
parses HTTP Accept-Language header and returns a matched defined language.
npm install accept-language --save
const acceptLanguage = require('accept-language');
acceptLanguage.languages(['en-US', 'zh-CN']);
console.log(acceptLanguage.get('en-GB,en;q=0.8,sv'));
/*
'en-US'
*/
If you are using Express server please use the middleware express-request-language.
Define your language tags ordered in highest priority comes first fashion. The language tags must comply with BCP47 standard.
acceptLanguage.languages(['en-US', 'zh-CN']);
Get the most likely language given an Accept-Language
string. In order for it to work you must set all your languages first.
acceptLanguage.get('en-GB,en;q=0.8,sv');
Tingan Ho @tingan87
MIT
FAQs
Accept-Language parser for nodejs
The npm package accept-language receives a total of 104,196 weekly downloads. As such, accept-language popularity was classified as popular.
We found that 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.