npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

user-locale

v0.17.0

Published

Get the default locale from the user's browser and system

Downloads

28

Readme

user-locale

user-locale provides functions to retrieve default locale parameters from the user's browser and system.

The functions allow to retrieve:

  • the date format;
  • the time format;
  • the number format;
  • the preferred language tags (en-US, en, …);
  • the time zone;
  • the guessed country (from the time zone and preferred language tags);
  • the first day of the week (from the guessed country);
  • the color scheme (as configured in the OS or user agent).

Usage

Date format

getDateFormat() returns the order of the date components and the separator of the components.

import { DateEndianness, DateFormat, getDateFormat } from 'user-locale';

getDateFormat()

A return value looks like:

const dateFormat: DateFormat = {
  endianness: DateEndianness.LittleEndian,
  separator: '/'
}

The endianness property allows to know in what order the date components are placed:

  • DateEndianness.BigEndian: year, month, day
  • DateEndianness.LittleEndian: day, month, year
  • DateEndianness.MiddleEndian: month, day, year

Time format

getTimeFormat() returns whether the clock is the 12-hour clock or 24-hour clock, and the separator of the time components.

import { getTimeFormat, TimeFormat } from 'user-locale';

getTimeFormat()

A return value looks like:

const timeFormat: TimeFormat = {
  is24HourClock: true,
  separator: ':'
}

Number format

getNumberFormat() returns a value from the NumberFormat enum, which indicates the characters of the thousands grouping and the decimal separator.

import { getNumberFormat, NumberFormat } from 'user-locale';

getNumberFormat()

The possible returned values are:

NumberFormat.CommaPeriod // e.g. for locale 'en-US'
NumberFormat.PeriodComma // e.g. for locale 'de-DE'
NumberFormat.SpaceComma // e.g. for locale 'fr-FR'

Preferred language tags

getPreferredLanguageTags returns the preferred (BCP 47) language tags.

import { getPreferredLanguageTags } from 'user-locale';

getPreferredLanguageTags() // ['en-US', 'en']

user-locale includes a convenient utility function, getNativeLanguageNames(), which returns an array of objects containing language names and their corresponding tags. Each language name is written in its native language.

import { getNativeLanguageNames } from 'user-locale';

getNativeLanguageNames()

You can optionally provide a list of specific language tags to filter the results:

import { getNativeLanguageNames } from 'user-locale';

getNativeLanguageNames('fr', 'en') // [{ "tag": "en", "name": "English" }, { "tag": "fr", "name": "Français" }]

Time zone

getTimeZone returns the time zone.

import { getTimeZone } from 'user-locale';

getTimeZone() // 'Asia/Bangkok'

Country

guessCountryCode guesses the country from the time zone and preferred language tags.

import { guessCountryCode } from 'user-locale';

guessCountryCode() // ['KH', 'LA', 'TH', 'VN']

The function returns an array of possible countries. It first gets the list of countries which use the user's time zone (for instance, "Asia/Bangkok" is used in Thailand, Vietnam, Laos and Cambodia), then filters out this list by any country code present in the preferred language tags (for instance, ['th', 'th-TH'] includes the country code of Thailand).

If the function returns only one element, it is fairly likely that the identified country is correct.

First day of the week

getFirstDayOfWeek returns the first day of the week, based on the country that has been guessed.

import { FirstDayOfWeek, getFirstDayOfWeek } from 'user-locale';

getFirstDayOfWeek() // FirstDayOfWeek.Monday

Color scheme

getColorScheme returns the color scheme as configured in the OS or user agent.

import { ColorScheme, getColorScheme } from 'user-locale';

getColorScheme() // ColorScheme.Dark

Utility functions

import { dateFormatter, dateTimeFormatter, timeFormatter, numberFormatter } from 'user-locale';

numberFormatter(NumberFormat.PeriodComma)(1000.01) // '1.000,01'

dateFormatter({ endianness: DateEndianness.LittleEndian, separator: '/' })(Temporal.PlainDate.from('2023-09-02')) // '02/09/2023'

timeFormatter({ is24HourClock: true, separator: ':' }, { precision: 'minute' })(Temporal.PlainTime.from('13:30:05')) // '13:30'

const formatDate = dateFormatter({ endianness: DateEndianness.LittleEndian, separator: '/' })
const formatTime = timeFormatter({ is24HourClock: false, separator: ':' }, { precision: 'second', omitZeroUnits: true })
dateTimeFormatter(formatDate, formatTime)(Temporal.PlainDateTime.from('2023-09-02 00:00:00')) // '02/09/2023 12 AM'

Install

You can get user-locale via npm.

npm install user-locale