Security News
RubyGems.org Adds New Maintainer Role
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.
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 consumable array of language codes.
npm install accept-language --save
var acceptLanguage = require('accept-language');
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv');
console.log(language);
Output:
[
{
code: "en",
region: "GB",
quality: 1.0
},
{
code: "sv",
region: undefined,
quality: 1.0
},
{
code: "en",
region: undefined,
quality: 0.8
}
];
Filter non-defined language codes:
var acceptLanguage = require('accept-language');
acceptLanguage.codes(['en', 'zh']);
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv');
console.log(language);
Output:
[
{
code: "en",
region: "GB",
quality: 1.0
},
{
code: "en",
region: undefined,
quality: 0.8
}
];
Use default value:
var acceptLanguage = require('accept-language');
acceptLanguage.default({
code : 'en',
region : 'US'
// No need to specify quality
});
acceptLanguage.codes(['en', 'zh']);
var language = acceptLanguage.parse('fr-CA');
console.log(language);
Output:
[
{
code: "en",
region: "US",
quality: 1.0
}
];
The output is always sorted with the highest quality first.
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
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.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.