Skip to content

Commit f05424c

Browse files
committed
feat: use system language when no locale has been chosen
1 parent 1872952 commit f05424c

9 files changed

Lines changed: 42 additions & 35 deletions

File tree

‎apps/app-frontend/src/App.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import {
151151
setRestartAfterPendingUpdate,
152152
} from '@/helpers/utils.js'
153153
import { start_join_server, start_join_singleplayer_world } from '@/helpers/worlds.ts'
154-
import i18n, { setLocale } from '@/i18n.config'
154+
import i18n, { detectSystemLocale, setLocale } from '@/i18n.config'
155155
import {
156156
instanceKeys,
157157
instanceListQueryOptions,
@@ -703,10 +703,7 @@ async function setupApp() {
703703
pending_update_toast_for_version,
704704
} = await traceStartupStep('Read startup settings', getSettings)
705705
706-
// Initialize locale from saved settings
707-
if (locale) {
708-
await traceStartupStep('Apply startup locale', () => setLocale(locale))
709-
}
706+
await traceStartupStep('Apply startup locale', () => setLocale(locale || detectSystemLocale()))
710707
711708
Object.assign(appSettings.featureFlags, feature_flags)
712709
isMaximized.value = await traceStartupStep('Read window maximized state', () =>
@@ -1009,15 +1006,15 @@ watch(
10091006
if (appTheme.syncAcrossDevices && appTheme.preferred !== selectedTheme) {
10101007
appTheme.preferred = selectedTheme
10111008
}
1012-
if (i18n.global.locale.value !== locale) {
1009+
if (locale && i18n.global.locale.value !== locale) {
10131010
await setLocale(locale)
10141011
}
10151012
10161013
if (appTheme.syncAcrossDevices && settings.theme !== selectedTheme) {
10171014
settings.theme = selectedTheme
10181015
settingsChanged = true
10191016
}
1020-
if (settings.locale !== locale) {
1017+
if (locale && settings.locale !== locale) {
10211018
settings.locale = locale
10221019
settingsChanged = true
10231020
}

‎apps/app-frontend/src/i18n.config.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createMessageCompiler,
44
type CrowdinMessages,
55
LOCALES,
6+
matchLocale,
67
} from '@modrinth/ui'
78
import englishUi from '@modrinth/ui/src/locales/en-US/index.json'
89
import { createI18n } from 'vue-i18n'
@@ -42,6 +43,10 @@ async function fetchMessages(url: string): Promise<CrowdinMessages> {
4243
return response.json()
4344
}
4445

46+
export function detectSystemLocale(): string {
47+
return matchLocale(navigator.languages) ?? 'en-US'
48+
}
49+
4550
export async function setLocale(requestedLocale: string): Promise<void> {
4651
const request = ++localeRequest
4752
const locale = LOCALES.some((candidate) => candidate.code === requestedLocale)

‎apps/frontend/src/app.vue‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ watch(
6666
rememberStoredAccountAppearance(userId, preferences.appearance)
6767
}
6868
69-
if (locale.value !== preferences.localization.locale) {
70-
void setLocale(preferences.localization.locale)
69+
const accountLocale = preferences.localization.locale
70+
if (accountLocale && locale.value !== accountLocale) {
71+
void setLocale(accountLocale)
7172
}
7273
7374
cosmetics.value.searchDisplayMode.mod = preferences.layouts.mods === 'rows' ? 'list' : 'grid'

‎apps/frontend/src/plugins/i18n.ts‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
I18N_INJECTION_KEY,
55
type I18nContext,
66
LOCALES,
7+
matchLocale,
78
transformCrowdinMessages,
89
uiLocaleModules,
910
useDebugLogger,
@@ -127,20 +128,7 @@ async function loadLocale(code: string): Promise<void> {
127128
}
128129

129130
function parseAcceptLanguage(header: string): string | null {
130-
try {
131-
for (const lang of header
132-
.split(',')
133-
.map((l) => l.split(';')[0]?.trim())
134-
.filter(Boolean)) {
135-
const exact = LOCALES.find((loc) => loc.code === lang)
136-
if (exact) return exact.code
137-
const prefix = LOCALES.find((loc) => loc.code.startsWith(lang.split('-')[0] + '-'))
138-
if (prefix) return prefix.code
139-
}
140-
} catch {
141-
// Malformed header, ignore
142-
}
143-
return null
131+
return matchLocale(header.split(',').map((l) => l.split(';')[0]))
144132
}
145133

146134
export default defineNuxtPlugin({

‎apps/labrinth/src/models/v3/preferences.rs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,11 @@ pub enum Theme {
8989
Retro,
9090
}
9191

92-
#[derive(Debug, Serialize, Deserialize, ToSchema, PartialEq, Component)]
92+
#[derive(
93+
Debug, Serialize, Deserialize, ToSchema, Default, PartialEq, Component,
94+
)]
9395
pub struct LocalizationPreferences {
94-
pub locale: String,
95-
}
96-
97-
impl Default for LocalizationPreferences {
98-
fn default() -> Self {
99-
Self {
100-
locale: "en-US".to_owned(),
101-
}
102-
}
96+
pub locale: Option<String>,
10397
}
10498

10599
#[derive(Debug, Serialize, Deserialize, ToSchema, PartialEq, Component)]

‎packages/api-client/src/modules/labrinth/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ export namespace Labrinth {
18341834
}
18351835

18361836
export type LocalizationPreferences = {
1837-
locale: string
1837+
locale: string | null
18381838
}
18391839

18401840
export type LayoutPreferences = {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UPDATE settings SET locale = '' WHERE locale = 'en-US';

‎packages/ui/src/composables/i18n.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ export function transformCrowdinMessages(messages: CrowdinMessages): Record<stri
233233

234234
const LOCALE_CODES = new Set(LOCALES.map((l) => l.code))
235235

236+
/**
237+
* Picks the best supported locale for a list of language tags, most preferred first.
238+
* Falls back to the same language, preferring its home region (e.g. `de` becomes `de-DE`).
239+
*/
240+
export function matchLocale(languages: readonly string[]): string | null {
241+
for (const language of languages) {
242+
const tag = language.trim().toLowerCase()
243+
if (!tag) continue
244+
245+
const exact = LOCALES.find((l) => l.code.toLowerCase() === tag)
246+
if (exact) return exact.code
247+
248+
const base = tag.split('-')[0]
249+
const sameLanguage = LOCALES.filter((l) => l.code.toLowerCase().startsWith(`${base}-`))
250+
const match =
251+
sameLanguage.find((l) => l.code.toLowerCase() === `${base}-${base}`) ?? sameLanguage[0]
252+
if (match) return match.code
253+
}
254+
return null
255+
}
256+
236257
/**
237258
* Builds locale messages from glob-imported modules.
238259
* Only includes locales that are defined in the LOCALES array.

‎packages/ui/src/layouts/wrapped/settings/language-settings/index.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ watch(
9999
preferences,
100100
(value) => {
101101
if (!value || hasChanges.value) return
102-
persistedLocale.value = value.localization.locale
102+
persistedLocale.value = value.localization.locale ?? locale.value
103103
},
104104
{ immediate: true, flush: 'sync' },
105105
)

0 commit comments

Comments
 (0)