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

preferred-locale

v2.0.2

Published

Get the users' most preferred locale/language from your app's available translations with zero dependencies

Downloads

2,006

Readme

Features

  • Uses the Intl.Locale API (backwards compatible)
  • Works on node & browsers
  • Zero dependencies
  • TypeScript support

Usage

This library is fully typed with TSDoc examples. View the online documentation here: https://wopian.github.io/preferred-locale/

import { preferredLocale } from 'preferred-locale'

// Note: All examples assume the browser's reported locales are:
// [ 'en-GB', 'en', 'ja-JP', 'en-US', 'ja' ]

const supportedLocales = ['en-US', 'ja-JP']
const fallbackLocale = 'ja-JP'
const locale = preferredLocale(supportedLocales, fallbackLocale)
console.log(locale) // 'en-US', converts 'en-GB' and 'en' to 'en-US' as neither are translated, placing it before 'ja-JP' in preference order

preferredLocale(['en-us', 'fr-fr'], ['en-us'], {
  regionLowerCase: true
}) // 'en-us', converts 'en-GB' to 'en-us' as 'en-gb' is not translated

preferredLocale(['de', 'fr'], ['fr'], {
  languageOnly: true
}) // 'fr', converts 'en-GB' to 'en' (etc). No matching locales so returns 'fr' fallback

preferredLocale(['en-US', 'en-GB'], ['en-US']) // 'en-GB', as it is translated and first in user's preference order

Guaranteed Node / Browser Support

| Package | PackageSize | Node | Chrome | Firefox | Safari | Edge | | -----------------: | :-------------: | :--: | :----: | :-----: | :----: | :--: | | preferred-locale | ~600 bytes | 14+ | 69+ | 68+ | 12+ | 18+ |

preferred-locale@2 is a rewrite of preferred-locale@1, written in TypeScript as a native ESM module. If your environment does not support ESM modules, you can continue to use preferred-locale@1 as the resultant code is identical.

Why?

Many web applications that automatically detect the browser language and serve the relevent translation are fundamentally broken.

A browser that signals the user prefers the following locales (index 0 being most preferred) should never return content in Japanese (ja-JP) if the application has translations for Japanese and American English (en-US):

  • [ 'en-GB', 'en', 'ja-JP', 'en-US', 'ja' ]

Instead, many applications (e.g Epic Games' store, help and documentation) will instead serve their users content in Japanese as they do not provide translations for British English, only American English and only check for exact matches.

preferred-locale fixes this by traversing the supported node/browser languages in order of priority:

  1. If an exact match is found it uses that (e.g en-GB is translated).
  2. If the node/browser language is supported but the region is not (e.g Australian English), the canonical region is looked up and tested against (e.g en-AU becomes en-US),
  3. If only a language is provided (e.g en), the canonical region is looked up and tested against (e.g en becomes en-US)
  4. If no node/browser locale resolves to a translated locale, the fallback locale is returned

Live Demo

A step-by-step demonstration of how preferred-locale@1 works with your own browser locales is available at eehz9.csb.app.

Example Step-By-Step

Application has translations for en-US and ja-JP

  1. Raw browser locales [ 'en-GB', 'en', 'ja-JP', 'en-US', 'ja' ]

  2. Unify the browser locales [ 'en-GB', 'en-US', 'ja-JP', 'en-US', 'ja-JP' ]

  3. Deduplicate the locales [ 'en-GB', 'en-US', 'ja-JP' ]

  4. Remove locales not translated [ 'en-US', 'ja-JP' ]

  5. User gets content in en-US

Contributing

See CONTRIBUTING

Releases

See Github Releases

License

All code released under MIT