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.
npm install browser-localization-detector
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}`);
});
-
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.
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');
});
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
.
This library is licensed under the MIT License. See the LICENSE file for details.
For any questions or issues, please open an issue on GitHub. Contributions are welcome!