包详细信息

preferred-locale

wopian897MIT2.0.2

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

language, language-detection, detect, locale

自述文件

Preferred Locale

checks sponsor

types repoDependants devDeps

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

#

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 Package
Size
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



更新日志

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.0.10 (2020-08-04)

Chores

  • release: update documentation (49cef7c)
  • add funding field to package.json (30d1651)

1.0.9 (2020-07-26)

Chores

  • release: update documentation (d219033)

Documentation Changes

  • add typescript types badge (ad36e07)

1.0.6 (2020-07-26)

Chores

  • release: update documentation (ebea0bb)

Documentation Changes

  • include github action badges (a53774a)

Tests

  • don't run 'Intl.Locale supported' tests on Node versions that do not support Intl.Locale (1a1df51)

1.0.5 (2020-07-18)

Bug Fixes

  • isLanguageAvailable: correct typo in assigned variable name (950c72e)

Chores

  • release: update documentation (705ab27)

Tests

  • deduplicate: full line coverage (c0a0bd8)
  • userLocales: full line coverage (2d50bf1)
  • userLocales: remove test case (8be71cf)

1.0.4 (2020-07-14)

Bug Fixes

  • isLanguageAvailable: do not return regionless-locales in older browsers with languageOnly set to false (fa46526)

Chores

  • release: update documentation (f496898)

1.0.3 (2020-07-14)

Bug Fixes

  • userLocales: guard against window on node enviroments (96d590c)

Chores

  • release: update documentation (ac80716)

1.0.2 (2020-07-14)

Bug Fixes

  • userLocales: check Intl is implemented before checking if DateTimeFormat is implemented (192012e)

Build System / Dependencies

  • add package keywords (f07bd15)
  • do not publish types for internal functions (ab45273)

Chores

  • release: update documentation (3d415b1)
  • release: update documentation (847858a)

Documentation Changes

  • update package description (78b4a33)

1.0.1 (2020-07-14)

Chores

  • release: update documentation (8ead7cd)

Documentation Changes

1.0.0 (2020-07-13)

Chores

  • release: update documentation (34f813b)
  • release: update documentation (89760fb)

Continuous Integration

  • update workflow configs (b8cef20)

Tests

  • add Intl.Locale fallback tests (9e1fa33)

0.1.0 (2020-07-13)

Build System / Dependencies

Chores

  • release: update documentation (7c13428)
  • release: update documentation (a5bf738)
  • add configuration (5993518)
  • add fallback for older browsers (200d1eb)

Continuous Integration

  • size-limit: remove brotli config (4a161ce)
  • add github action for package size limits (32113cf)
  • typo: add config and exclude words (c919a70)

Documentation Changes

New Features

  • implement modern implementation using Intl.Locale (78002b1)

Tests

  • mark internal methods as private (2b08539)