Skip to content

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

feat(i18n): centralize i18n utilities, refine type safety, and enhance language normalization and storage handling

johaven11650a4b9cf1
diff --git a/backend/src/applications/notifications/i18n/index.ts b/backend/src/applications/notifications/i18n/index.ts
index e143d05a..7b6e728b 100644
--- a/backend/src/applications/notifications/i18n/index.ts
+++ b/backend/src/applications/notifications/i18n/index.ts
@@ -4,7 +4,7 @@
* See the LICENSE file for licensing details
*/
-import type { i18nLocaleSupported } from '../../../common/interfaces'
+import { i18nLocale } from '../../../common/i18n'
import { de } from './de'
import { es } from './es'
import { fr } from './fr'
@@ -19,14 +19,30 @@ import { ru } from './ru'
import { tr } from './tr'
import { zh } from './zh'
-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 const translations = new Map<i18nLocale, Record<string, string>>([
+ ['de', de],
+ ['es', es],
+ ['fr', fr],
+ ['hi', hi],
+ ['it', it],
+ ['ja', ja],
+ ['ko', ko],
+ ['pl', pl],
+ ['pt', pt],
+ ['pt-BR', pt_BR],
+ ['ru', ru],
+ ['tr', tr],
+ ['zh', zh]
+])
-export function translateObject<T>(language: string, obj: T): T {
- if (language?.length && Object.keys(translations).indexOf(language) > -1) {
- const tr = translations[language]
- for (const k of Object.keys(obj).filter((k) => !!obj[k] && tr[obj[k]])) {
- obj[k] = tr[obj[k]]
- }
+export function translateObject(language: i18nLocale, obj: Record<string, string>): Record<string, string> {
+ if (!language || !translations.has(language)) return obj
+ const tr: Record<string, string> = translations.get(language)
+ if (!tr) return obj
+ for (const key in obj) {
+ const v = obj[key]
+ const t = tr[v]
+ if (t) obj[key] = t
}
return obj
}
diff --git a/backend/src/applications/notifications/mails/models.ts b/backend/src/applications/notifications/mails/models.ts
index d2861e24..53d87623 100644
--- a/backend/src/applications/notifications/mails/models.ts
+++ b/backend/src/applications/notifications/mails/models.ts
@@ -5,6 +5,7 @@
*/
import { ACTION } from '../../../common/constants'
+import { i18nLocale } from '../../../common/i18n'
import { capitalizeString, SERVER_NAME } from '../../../common/shared'
import { fileName } from '../../files/utils/files'
import { UserModel } from '../../users/models/user.model'
@@ -14,7 +15,7 @@ import { defaultFooter, mailAuthor, mailEventOnElement, mailItalicContent, mailT
import { urlFromLink, urlFromSpace, urlFromSpaceFile, urlFromSync } from './urls'
export function commentMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
content: string
@@ -38,7 +39,7 @@ export function commentMail(
}
export function spaceMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
currentUrl: string
@@ -62,7 +63,7 @@ export function spaceMail(
}
export function spaceRootMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
currentUrl: string
@@ -90,7 +91,7 @@ export function spaceRootMail(
}
export function shareMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
currentUrl: string
@@ -113,7 +114,7 @@ export function shareMail(
}
export function linkMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
currentUrl: string
@@ -142,7 +143,7 @@ export function linkMail(
}
export function syncMail(
- language: string,
+ language: i18nLocale,
notification: NotificationContent,
options: {
currentUrl: string
@@ -165,7 +166,7 @@ export function syncMail(
return [`${tr.title}: ${capitalizeString(notification.element)}`, mailTemplate(content, footer)]
}
-export function auth2FaMail(language: string, notification: NotificationContent): [string, string] {
+export function auth2FaMail(language: i18nLocale, notification: NotificationContent): [string, string] {
const tr = translateObject(language, {
title: 'Security notification',
footer:
@@ -182,7 +183,7 @@ export function auth2FaMail(language: string, notification: NotificationContent)
return [tr.title, mailTemplate(content, footer)]
}
-export function authLocked(language: string, notification: NotificationContent): [string, string] {
+export function authLocked(language: i18nLocale, notification: NotificationContent): [string, string] {
const tr = translateObject(language, {
title: 'Security notification',
footer:
diff --git a/backend/src/applications/notifications/services/notifications-manager.service.ts b/backend/src/applications/notifications/services/notifications-manager.service.ts
index cf4e5827..5cf2d7f0 100644
--- a/backend/src/applications/notifications/services/notifications-manager.service.ts
+++ b/backend/src/applications/notifications/services/notifications-manager.service.ts
@@ -5,6 +5,7 @@
*/
import { Injectable, Logger } from '@nestjs/common'
+import { i18nLocale } from '../../../common/i18n'
import { MailProps } from '../../../infrastructure/mailer/interfaces/mail.interface'
import { Mailer } from '../../../infrastructure/mailer/mailer.service'
import { USER_NOTIFICATION } from '../../users/constants/user'
@@ -72,7 +73,7 @@ export class NotificationsManager {
.sendMails(
await Promise.all(
toUsers.map(async (m) => {
- const [title, html] = this.genMail(m.language, content, options)
+ const [title, html] = this.genMail(m.language as i18nLocale, content, options)
return {
to: m.email,
subject: title,
@@ -93,7 +94,7 @@ export class NotificationsManager {
}
}
- private genMail(language: string, content: NotificationContent, options?: NotificationOptions): [string, string] {
+ private genMail(language: i18nLocale, content: NotificationContent, options?: NotificationOptions): [string, string] {
switch (content.app) {
case NOTIFICATION_APP.COMMENTS:
return commentMail(language, content, { content: options.content, currentUrl: options.currentUrl, author: options.author })
diff --git a/backend/src/common/i18n.ts b/backend/src/common/i18n.ts
new file mode 100644
index 00000000..3d0144cd
--- /dev/null
+++ b/backend/src/common/i18n.ts
@@ -0,0 +1,19 @@
+/*
+ * 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
+ */
+
+export const LANG_DEFAULT = 'en' as const
+export type i18nLocaleSupported = 'de' | 'en' | 'es' | 'fr' | 'hi' | 'it' | 'ja' | 'ko' | 'pl' | 'pt' | 'pt-BR' | 'ru' | 'tr' | 'zh'
+export const LANG_SUPPORTED = new Set<i18nLocaleSupported>(['de', 'en', 'es', 'fr', 'hi', 'it', 'ja', 'ko', 'pl', 'pt', 'pt-BR', 'ru', 'tr', 'zh'])
+export type i18nLocale = Exclude<i18nLocaleSupported, typeof LANG_DEFAULT>
+
+export function normalizeLanguage(language: string): i18nLocaleSupported | null {
+ if (!language) return null
+ if (LANG_SUPPORTED.has(language as i18nLocaleSupported)) {
+ return language as i18nLocaleSupported
+ }
+ const code = language.split('-')[0]
+ return LANG_SUPPORTED.has(code as i18nLocaleSupported) ? (code as i18nLocaleSupported) : null
+}
diff --git a/backend/src/common/interfaces.ts b/backend/src/common/interfaces.ts
index 87bf8bb4..4cd45594 100644
--- a/backend/src/common/interfaces.ts
+++ b/backend/src/common/interfaces.ts
@@ -10,5 +10,3 @@ 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/src/app/layout/layout.service.ts b/frontend/src/app/layout/layout.service.ts
index 4b6e5e82..b0758cc2 100644
--- a/frontend/src/app/layout/layout.service.ts
+++ b/frontend/src/app/layout/layout.service.ts
@@ -10,13 +10,13 @@ import { Title } from '@angular/platform-browser'
import { FaConfig } from '@fortawesome/angular-fontawesome'
import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
import { ContextMenuComponent, ContextMenuService } from '@perfectmemory/ngx-contextmenu'
-import { getBrowserLanguage, L10nTranslationService } from 'angular-l10n'
+import { L10nTranslationService } from 'angular-l10n'
import { BsModalRef, BsModalService, ModalOptions } from 'ngx-bootstrap/modal'
import { setTheme } from 'ngx-bootstrap/utils'
import { ActiveToast, ToastrService } from 'ngx-toastr'
import { BehaviorSubject, fromEvent, mergeWith, Observable, Subject } from 'rxjs'
import { map } from 'rxjs/operators'
-import { i18nLanguageText, LANG_FORMAT } from '../../i18n/l10n'
+import { getBrowserL10nLocale, i18nLanguageText } from '../../i18n/l10n'
import { APP_NAME } from '../app.constants'
import { USER_LANGUAGE_AUTO } from '../applications/users/user.constants'
import { getTheme } from '../common/utils/functions'
@@ -223,7 +223,7 @@ export class LayoutService {
setLanguage(language: string): Promise<void> {
if (!language || language === USER_LANGUAGE_AUTO) {
- language = getBrowserLanguage(LANG_FORMAT) || ''
+ language = getBrowserL10nLocale().language
}
if (language && language !== this.getCurrentLanguage()) {
return this.translation.setLocale({ language })
@@ -231,7 +231,7 @@ export class LayoutService {
return Promise.resolve()
}
- getCurrentLanguage() {
+ getCurrentLanguage(): string {
return this.translation.getLocale().language
}
diff --git a/frontend/src/i18n/l10n.ts b/frontend/src/i18n/l10n.ts
index 8316e149..40ccda32 100755
--- a/frontend/src/i18n/l10n.ts
+++ b/frontend/src/i18n/l10n.ts
@@ -5,7 +5,7 @@
*/
import { inject, Injectable } from '@angular/core'
-import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
+import { i18nLocaleSupported, LANG_SUPPORTED, normalizeLanguage } from '@sync-in-server/backend/src/common/i18n'
import {
getBrowserLanguage,
L10N_LOCALE,
@@ -73,17 +73,17 @@ export const l10nConfig: L10nConfig & {
]
}
-export function getL10nLocale(): L10nLocale {
- return { language: getBrowserLanguage(LANG_FORMAT) }
+export function getBrowserL10nLocale(): L10nLocale {
+ return { language: normalizeLanguage(getBrowserLanguage(LANG_FORMAT)) }
}
@Injectable({ providedIn: 'root' })
export class TranslationStorage implements L10nStorage {
private readonly hasStorage = typeof Storage !== 'undefined'
- public async read(): Promise<L10nLocale | null> {
+ async read(): Promise<L10nLocale | null> {
if (!this.hasStorage) {
- return getL10nLocale()
+ return getBrowserL10nLocale()
}
let stored: L10nLocale | null = null
const raw = sessionStorage.getItem(STORAGE_SESSION_KEY)
@@ -96,15 +96,15 @@ export class TranslationStorage implements L10nStorage {
}
}
const lang = stored?.language
- const isSupported = !!lang && Object.hasOwn(i18nLanguageText, lang)
+ const isSupported = !!lang && LANG_SUPPORTED.has(lang as i18nLocaleSupported)
if (!isSupported) {
sessionStorage.removeItem(STORAGE_SESSION_KEY)
- return getL10nLocale()
+ return getBrowserL10nLocale()
}
return stored
}
- public async write(locale: L10nLocale): Promise<void> {
+ async write(locale: L10nLocale): Promise<void> {
if (!this.hasStorage) return
try {
const value = JSON.stringify(locale)
@@ -120,13 +120,10 @@ export class TranslationLoader implements L10nTranslationLoader {
private readonly bsLocale = inject(BsLocaleService)
get(language: string): Observable<Record<string, any>> {
- if (!Object.hasOwn(i18nLanguageText, language)) {
- language = language.split('-')[0]
- }
- if (Object.hasOwn(i18nLanguageText, language)) {
+ if (language && LANG_SUPPORTED.has(language as i18nLocaleSupported)) {
loadDayjsLocale(language).catch(console.error)
loadBootstrapLocale(language)
- this.bsLocale.use(language)
+ this.bsLocale.use(language.toLowerCase())
} else {
return of({})
}
diff --git a/frontend/src/i18n/lib/bs.i18n.ts b/frontend/src/i18n/lib/bs.i18n.ts
index f3ff7a18..f5699658 100644
--- a/frontend/src/i18n/lib/bs.i18n.ts
+++ b/frontend/src/i18n/lib/bs.i18n.ts
@@ -4,7 +4,7 @@
* See the LICENSE file for licensing details
*/
-import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
+import { i18nLocaleSupported } from '@sync-in-server/backend/src/common/i18n'
import {
defineLocale,
deLocale,
@@ -41,7 +41,7 @@ const BOOTSTRAP_LOCALES: Record<i18nLocaleSupported, LocaleData> = {
}
export function loadBootstrapLocale(language: string): void {
- const locale = BOOTSTRAP_LOCALES[language]
+ const locale: LocaleData = BOOTSTRAP_LOCALES[language]
if (!locale) return
- defineLocale(language, locale)
+ defineLocale(language.toLowerCase(), locale)
}
diff --git a/frontend/src/i18n/lib/dayjs.i18n.ts b/frontend/src/i18n/lib/dayjs.i18n.ts
index 87f78566..e0e39c33 100644
--- a/frontend/src/i18n/lib/dayjs.i18n.ts
+++ b/frontend/src/i18n/lib/dayjs.i18n.ts
@@ -4,10 +4,10 @@
* See the LICENSE file for licensing details
*/
-import type { i18nLocaleSupported } from '@sync-in-server/backend/src/common/interfaces'
+import { i18nLocaleSupported } from '@sync-in-server/backend/src/common/i18n'
import { dJs } from '../../app/common/utils/time'
-const DAYJS_LOADER: Record<i18nLocaleSupported, () => Promise<unknown>> = {
+const DAYJS_LOADER: Record<i18nLocaleSupported, () => Promise<ILocale>> = {
de: () => import('dayjs/esm/locale/de'),
en: () => import('dayjs/esm/locale/en'),
es: () => import('dayjs/esm/locale/es'),
@@ -21,7 +21,7 @@ const DAYJS_LOADER: Record<i18nLocaleSupported, () => Promise<unknown>> = {
'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')
+ zh: () => import('dayjs/esm/locale/zh')
}
export async function loadDayjsLocale(language: string) {