Merge pull request #1326 from pataar/patch-2

Add detectLanguage.md
This commit is contained in:
Angelos Chalaris
2020-10-06 18:47:29 +03:00
committed by GitHub

View File

@ -0,0 +1,20 @@
---
title: detectLanguage
tags: browser,intermediate
---
Detects the preferred language of the current user.
- Use `NavigationLanguage.language` or the first `NavigationLanguage.languages` if available, otherwise return `defaultLang`.
- Omit the second argument, `defaultLang`, to use `'en-US'` as the default language code.
```js
const detectLanguage = (defaultLang = 'en-US') =>
navigator.language ||
(Array.isArray(navigator.languages) && navigator.languages[0]) ||
defaultLang;
```
```js
detectLanguage(); // 'nl-NL'
```