diff --git a/lib/locale.ts b/lib/locale.ts index 74e8469d..4316832c 100644 --- a/lib/locale.ts +++ b/lib/locale.ts @@ -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, '_') } /** @@ -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 } /** diff --git a/tests/locale.test.ts b/tests/locale.test.ts index 369c9a4e..281743cd 100644 --- a/tests/locale.test.ts +++ b/tests/locale.test.ts @@ -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, @@ -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) })