Giter VIP home page Giter VIP logo

preferred-locale's Introduction

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



preferred-locale's People

Contributors

dependabot[bot] avatar renovate[bot] avatar wopian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

preferred-locale's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Use matchDepNames instead of matchPackageNames

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • build(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/auto-assign.yml
  • pozil/auto-assign-issue v1
.github/workflows/cd.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/cache v4
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/lockfile.yml
  • actions/checkout v4
  • Simek/yarn-lock-changes 23b5437388098454b9d9c1574b50066b3338dbf1
npm
package.json
  • @ava/typescript ~4.1.0
  • @mxssfd/typedoc-theme ~1.1.0
  • @rushstack/eslint-patch ~1.10.0
  • @semantic-release/changelog ~6.0.2
  • @semantic-release/commit-analyzer ~11.1.0
  • @semantic-release/github ~9.2.0
  • @semantic-release/npm ~11.0.0
  • @semantic-release/release-notes-generator ~12.1.0
  • @types/node ~20.12.0
  • @typescript-eslint/eslint-plugin ~6.21.0
  • @typescript-eslint/parser ~6.21.0
  • ava ~6.1.0
  • c8 ~9.1.0
  • eslint ~8.57.0
  • eslint-config-prettier ~9.1.0
  • eslint-plugin-import ~2.29.0
  • eslint-plugin-prettier ~5.1.0
  • eslint-plugin-simple-import-sort ~10.0.0
  • eslint-plugin-unicorn ~49.0.0
  • prettier ~3.2.0
  • semantic-release ~23.0.0
  • tsx ~4.9.0
  • typedoc ~0.25.0
  • typescript ~5.4.0
  • node >= 14
  • yarn 4.2.1

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Preset is invalid JSON (local>wopian/renovate-config)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.