Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,24 @@
---
title: Detect language
type: snippet
language: javascript
tags: [browser]
cover: accessibility
dateModified: 2020-10-06T18:47:16+03:00
---
Detects the preferred language of the current user.
- Use `Navigator.language` or the first value of `Navigator.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'
```