![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
locales-detector
Advanced tools
This library is for detecting users locales from browser, url or some custom sources. The you can transform these languages to desired formats.
Compared to other libraries, this always return array of locales, not only first one. Thanks to this you can work with fallback to secondary languages.
You can use package as npm module. Just install it:
npm install locales-detector --save
and use it in you project:
const { LocaleResolver, DETECTORS } = require('locales-detector');
console.log((new LocaleResolver([new DETECTORS.NavigatorDetector()])).getLocales());
// output ['es', 'en]
It can be also used with RequireJS, CommonJS, Browserify or Webpack.
LocaleResolver class is core of this library. Constructor have two arguments - array of Detectors and optional array of Transformers.
Detectors are classes that can detect users locales. It always return array of locales (or empty array). You can used prebuild or write you custom.
Prebuild decetors are NavigatorDetector and UrlDetector:
const { LocaleResolver, DETECTORS } = require('locales-detector');
// try to find lang settings in lang GET parameter
const urlDetector = new DETECTORS.UrlDetector('lang');
// try to find lang in browser settings
const navigatorDetector = new DETECTORS.NavigatorDetector();
console.log((new LocaleResolver([urlDetector, navigatorDetector])).getLocales());
// output ['es', 'en]
If you want to build your own detector, create new class with getLocales method that always return array.
class CustomDetector {
getLocales() {
return ['es'];
}
}
Transformers allow you to modify format of locales, remove some you dont want to or create custom callbacks. Same as with Detector you can use some prebuild or create your own.
Prebuild transformers are:
FallbacksTransformer
Create automatic callback for regional specific languages. eg. from ['en-GB'] it will make ['en-GB', 'en'].
IETFTransformer
It convert language format to standardised IETFT. eg. from ['en-gb'] it will make ['en-GB'']
InvalidLocalesTransformer
It convert some unstandard language format like ['english'] => ['en']. There is some default setting but you can set your own convert table.
LanguageOnlyTransformer
If you don't want to bother with regional locales of language, you can just strip it. It make from ['en-GB'] => ['en']
If you want to build your own transformer, create new class with transform method that have flat array of locales as parameter and return flat array of locales.
class CustomTransformer {
transform(locales) {
return locales.map((locale) => {
return locale.toLocaleLowerCase();
});
}
}
In example you can see object that first load locales from url and then from browser settings. It create automatic fallbacks and convert ivalid locales.
const { LocaleResolver, DETECTORS, TRANSFORMERS } = require('locales-detector');
console.log((new LocaleResolver([new DETECTORS.UrlDetector('lang'), new DETECTORS.NavigatorDetector()], [new TRANSFORMERS.InvalidLocalesTransformer(), new TRANSFORMERS.FallbacksTransformer()])).getLocales());
// output ['es-419', 'es', 'en']
FAQs
Tool for detecting locales of visitors and transform them.
The npm package locales-detector receives a total of 0 weekly downloads. As such, locales-detector popularity was classified as not popular.
We found that locales-detector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.