All Projects → wojtekmaj → get-user-locale

wojtekmaj / get-user-locale

Licence: MIT License
A function that returns user's locale as an IETF language tag, based on all available sources.

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to get-user-locale

LorittaLocales
🌎 Loritta's localization files, bringing Loritta's cuteness to everyone around the world!
Stars: ✭ 21 (-52.27%)
Mutual labels:  locale
laravel-localizer
Automatically detect and set an app locale that matches your visitor's preference.
Stars: ✭ 34 (-22.73%)
Mutual labels:  locale
pastey
A lightweight, self-hosted paste platform
Stars: ✭ 65 (+47.73%)
Mutual labels:  language-detection
jstarcraft-nlp
专注于解决自然语言处理领域的几个核心问题:词法分析,句法分析,语义分析,语种检测,信息抽取,文本聚类和文本分类. 为相关领域的研发人员提供完整的通用设计与参考实现. 涵盖了多种自然语言处理算法,适配了多个自然语言处理框架. 兼容Lucene/Solr/ElasticSearch插件.
Stars: ✭ 92 (+109.09%)
Mutual labels:  language-detection
laravel-localized-routes
A convenient way to set up and use localized routes in a Laravel app.
Stars: ✭ 257 (+484.09%)
Mutual labels:  locale
go-locale
GoLang library used to retrieve the current locale(s) of the operating system.
Stars: ✭ 16 (-63.64%)
Mutual labels:  locale
ember-best-language
🏳 A FastBoot-enabled addon to detect the best language for your user.
Stars: ✭ 18 (-59.09%)
Mutual labels:  locale
Command-line-translator
Command-line access to google translate and some other features
Stars: ✭ 26 (-40.91%)
Mutual labels:  language-detection
spacy-fastlang
Language detection using Spacy and Fasttext
Stars: ✭ 34 (-22.73%)
Mutual labels:  language-detection
UniLang
Translate text from one language to another using Google Translate
Stars: ✭ 33 (-25%)
Mutual labels:  language-detection
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (-22.73%)
Mutual labels:  locale
tongue
Elixir port of Nakatani Shuyo's natural language detector
Stars: ✭ 17 (-61.36%)
Mutual labels:  language-detection
GDCalendar
Calendar component with RTL languages written in swift
Stars: ✭ 27 (-38.64%)
Mutual labels:  locale
SwiftUIMLKitTranslator
SwiftUI MLKit Language Identification & Translator
Stars: ✭ 23 (-47.73%)
Mutual labels:  language-detection
flutter-devicelocale
A Flutter package to read and return the set device locales
Stars: ✭ 45 (+2.27%)
Mutual labels:  locale
awesome-translations
😎 Awesome lists about Internationalization & localization stuff. l10n, g11n, m17n, i18n. Translations! 🌎🌍
Stars: ✭ 54 (+22.73%)
Mutual labels:  locale
nlpserver
NLP Web Service
Stars: ✭ 76 (+72.73%)
Mutual labels:  language-detection
rosetta
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
Stars: ✭ 23 (-47.73%)
Mutual labels:  locale
language-identification-template
Detect the languages from short pieces of text
Stars: ✭ 20 (-54.55%)
Mutual labels:  language-detection
local.js
Localization script for web sites which has the multiple languages.
Stars: ✭ 15 (-65.91%)
Mutual labels:  locale

npm downloads CI tested with jest

Get-User-Locale

A function that returns user's locale as an IETF language tag, based on all available sources.

tl;dr

  • Install by executing npm install get-user-locale or yarn add get-user-locale.
  • Import by adding import getUserLocale from 'get-user-locale'.
  • Do stuff with it!
    const userLocale = getUserLocale();

User guide

getUserLocale()

A function that returns user's preferred locale as an IETF language tag, based on all available sources.

Sample result

'de-DE';

Usage

import getUserLocale from 'get-user-locale';

or

import { getUserLocale } from 'get-user-locale';

getUserLocales()

A function that returns an array of user's preferred locales as an IETF language tags, based on all available sources.

Sample result

['de-DE', 'de', 'en-US', 'en'];

Usage

import { getUserLocales } from 'get-user-locale';

Technical details

There are at least five ways of determining user's locale:

  • window.navigator.languages
  • window.navigator.language
  • window.navigator.userLanguage
  • window.navigator.browserLanguage
  • window.navigator.systemLanguage

...languages is an array of strings, the others are strings. Some browsers return mixed-case IETF language tags (e.g. de-DE), while others return lowercase ones (e.g. de-de). Finally, non-browser environments will not return anything, so you need a fallback.

Get-User-Locale does the following:

  • Combines all of them into one sane set of locales - in that particular order,
  • Dedupes them,
  • Fixes invalid, lowercased IETF language tags (so that the part after - is always uppercased),
  • Adds a fallback to en-US, so if all else fails, you will get a result that won't crash your app.

License

The MIT License.

Author

Wojciech Maj
[email protected]
https://wojtekmaj.pl
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].