Skip to content

Guesses the user's regional parameters on-device, in a reliable way.

License

Notifications You must be signed in to change notification settings

muratgozel/regionist

Repository files navigation

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

npm i regionist

or inject with script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/regionist@7/dist/regionist.iife.js"></script>

<script type="text/javascript">
    console.log(window.Regionist)
</script>

Usage

guess

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

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 first, please.


Thanks for the attention 💙 Any amount of support on patreon or github will return you back as bug fixes, new features and bits and bytes.