Skip to content

Commit

Permalink
fix(locale): fallback to device preferences instead of 'en'
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme committed Feb 15, 2025
1 parent 353bb78 commit 5ccded6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
6 changes: 4 additions & 2 deletions lib/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale

/**
* Returns the user's locale
*/
export function getLocale(): string {
return document.documentElement.dataset.locale || 'en'
return document.documentElement.dataset.locale || environmentLocale.replace(/-/g, '_')
}

/**
Expand All @@ -22,7 +24,7 @@ export function getCanonicalLocale(): string {
* Returns the user's language
*/
export function getLanguage(): string {
return document.documentElement.lang || 'en'
return document.documentElement.lang || environmentLocale
}

/**
Expand Down
63 changes: 29 additions & 34 deletions tests/locale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { afterEach, beforeEach, describe, expect, it, test } from 'vitest'
import { beforeEach, describe, expect, it } from 'vitest'
import {
getCanonicalLocale,
getLanguage,
Expand All @@ -13,54 +13,49 @@ import {
const setLocale = (locale: string) => document.documentElement.setAttribute('data-locale', locale)
const setLanguage = (lang: string) => document.documentElement.setAttribute('lang', lang)

describe('getCanonicalLocale', () => {
afterEach(() => {
setLocale('')
describe('getLanguage', () => {
it('returns the set language as it is', () => {
setLanguage('de-DE')
expect(getLanguage()).toEqual('de-DE')
})

it('Returns primary locales as is', () => {
setLocale('de')
expect(getCanonicalLocale()).toEqual('de')
setLocale('zu')
expect(getCanonicalLocale()).toEqual('zu')
it('returns the environment locale if no language is set', () => {
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
setLanguage('')
expect(getLanguage()).toEqual(environmentLocale)
})
})

it('Returns extended locales with hyphens', () => {
setLocale('az_Cyrl_AZ')
expect(getCanonicalLocale()).toEqual('az-Cyrl-AZ')
describe('getLocale', () => {
it('returns the set locale as it is with underscore', () => {
setLocale('de_DE')
expect(getCanonicalLocale()).toEqual('de-DE')
expect(getLocale()).toEqual('de_DE')
})
})

test('getLanguage', () => {
document.documentElement.removeAttribute('lang')
// Expect fallback
expect(getLanguage()).toBe('en')
setLanguage('')
expect(getLanguage()).toBe('en')

// Expect value
setLanguage('zu')
expect(getLanguage()).toBe('zu')
it('returns the environment locale with underscore if no locale is set', () => {
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
setLocale('')
expect(getLocale()).toEqual(environmentLocale.replace(/-/g, '_'))
})
})

test('getLocale', () => {
document.documentElement.removeAttribute('data-locale')
// Expect fallback
expect(getLocale()).toBe('en')
setLocale('')
expect(getLocale()).toBe('en')
describe('getCanonicalLocale', () => {
it('returns the set locale with hyphen', () => {
setLocale('de_DE')
expect(getCanonicalLocale()).toEqual('de-DE')
})

// Expect value
setLocale('de_DE')
expect(getLocale()).toBe('de_DE')
it('returns the environment locale with hyphen if no locale is set', () => {
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
setLocale('')
expect(getCanonicalLocale()).toEqual(environmentLocale)
})
})

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

it('fallsback to English which is LTR', () => {
it('falls back to English which is LTR', () => {
// Expect fallback which is English = LTR
expect(isRTL()).toBe(false)
})
Expand Down

0 comments on commit 5ccded6

Please sign in to comment.