browser-localization-detector
v1.0.2
Published
Detects and sets the user's preferred language for web applications.
Downloads
3
Maintainers
Readme
browser-localization-detector
This library provides a simple and efficient way to detect the preferred language of a user's browser in a Node.js
environment. It utilizes the accept-language-parser
library to parse the Accept-Language
header of an HTTP request
and extract the user's preferred language.
Installation
npm install browser-localization-detector
Usage
const detectPreferredLanguage = require('browser-localization-detector');
// Example usage in an Express.js route handler
app.get('/', (req, res) => {
const preferredLanguage = detectPreferredLanguage(req);
res.send(`Your preferred language is: ${preferredLanguage}`);
});
API
detectPreferredLanguage(req)
req
: HTTP request object.
Returns the user's preferred language code based on the Accept-Language
header of the provided request object (req
).
If no language is detected, null
is returned.
Example
const express = require('express');
const detectPreferredLanguage = require('browser-localization-detector');
const app = express();
app.get('/', (req, res) => {
const preferredLanguage = detectPreferredLanguage(req);
res.send(`Your preferred language is: ${preferredLanguage}`);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Note
This library is designed to be used in a Node.js environment and does not support browser-side JavaScript. If you
require browser-side localization detection, consider using libraries such as i18next
in conjunction
with i18next-browser-languagedetector
.
License
This library is licensed under the MIT License. See the LICENSE file for details.
Author
For any questions or issues, please open an issue on GitHub. Contributions are welcome!