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

@narando/negotiate-accepted-language

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@narando/negotiate-accepted-language

A custom `Accept-Language` parser. You can use this package to localize your clients based on the http headers.

  • 0.36.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14
increased by27.27%
Maintainers
2
Weekly downloads
 
Created
Source

@narando/negotiate-accepted-language

A custom Accept-Language parser. You can use this package to localize your clients based on the http headers.

Getting Started

You need to have nodejs and npm installed.

$ npm install @narando/negotiate-accepted-language

Usage

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"

Usage as express middleware

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);

Error handling

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

Keywords

FAQs

Package last updated on 26 Jan 2021

Did you know?

Socket

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.

Install

Related posts

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