![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
preferred-locale
Advanced tools
Get the users' most preferred locale/language from your app's available translations with zero dependencies
🎌 Get the users' most preferred locale/language from your app's available translations with zero dependencies
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
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.
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:
en-GB
is translated).en-AU
becomes en-US
),en
), the canonical region is looked up and tested against (e.g en
becomes en-US
)A step-by-step demonstration of how preferred-locale@1
works with your own browser locales is available at eehz9.csb.app.
Application has translations for en-US
and ja-JP
Raw browser locales [ 'en-GB', 'en', 'ja-JP', 'en-US', 'ja' ]
Unify the browser locales [ 'en-GB', 'en-US', 'ja-JP', 'en-US', 'ja-JP' ]
Deduplicate the locales [ 'en-GB', 'en-US', 'ja-JP' ]
Remove locales not translated [ 'en-US', 'ja-JP' ]
User gets content in en-US
See CONTRIBUTING
See Github Releases
All code released under MIT
FAQs
Get the users' most preferred locale/language from your app's available translations with zero dependencies
We found that preferred-locale demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.