Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@narando/negotiate-accepted-language
Advanced tools
A custom `Accept-Language` parser. You can use this package to localize your clients based on the http headers.
A custom Accept-Language
parser. You can use this package to localize your
clients based on the http headers.
You need to have nodejs
and npm
installed.
$ npm install @narando/negotiate-accepted-language
Based on the priority the best matching language will be returned.
import negotiateAcceptedLanguage from "@narando/negotiate-accepted-language";
const supportedLanguages = ["de-DE", "en-US"];
const acceptedLanguages = "de-DE,de;q=0.9,en;q=0.8,en-US;q=0.7";
negotiateAcceptedLanguage(supportedLanguages, acceptedLanguages);
// returns "de-DE"
In case no one of the accepted languages will match the supported languages exactly. The function tries to find the closest match. To find the closest match the country tag will be ignored.
import negotiateAcceptedLanguage from "@narando/negotiate-accepted-language";
const supportedLanguages = ["de-AT", "en-US"];
const acceptedLanguages = "de-DE,en-CA;q=0.9";
negotiateAcceptedLanguage (supportedLanguages, acceptedLanguages);
// returns "de-AT"
Implement this function as an express middleware.
import negotiateAcceptedLanguage from "@narando/negotiate-accepted-language";
function getLanguageMiddleware(req, res, next) {
const supportedLanguages = ["de-DE", "en-US"];
const acceptedLanguages = req.get("accept-language");
const language = negotiateAcceptedLanguage(
supportedLanguages,
acceptedLanguages
);
// Use language with the i18n module of your choice:
req.i18n.setLanguage(language);
// Or just save the value to use it later:
req.language = language;
next();
}
// Mount as middleware
app.use(getLanguageMiddleware);
The function wil return null if the languagesToParse
are not supported.
import negotiateAcceptedLanguage from "@narando/negotiate-accepted-language";
const supportedLanguages = ["de-DE", "en-US"];
const acceptedLanguages = "af,ch-ZH;q=0.8";
const matchingLanguage = negotiateAcceptedLanguage(
supportedLanguages,
acceptedLanguages
);
// matchingLanguage is null
FAQs
A custom `Accept-Language` parser. You can use this package to localize your clients based on the http headers.
The npm package @narando/negotiate-accepted-language receives a total of 8 weekly downloads. As such, @narando/negotiate-accepted-language popularity was classified as not popular.
We found that @narando/negotiate-accepted-language demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.