Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(i18n): modularize locale configurations, improve type safety, and optimize language loading

johaven3b05b1b88502
diff --git a/backend/src/applications/notifications/i18n/index.ts b/backend/src/applications/notifications/i18n/index.ts
index feb4789f..e143d05a 100644
--- a/backend/src/applications/notifications/i18n/index.ts
+++ b/backend/src/applications/notifications/i18n/index.ts
@@ -4,6 +4,7 @@
* See the LICENSE file for licensing details
*/
+import type { i18nLocaleSupported } from '../../../common/interfaces'
import { de } from './de'
import { es } from './es'
import { fr } from './fr'
@@ -18,7 +19,7 @@ import { ru } from './ru'
import { tr } from './tr'
import { zh } from './zh'
-export const translations = { de, es, fr, hi, it, ja, ko, pl, pt, ru, tr, zh, 'pt-BR': pt_BR }
+export const translations: Record<Exclude<i18nLocaleSupported, 'en'>, any> = { de, es, fr, hi, it, ja, ko, pl, pt, ru, tr, zh, 'pt-BR': pt_BR }
export function translateObject<T>(language: string, obj: T): T {
if (language?.length && Object.keys(translations).indexOf(language) > -1) {
diff --git a/backend/src/common/interfaces.ts b/backend/src/common/interfaces.ts
index 4cd45594..87bf8bb4 100644
--- a/backend/src/common/interfaces.ts
+++ b/backend/src/common/interfaces.ts
@@ -10,3 +10,5 @@ export interface StorageQuota {
storageUsage: number
storageQuota: number
}
+
+export type i18nLocaleSupported = 'de' | 'en' | 'es' | 'fr' | 'hi' | 'it' | 'ja' | 'ko' | 'pl' | 'pt' | 'pt-BR' | 'ru' | 'tr' | 'zh'
diff --git a/frontend/angular.json b/frontend/angular.json
index 0217fd1c..3b54cc2d 100644
--- a/frontend/angular.json
+++ b/frontend/angular.json
@@ -69,7 +69,8 @@
"optimization": false,
"extractLicenses": false,
"sourceMap": true,
- "namedChunks": true
+ "namedChunks": true,
+ "verbose": false
}
},
"defaultConfiguration": "production"
diff --git a/frontend/src/app/applications/users/components/user-account.component.ts b/frontend/src/app/applications/users/components/user-account.component.ts
index 7b5eef37..9bff9a90 100644
--- a/frontend/src/app/applications/users/components/user-account.component.ts
+++ b/frontend/src/app/applications/users/components/user-account.component.ts
@@ -234,7 +234,6 @@ export class UserAccountComponent implements OnDestroy {
private updateLanguage(language: string) {
this.user.language = language
- this.layout.setLanguage(language)
- this.layout.sendNotification('info', 'Configuration', 'Language updated')
+ this.layout.setLanguage(language).then(() => this.layout.sendNotification('info', 'Configuration', 'Language updated'))
}
}
diff --git a/frontend/src/app/applications/users/user.constants.ts b/frontend/src/app/applications/users/user.constants.ts
index ed491fad..c97aff38 100644
--- a/frontend/src/app/applications/users/user.constants.ts
+++ b/frontend/src/app/applications/users/user.constants.ts
@@ -43,7 +43,7 @@ export const USER_ICON = {
export const USER_PASSWORD_CHANGE_TEXT = 'Change me !'
-export const USER_LANGUAGE_AUTO = 'auto'
+export const USER_LANGUAGE_AUTO = 'auto' as const
export const USER_MENU: AppMenu = {
title: USER_TITLE.ACCOUNT,
diff --git a/frontend/src/app/applications/users/user.service.ts b/frontend/src/app/applications/users/user.service.ts
index 68316ba8..39f6157b 100644
--- a/frontend/src/app/applications/users/user.service.ts
+++ b/frontend/src/app/applications/users/user.service.ts
@@ -97,7 +97,7 @@ export class UserService {
initUser(user: UserType, impersonate = false) {
this.refreshAvatar()
- this.layout.setLanguage(user.language)
+ this.layout.setLanguage(user.language).catch(console.error)
this.store.userImpersonate.set(impersonate || user.impersonated)
this.store.user.next(user)
this.checkQuota(this.store.user.getValue())
diff --git a/frontend/src/app/layout/layout.service.ts b/frontend/src/app/layout/layout.service.ts
index a43d5f75..4b6e5e82 100644
--- a/frontend/src/app/layout/layout.service.ts
+++ b/frontend/src/app/layout/layout.service.ts
@@ -221,13 +221,14 @@ export class LayoutService {
}
}
- setLanguage(language: string) {
+ setLanguage(language: string): Promise<void> {
if (!language || language === USER_LANGUAGE_AUTO) {
language = getBrowserLanguage(LANG_FORMAT) || ''
}
if (language && language !== this.getCurrentLanguage()) {
- this.translation.setLocale({ language }).catch(console.error)
+ return this.translation.setLocale({ language })
}
+ return Promise.resolve()
}
getCurrentLanguage() {
diff --git a/frontend/src/i18n/l10n.ts b/frontend/src/i18n/l10n.ts
index 3c596d79..8316e149 100755
--- a/frontend/src/i18n/l10n.ts
+++ b/frontend/src/i18n/l10n.ts
@@ -5,6 +5,7 @@
*/
import { inject, Injectable } from '@angular/core'
+import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
import {
getBrowserLanguage,
L10N_LOCALE,
@@ -15,62 +16,18 @@ import {
L10nStorage,
L10nTranslationLoader
} from 'angular-l10n'
-import {
- defineLocale,
- deLocale,
- enGbLocale,
- esLocale,
- frLocale,
- hiLocale,
- itLocale,
- jaLocale,
- koLocale,
- plLocale,
- ptBrLocale,
- ruLocale,
- trLocale,
- zhCnLocale
-} from 'ngx-bootstrap/chronos'
import { BsLocaleService } from 'ngx-bootstrap/datepicker'
import { from, Observable, of } from 'rxjs'
import { catchError, map } from 'rxjs/operators'
-import 'dayjs/esm/locale/de'
-import 'dayjs/esm/locale/en'
-import 'dayjs/esm/locale/es'
-import 'dayjs/esm/locale/fr'
-import 'dayjs/esm/locale/hi'
-import 'dayjs/esm/locale/it'
-import 'dayjs/esm/locale/ja'
-import 'dayjs/esm/locale/ko'
-import 'dayjs/esm/locale/pl'
-import 'dayjs/esm/locale/pt'
-import 'dayjs/esm/locale/pt-br'
-import 'dayjs/esm/locale/ru'
-import 'dayjs/esm/locale/tr'
-import 'dayjs/esm/locale/zh'
import { USER_LANGUAGE_AUTO } from '../app/applications/users/user.constants'
-import { dJs } from '../app/common/utils/time'
-
-// BOOTSTRAP LOCALES
-defineLocale('de', deLocale)
-defineLocale('en', enGbLocale)
-defineLocale('es', esLocale)
-defineLocale('fr', frLocale)
-defineLocale('hi', hiLocale)
-defineLocale('it', itLocale)
-defineLocale('ja', jaLocale)
-defineLocale('ko', koLocale)
-defineLocale('pl', plLocale)
-defineLocale('pt', ptBrLocale)
-defineLocale('pt-br', ptBrLocale)
-defineLocale('ru', ruLocale)
-defineLocale('tr', trLocale)
-defineLocale('zh', zhCnLocale)
+import { loadBootstrapLocale } from './lib/bs.i18n'
+import { loadDayjsLocale } from './lib/dayjs.i18n'
export const LANG_FORMAT: L10nFormat = 'language-region' as const
export const STORAGE_SESSION_KEY = 'locale' as const
+export const LANG_DEFAULT: i18nLocaleSupported = 'en'
-export const i18nLanguageText = {
+export const i18nLanguageText: Record<i18nLocaleSupported | typeof USER_LANGUAGE_AUTO, string> = {
[USER_LANGUAGE_AUTO]: 'Auto',
de: 'Deutsch',
en: 'English',
@@ -88,14 +45,16 @@ export const i18nLanguageText = {
zh: '中文(简体) '
}
-export const l10nConfig: L10nConfig = {
+export const l10nConfig: L10nConfig & {
+ schema: { locale: { language: i18nLocaleSupported }; dir: 'ltr' | 'rtl' }[]
+} = {
format: LANG_FORMAT,
// Provider without static asset: resources will be loaded through TranslationLoader
providers: [{ name: 'app', asset: 'app' }],
fallback: false,
cache: true,
keySeparator: '|',
- defaultLocale: { language: 'en' },
+ defaultLocale: { language: LANG_DEFAULT },
schema: [
{ locale: { language: 'de' }, dir: 'ltr' },
{ locale: { language: 'en' }, dir: 'ltr' },
@@ -165,7 +124,8 @@ export class TranslationLoader implements L10nTranslationLoader {
language = language.split('-')[0]
}
if (Object.hasOwn(i18nLanguageText, language)) {
- dJs.locale(language)
+ loadDayjsLocale(language).catch(console.error)
+ loadBootstrapLocale(language)
this.bsLocale.use(language)
} else {
return of({})
@@ -184,7 +144,7 @@ export class TranslationMissing implements L10nMissingTranslationHandler {
public handle(key: string, value?: string, params?: any): string {
// Log missing translations and return the key by default to make it easier to spot during development
- if (this.locale.language.startsWith('en')) {
+ if (this.locale.language.startsWith(LANG_DEFAULT)) {
// Skip missing-translation logs for English since it's the default language
return key
}
diff --git a/frontend/src/i18n/lib/bs.i18n.ts b/frontend/src/i18n/lib/bs.i18n.ts
new file mode 100644
index 00000000..f3ff7a18
--- /dev/null
+++ b/frontend/src/i18n/lib/bs.i18n.ts
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2012-2025 Johan Legrand <[email protected]>
+ * This file is part of Sync-in | The open source file sync and share solution
+ * See the LICENSE file for licensing details
+ */
+
+import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
+import {
+ defineLocale,
+ deLocale,
+ enGbLocale,
+ esLocale,
+ frLocale,
+ hiLocale,
+ itLocale,
+ jaLocale,
+ koLocale,
+ LocaleData,
+ plLocale,
+ ptBrLocale,
+ ruLocale,
+ trLocale,
+ zhCnLocale
+} from 'ngx-bootstrap/chronos'
+
+const BOOTSTRAP_LOCALES: Record<i18nLocaleSupported, LocaleData> = {
+ de: deLocale,
+ en: enGbLocale,
+ es: esLocale,
+ fr: frLocale,
+ hi: hiLocale,
+ it: itLocale,
+ ja: jaLocale,
+ ko: koLocale,
+ pl: plLocale,
+ pt: ptBrLocale,
+ 'pt-BR': ptBrLocale,
+ ru: ruLocale,
+ tr: trLocale,
+ zh: zhCnLocale
+}
+
+export function loadBootstrapLocale(language: string): void {
+ const locale = BOOTSTRAP_LOCALES[language]
+ if (!locale) return
+ defineLocale(language, locale)
+}
diff --git a/frontend/src/i18n/lib/dayjs.i18n.ts b/frontend/src/i18n/lib/dayjs.i18n.ts
new file mode 100644
index 00000000..87f78566
--- /dev/null
+++ b/frontend/src/i18n/lib/dayjs.i18n.ts
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012-2025 Johan Legrand <[email protected]>
+ * This file is part of Sync-in | The open source file sync and share solution
+ * See the LICENSE file for licensing details
+ */
+
+import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
+import { dJs } from '../../app/common/utils/time'
+
+const DAYJS_LOADER: Record<i18nLocaleSupported, () => Promise<unknown>> = {
+ de: () => import('dayjs/esm/locale/de'),
+ en: () => import('dayjs/esm/locale/en'),
+ es: () => import('dayjs/esm/locale/es'),
+ fr: () => import('dayjs/esm/locale/fr'),
+ hi: () => import('dayjs/esm/locale/hi'),
+ it: () => import('dayjs/esm/locale/it'),
+ ja: () => import('dayjs/esm/locale/ja'),
+ ko: () => import('dayjs/esm/locale/ko'),
+ pl: () => import('dayjs/esm/locale/pl'),
+ pt: () => import('dayjs/esm/locale/pt'),
+ 'pt-BR': () => import('dayjs/esm/locale/pt-br'),
+ ru: () => import('dayjs/esm/locale/ru'),
+ tr: () => import('dayjs/esm/locale/tr'),
+ zh: () => import('dayjs/esm/locale/zh-cn')
+}
+
+export async function loadDayjsLocale(language: string) {
+ const loader = DAYJS_LOADER[language]
+ if (!loader) return
+ await loader()
+ dJs.locale(language)
+}