1
0
mirror of https://github.com/jbranchaud/til synced 2026-07-02 23:58:25 +00:00

Add Get User's Preferred Language From Browser as a JavaScript TIL

This commit is contained in:
jbranchaud
2026-05-14 11:27:58 -05:00
parent e58ffffda0
commit 9c65e5c0b3
2 changed files with 36 additions and 1 deletions
+2 -1
View File
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1791 TILs and counting..._ _1792 TILs and counting..._
See some of the other learning resources I work on: See some of the other learning resources I work on:
@@ -622,6 +622,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Get The Response Status From An Axios Error](javascript/get-the-response-status-from-an-axios-error.md) - [Get The Response Status From An Axios Error](javascript/get-the-response-status-from-an-axios-error.md)
- [Get The Time Components Of A Date](javascript/get-the-time-components-of-a-date.md) - [Get The Time Components Of A Date](javascript/get-the-time-components-of-a-date.md)
- [Get The Time Zone Of The Client Computer](javascript/get-the-time-zone-of-the-client-computer.md) - [Get The Time Zone Of The Client Computer](javascript/get-the-time-zone-of-the-client-computer.md)
- [Get User's Preferred Language From Browser](javascript/get-users-preferred-language-from-browser.md)
- [Globally Install A Package With Yarn](javascript/globally-install-a-package-with-yarn.md) - [Globally Install A Package With Yarn](javascript/globally-install-a-package-with-yarn.md)
- [Globally Install Specific Version Of PNPM](javascript/globally-install-specific-version-of-pnpm.md) - [Globally Install Specific Version Of PNPM](javascript/globally-install-specific-version-of-pnpm.md)
- [Immutable Remove With The Spread Operator](javascript/immutable-remove-with-the-spread-operator.md) - [Immutable Remove With The Spread Operator](javascript/immutable-remove-with-the-spread-operator.md)
@@ -0,0 +1,34 @@
# Get User's Preferred Language From Browser
A great way to determine a user's preferred language if you aren't able to ask
them directly is to look at the language setting for their browser's UI.
We can get this from the instance of
[`Navigator`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator) in the
user's JavaScript runtime within the browser.
My browser's UI is set to US English, which I can verify like so:
```javascript
> navigator.language
'en-US'
```
This is useful for all sorts of things like [formatting dates for
display](basic-date-formatting-without-a-library.md):
```javascript
> const now = new Date();
> Intl.DateTimeFormat(navigator.language).format(now)
'5/14/2026'
```
Or for [formatting other kinds of units for
display](formatting-values-with-units-for-display.md):
```javascript
> const milesFormat =
Intl.NumberFormat(navigator.language, { style: "unit", unit: "mile" });
> milesFormat.format(1500)
"1,500 mi"
```