https://github.com/funny-bytes/hapi-locale-17
Locale and language detection for HAPI server
https://github.com/funny-bytes/hapi-locale-17
Last synced: 5 months ago
JSON representation
Locale and language detection for HAPI server
- Host: GitHub
- URL: https://github.com/funny-bytes/hapi-locale-17
- Owner: funny-bytes
- Created: 2017-12-15T17:07:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-09-24T12:15:21.000Z (9 months ago)
- Last Synced: 2025-09-24T13:28:37.798Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 1.12 MB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# hapi-locale-17
Locale and language detection for Hapi Server.

[](https://coveralls.io/github/funny-bytes/hapi-locale-17)
[](https://codeclimate.com/github/funny-bytes/hapi-locale-17/maintainability)
[]()
[](https://github.com/airbnb/javascript)
[]()
Evaluates locale information from `accept-language` header and query or path parameter.
Decorates Hapi request object with `request.getLocale()` available in all route handlers.
Priority of evaluation:
(1) `locale` query parameter (if provided),
(2) `locale` path parameter (if provided),
(3) `accept-language` http request header,
(4) fallback locale (the first locale in `locales` list).
Decorated method `request.getLocale()` can be renamed.
Query and path parameters `locale` can be renamed or switched off.
Tested with
* Hapi 20/21 on Node 22
## Install
```bash
npm install hapi-locale-17
```
## Usage
Register the plugin with Hapi server like this:
```js
const Hapi = require('@hapi/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
}
});
await server.start();
};
provision();
```
In your route handler, do something like this:
```js
server.route({
method: 'GET',
path: '/test',
handler: function (request, h) {
const locale = request.getLocale();
// ...
}
});
```
## Options
The plugin provides the following options:
| Option | Default | Description |
|-----------|-------------|-------------|
| `locales` | `[]` | Your list of supported locales, e.g., `['de', 'en']` or `['en-US', 'es-ES']`. |
| `query` | `locale` | Name of query parameter to evaluate. Set to `false` to switch off. |
| `path` | `locale` | Name of path parameter to evaluate. Set to `false` to switch off. |
| `method` | `getLocale` | Name of method for request decoration, i.e., `request.getLocale()`. |