Skip to content

Commit 5ccded6

Browse files
committed
fix(locale): fallback to device preferences instead of 'en'
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
1 parent 353bb78 commit 5ccded6

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

lib/locale.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
55

6+
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
7+
68
/**
79
* Returns the user's locale
810
*/
911
export function getLocale(): string {
10-
return document.documentElement.dataset.locale || 'en'
12+
return document.documentElement.dataset.locale || environmentLocale.replace(/-/g, '_')
1113
}
1214

1315
/**
@@ -22,7 +24,7 @@ export function getCanonicalLocale(): string {
2224
* Returns the user's language
2325
*/
2426
export function getLanguage(): string {
25-
return document.documentElement.lang || 'en'
27+
return document.documentElement.lang || environmentLocale
2628
}
2729

2830
/**

tests/locale.test.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
import { afterEach, beforeEach, describe, expect, it, test } from 'vitest'
5+
import { beforeEach, describe, expect, it } from 'vitest'
66
import {
77
getCanonicalLocale,
88
getLanguage,
@@ -13,54 +13,49 @@ import {
1313
const setLocale = (locale: string) => document.documentElement.setAttribute('data-locale', locale)
1414
const setLanguage = (lang: string) => document.documentElement.setAttribute('lang', lang)
1515

16-
describe('getCanonicalLocale', () => {
17-
afterEach(() => {
18-
setLocale('')
16+
describe('getLanguage', () => {
17+
it('returns the set language as it is', () => {
18+
setLanguage('de-DE')
19+
expect(getLanguage()).toEqual('de-DE')
1920
})
2021

21-
it('Returns primary locales as is', () => {
22-
setLocale('de')
23-
expect(getCanonicalLocale()).toEqual('de')
24-
setLocale('zu')
25-
expect(getCanonicalLocale()).toEqual('zu')
22+
it('returns the environment locale if no language is set', () => {
23+
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
24+
setLanguage('')
25+
expect(getLanguage()).toEqual(environmentLocale)
2626
})
27+
})
2728

28-
it('Returns extended locales with hyphens', () => {
29-
setLocale('az_Cyrl_AZ')
30-
expect(getCanonicalLocale()).toEqual('az-Cyrl-AZ')
29+
describe('getLocale', () => {
30+
it('returns the set locale as it is with underscore', () => {
3131
setLocale('de_DE')
32-
expect(getCanonicalLocale()).toEqual('de-DE')
32+
expect(getLocale()).toEqual('de_DE')
3333
})
34-
})
35-
36-
test('getLanguage', () => {
37-
document.documentElement.removeAttribute('lang')
38-
// Expect fallback
39-
expect(getLanguage()).toBe('en')
40-
setLanguage('')
41-
expect(getLanguage()).toBe('en')
4234

43-
// Expect value
44-
setLanguage('zu')
45-
expect(getLanguage()).toBe('zu')
35+
it('returns the environment locale with underscore if no locale is set', () => {
36+
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
37+
setLocale('')
38+
expect(getLocale()).toEqual(environmentLocale.replace(/-/g, '_'))
39+
})
4640
})
4741

48-
test('getLocale', () => {
49-
document.documentElement.removeAttribute('data-locale')
50-
// Expect fallback
51-
expect(getLocale()).toBe('en')
52-
setLocale('')
53-
expect(getLocale()).toBe('en')
42+
describe('getCanonicalLocale', () => {
43+
it('returns the set locale with hyphen', () => {
44+
setLocale('de_DE')
45+
expect(getCanonicalLocale()).toEqual('de-DE')
46+
})
5447

55-
// Expect value
56-
setLocale('de_DE')
57-
expect(getLocale()).toBe('de_DE')
48+
it('returns the environment locale with hyphen if no locale is set', () => {
49+
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
50+
setLocale('')
51+
expect(getCanonicalLocale()).toEqual(environmentLocale)
52+
})
5853
})
5954

6055
describe('isRTL', () => {
6156
beforeEach(() => document.documentElement.removeAttribute('data-locale'))
6257

63-
it('fallsback to English which is LTR', () => {
58+
it('falls back to English which is LTR', () => {
6459
// Expect fallback which is English = LTR
6560
expect(isRTL()).toBe(false)
6661
})

0 commit comments

Comments
 (0)