Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(locale): fallback to device preferences instead of 'en' #864

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ShGKme
Copy link
Contributor

@ShGKme ShGKme commented Feb 15, 2025

When language or locale is not available, @nextcloud/l10n fallbacks to 'en'.

Instead, fallback to the environment (aka Web-Brrowser/OS) settings.

Alternative: fallback locale to language instead of system locale.

@ShGKme ShGKme added type: bug 🐛 Something isn't working 3. to review 3️⃣ Waiting for reviews labels Feb 15, 2025
@ShGKme ShGKme requested review from susnux and Antreesy February 15, 2025 18:45
@ShGKme ShGKme self-assigned this Feb 15, 2025
@ShGKme ShGKme force-pushed the fix/fallback-to-navigator-language branch from 4fefcdb to 5ccded6 Compare February 15, 2025 20:22
/**
* Returns the user's locale
*/
export function getLocale(): string {
return document.documentElement.dataset.locale || 'en'
return document.documentElement.dataset.locale || environmentLocale.replace(/-/g, '_')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only replaces the first instance of - but BCP47 allows also adding the script which might become a thing for us. Because there are some languages using Latin and Cyrillic script. One popular one is Serbian.

So possible variants:

  • sr-Latn-RS
  • sr-Cyrl-RS

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Language is not locale, probably better:

Suggested change
return document.documentElement.lang || environmentLocale
return document.documentElement.lang || navigator.language || navigator.userLanguage

setLanguage('zu')
expect(getLanguage()).toBe('zu')
it('returns the environment locale with underscore if no locale is set', () => {
const environmentLocale = Intl.DateTimeFormat().resolvedOptions().locale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this should be mocked otherwise there is no control what this value will be


it('Returns extended locales with hyphens', () => {
setLocale('az_Cyrl_AZ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are loosing the test case of having the script information in the locale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review 3️⃣ Waiting for reviews type: bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants