New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hapi-locale-17

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-locale-17 - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

5

package.json
{
"name": "hapi-locale-17",
"version": "1.0.0",
"version": "1.0.1",
"description": "Locale and language detection for HAPI v17+",

@@ -42,4 +42,5 @@ "main": "src/index.js",

"dependencies": {
"accept-language-parser": "^1.4.1"
"accept-language-parser": "^1.4.1",
"rind-locale": "0.0.3"
}
}

39

README.md
# hapi-locale-17
Locale and language detection for HAPI v17+.
Locale and language detection for Hapi Server v17.
Evaluates locale information from `accept-language` query parameter and attaches locale to `request.locale` available in all Hapi route handlers.
If a `locale` query parameter is privided, it has highest priority. Second priority is the `accept-language` http request parameter. Final priority is a fallback locale.
Target attribute `request.locale` and query parameter `locale` are configurable.
## Install
```bash
npm install --save hapi-locale-17
```
## Example
```js
const Hapi = require('hapi');
const HapiLocale = require('hapi-locale-17');
const server = new Hapi.Server({
port: 3000,
});
const provision = async () => {
await server.register({
plugin: HapiLocale,
options: {
locales: ['de', 'en'], // your supported locales
query: 'lang', // name of query param, defaults to 'locale'
attribute: 'lang', // name of target attribute, defaults to 'locale'
}
});
await server.start();
};
provision();
```
const parser = require('accept-language-parser');
const matcher = require('rind-locale');
const pkg = require('../package.json');
const register = (server, {
locales = ['en'], fallback = locales[0], query = 'locale', attribute = 'locale',
locales = [], fallback = locales[0], query = 'locale', attribute = 'locale',
}) => {

@@ -14,5 +15,5 @@ server.ext({

// 1. query parameter
const queryLocale = request.query[query];
if (queryLocale && locales.includes(queryLocale)) {
setLocale(queryLocale);
const queryParam = request.query[query];
if (queryParam) {
setLocale(matcher({ locales })(queryParam) || fallback);
return h.continue;

@@ -19,0 +20,0 @@ }

@@ -31,7 +31,9 @@ const Hapi = require('hapi');

describe('hapi-locale-17 with default options', async () => {
describe('hapi-locale-17 with `locales` option', async () => {
let server;
before(async () => {
server = await setup();
server = await setup({
locales: ['es', 'de', 'en'],
});
});

@@ -43,3 +45,3 @@

it('should add `locale` to request with default `en`', () => {
it('should add `locale` to request with supported `de`', () => {
return server

@@ -53,7 +55,7 @@ .inject({

.should.be.fulfilled.then((response) => {
expect(response.request.locale).to.be.equal('en');
expect(response.request.locale).to.be.equal('de');
});
});
it('should add `locale` to request with default `en` / query param with unsupported value', () => {
it('should add `locale` to request with supported `de` / query param', () => {
return server

@@ -63,33 +65,6 @@ .inject({

headers: {
'Accept-Language': 'de-DE,de;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
'Accept-Language': 'en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
},
})
.should.be.fulfilled.then((response) => {
expect(response.request.locale).to.be.equal('en');
});
});
});
describe('hapi-locale-17 with `locales` option', async () => {
let server;
before(async () => {
server = await setup({
locales: ['de', 'en']
});
});
after(async () => {
server.stop();
});
it('should add `locale` to request with supported `de`', () => {
return server
.inject({
url: '/test',
headers: {
'Accept-Language': 'de-DE,de;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
},
})
.should.be.fulfilled.then((response) => {
expect(response.request.locale).to.be.equal('de');

@@ -99,12 +74,12 @@ });

it('should add `locale` to request with supported `de` / query param', () => {
it('should add `locale` to request with default `es` / query param with unsupported locale', () => {
return server
.inject({
url: '/test?locale=de',
url: '/test?locale=tr',
headers: {
'Accept-Language': 'en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
'Accept-Language': 'q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
},
})
.should.be.fulfilled.then((response) => {
expect(response.request.locale).to.be.equal('de');
expect(response.request.locale).to.be.equal('es');
});

@@ -111,0 +86,0 @@ });

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