https://github.com/muratgozel/regionist
Guesses the user's regional parameters on-device, in a reliable way.
https://github.com/muratgozel/regionist
country country-detection javascript language-detection localization timezone timezone-detection
Last synced: 11 months ago
JSON representation
Guesses the user's regional parameters on-device, in a reliable way.
- Host: GitHub
- URL: https://github.com/muratgozel/regionist
- Owner: muratgozel
- License: mit
- Created: 2020-02-08T18:52:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-24T01:40:37.000Z (over 2 years ago)
- Last Synced: 2024-06-10T10:55:21.471Z (about 2 years ago)
- Topics: country, country-detection, javascript, language-detection, localization, timezone, timezone-detection
- Language: JavaScript
- Homepage: https://muratgozel.github.io/regionist/
- Size: 1.47 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# regionist
Guesses the user's regional parameters on-device.
This library relies on the `window` object of the user's browser. It guesses timezone, country and the preferred language. It's stateless. Only two methods: `guess` and `match`. As there is no %100 accurate way of detecting user's regional parameters, this library tries its chance by combinating the outputs of `window.Intl` and `window.navigator` objects.
## Install
```sh
npm i regionist
```
or inject with script tag:
```html
console.log(window.Regionist)
```
## Usage
### guess
```js
import { regionist } from 'regionist'
// or
// const { regionist } = require('regionist')
// guess
const guessResult = regionist.guess()
console.assert(guessResult === {
timezone: 'America/New_York', // timezone name as returned by window.Intl object
timezoneCountry: 'US', // mapped from the timezone
preferredLocale: 'tr-TR', // relies on window.navigator
preferredLanguage: 'tr' // it's just derived from preferredLocale
})
```
### match
```js
import { regionist } from 'regionist'
// match, useful for finding the best matching locale against a list of supported locales
const bestLocale = regionist.match(['az-AZ', 'en-US', 'tr-TR'])
console.assert(bestLocale === 'tr-TR') // because user's preferred locale is tr-TR
const bestLocale2 = regionist.match(['az_AZ', 'en_us', 'tr_tr'])
console.assert(bestLocale === 'tr-TR') // always returns formatted
```
## Contributing
If you're interested in contributing, read the [CONTRIBUTING.md](https://github.com/muratgozel/muratgozel/blob/main/CONTRIBUTING.md) first, please.
---
Thanks for the attention 💙 Any amount of support on [patreon](https://patreon.com/muratgozel?utm_medium=organic&utm_source=github_repo&utm_campaign=github&utm_content=join_link) or [github](https://github.com/sponsors/muratgozel) will return you back as bug fixes, new features and bits and bytes.