Skip to content

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

feat(i18n): enhance locale support by adding new languages, centralizing i18n configuration, improving storage handling, and refining locale-specific settings

johavenccd538ce21fe
diff --git a/backend/src/applications/notifications/i18n/index.ts b/backend/src/applications/notifications/i18n/index.ts
index 5433bc40..feb4789f 100644
--- a/backend/src/applications/notifications/i18n/index.ts
+++ b/backend/src/applications/notifications/i18n/index.ts
@@ -9,11 +9,16 @@ import { es } from './es'
import { fr } from './fr'
import { hi } from './hi'
import { it } from './it'
+import { ja } from './ja'
+import { ko } from './ko'
+import { pl } from './pl'
import { pt } from './pt'
-import { zh } from './zh'
+import { pt_BR } from './pt_br'
import { ru } from './ru'
+import { tr } from './tr'
+import { zh } from './zh'
-export const translations = { fr, de, es, hi, it, pt, zh, ru }
+export const translations = { 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/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts
index fd4555df..9c3db6a6 100644
--- a/frontend/src/app/app.component.ts
+++ b/frontend/src/app/app.component.ts
@@ -4,42 +4,12 @@
* See the LICENSE file for licensing details
*/
-import { Component, inject } from '@angular/core'
+import { Component } from '@angular/core'
import { RouterOutlet } from '@angular/router'
-import { FaConfig } from '@fortawesome/angular-fontawesome'
-import { L10N_LOCALE, L10nLoader, L10nLocale } from 'angular-l10n'
-import { defineLocale, deLocale, enGbLocale, esLocale, frLocale, hiLocale, itLocale, ptBrLocale, zhCnLocale, ruLocale } from 'ngx-bootstrap/chronos'
-import { BsLocaleService } from 'ngx-bootstrap/datepicker'
-import { setTheme } from 'ngx-bootstrap/utils'
-import { dJs } from './common/utils/time'
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: '<router-outlet></router-outlet>'
})
-export class AppComponent {
- private locale = inject<L10nLocale>(L10N_LOCALE)
- private l10nLoader = inject(L10nLoader)
- private bsLocaleService = inject(BsLocaleService)
-
- constructor() {
- const faConfig = inject(FaConfig)
-
- faConfig.fixedWidth = true
- setTheme('bs5')
- defineLocale('en', enGbLocale)
- defineLocale('fr', frLocale)
- defineLocale('de', deLocale)
- defineLocale('es', esLocale)
- defineLocale('pt', ptBrLocale)
- defineLocale('it', itLocale)
- defineLocale('zh', zhCnLocale)
- defineLocale('hi', hiLocale)
- defineLocale('ru', ruLocale)
- this.l10nLoader.init().then(() => {
- dJs.locale(this.locale.language)
- this.bsLocaleService.use(this.locale.language)
- })
- }
-}
+export class AppComponent {}
diff --git a/frontend/src/app/layout/layout.service.ts b/frontend/src/app/layout/layout.service.ts
index f83c3386..a43d5f75 100644
--- a/frontend/src/app/layout/layout.service.ts
+++ b/frontend/src/app/layout/layout.service.ts
@@ -7,19 +7,19 @@
import { HttpErrorResponse } from '@angular/common/http'
import { inject, Injectable, NgZone } from '@angular/core'
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 { BsLocaleService } from 'ngx-bootstrap/datepicker'
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 } from '../../i18n/l10n'
+import { i18nLanguageText, LANG_FORMAT } from '../../i18n/l10n'
import { APP_NAME } from '../app.constants'
import { USER_LANGUAGE_AUTO } from '../applications/users/user.constants'
import { getTheme } from '../common/utils/functions'
-import { dJs } from '../common/utils/time'
import { EVENT } from '../electron/constants/events'
import { Electron } from '../electron/electron.service'
import { BreadCrumbUrl } from './breadcrumb/breadcrumb.interfaces'
@@ -56,7 +56,7 @@ export class LayoutService {
private readonly title = inject(Title)
private readonly ngZone = inject(NgZone)
private readonly translation = inject(L10nTranslationService)
- private readonly bsLocale = inject(BsLocaleService)
+ private readonly faConfig = inject(FaConfig)
private readonly bsModal = inject(BsModalService)
private readonly toastr = inject(ToastrService)
private readonly contextMenu = inject<ContextMenuService<any>>(ContextMenuService)
@@ -77,6 +77,8 @@ export class LayoutService {
private readonly dialogConfig: ModalOptions = { animated: true, keyboard: true, backdrop: true, ignoreBackdropClick: true }
constructor() {
+ setTheme('bs5')
+ this.faConfig.fixedWidth = true
this.title.setTitle(APP_NAME)
this.preferTheme.subscribe((theme) => this.setTheme(theme))
}
@@ -220,15 +222,11 @@ export class LayoutService {
}
setLanguage(language: string) {
- if (!language) {
- language = getBrowserLanguage('language') || ''
- language = language.split('-')[0]
+ if (!language || language === USER_LANGUAGE_AUTO) {
+ language = getBrowserLanguage(LANG_FORMAT) || ''
}
- if (language && language !== this.translation.getLocale().language) {
- this.translation.setLocale({ language }).then(() => {
- dJs.locale(language)
- this.bsLocale.use(language)
- })
+ if (language && language !== this.getCurrentLanguage()) {
+ this.translation.setLocale({ language }).catch(console.error)
}
}
diff --git a/frontend/src/i18n/l10n.ts b/frontend/src/i18n/l10n.ts
index ce2f36bc..3c596d79 100755
--- a/frontend/src/i18n/l10n.ts
+++ b/frontend/src/i18n/l10n.ts
@@ -4,35 +4,92 @@
* See the LICENSE file for licensing details
*/
-import { Injectable } from '@angular/core'
-import { L10nConfig, L10nLocale, L10nMissingTranslationHandler, L10nStorage, L10nTranslationLoader } from 'angular-l10n'
+import { inject, Injectable } from '@angular/core'
+import {
+ getBrowserLanguage,
+ L10N_LOCALE,
+ L10nConfig,
+ L10nFormat,
+ L10nLocale,
+ L10nMissingTranslationHandler,
+ 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/locale/fr'
-import 'dayjs/locale/de.js'
-import 'dayjs/locale/es.js'
-import 'dayjs/locale/pt.js'
-import 'dayjs/locale/it.js'
-import 'dayjs/locale/zh.js'
-import 'dayjs/locale/hi.js'
-import 'dayjs/locale/ru.js'
+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)
+
+export const LANG_FORMAT: L10nFormat = 'language-region' as const
+export const STORAGE_SESSION_KEY = 'locale' as const
export const i18nLanguageText = {
[USER_LANGUAGE_AUTO]: 'Auto',
- en: 'English',
- fr: 'Français',
de: 'Deutsch',
+ en: 'English',
es: 'Español',
- pt: 'Português',
- it: 'Italiano',
- zh: '中文 (简体)',
+ fr: 'Français',
hi: 'हिन्दी',
- ru: 'Русский'
+ it: 'Italiano',
+ ja: '日本語',
+ ko: '한국어',
+ pl: 'Polski',
+ pt: 'Português',
+ 'pt-BR': 'Português (Brasil)',
+ ru: 'Русский',
+ tr: 'Türkçe',
+ zh: '中文(简体) '
}
export const l10nConfig: L10nConfig = {
- format: 'language',
+ format: LANG_FORMAT,
// Provider without static asset: resources will be loaded through TranslationLoader
providers: [{ name: 'app', asset: 'app' }],
fallback: false,
@@ -40,44 +97,79 @@ export const l10nConfig: L10nConfig = {
keySeparator: '|',
defaultLocale: { language: 'en' },
schema: [
- { locale: { language: 'en' }, dir: 'ltr' },
- { locale: { language: 'fr' }, dir: 'ltr' },
{ locale: { language: 'de' }, dir: 'ltr' },
+ { locale: { language: 'en' }, dir: 'ltr' },
{ locale: { language: 'es' }, dir: 'ltr' },
+ { locale: { language: 'fr' }, dir: 'ltr' },
+ { locale: { language: 'hi' }, dir: 'ltr' },
{ locale: { language: 'it' }, dir: 'ltr' },
+ { locale: { language: 'ja' }, dir: 'ltr' },
+ { locale: { language: 'ko' }, dir: 'ltr' },
+ { locale: { language: 'pl' }, dir: 'ltr' },
{ locale: { language: 'pt' }, dir: 'ltr' },
- { locale: { language: 'hi' }, dir: 'ltr' },
- { locale: { language: 'zh' }, dir: 'ltr' },
- { locale: { language: 'ru' }, dir: 'ltr' }
+ { locale: { language: 'pt-BR' }, dir: 'ltr' },
+ { locale: { language: 'ru' }, dir: 'ltr' },
+ { locale: { language: 'tr' }, dir: 'ltr' },
+ { locale: { language: 'zh' }, dir: 'ltr' }
]
}
+export function getL10nLocale(): L10nLocale {
+ return { language: getBrowserLanguage(LANG_FORMAT) }
+}
+
@Injectable({ providedIn: 'root' })
export class TranslationStorage implements L10nStorage {
- hasStorage: boolean
-
- constructor() {
- this.hasStorage = typeof Storage !== 'undefined'
- }
+ private readonly hasStorage = typeof Storage !== 'undefined'
public async read(): Promise<L10nLocale | null> {
- if (this.hasStorage) {
- const locale = sessionStorage.getItem('locale')
- return Promise.resolve(locale ? JSON.parse(locale) : locale)
+ if (!this.hasStorage) {
+ return getL10nLocale()
+ }
+ let stored: L10nLocale | null = null
+ const raw = sessionStorage.getItem(STORAGE_SESSION_KEY)
+ if (raw) {
+ try {
+ stored = JSON.parse(raw)
+ } catch (e) {
+ console.warn('Invalid locale in sessionStorage, resetting.', e)
+ sessionStorage.removeItem(STORAGE_SESSION_KEY)
+ }
}
- return Promise.resolve(null)
+ const lang = stored?.language
+ const isSupported = !!lang && Object.hasOwn(i18nLanguageText, lang)
+ if (!isSupported) {
+ sessionStorage.removeItem(STORAGE_SESSION_KEY)
+ return getL10nLocale()
+ }
+ return stored
}
public async write(locale: L10nLocale): Promise<void> {
- if (this.hasStorage) {
- sessionStorage.setItem('locale', JSON.stringify(locale))
+ if (!this.hasStorage) return
+ try {
+ const value = JSON.stringify(locale)
+ sessionStorage.setItem(STORAGE_SESSION_KEY, value)
+ } catch (e) {
+ console.warn('Failed to write locale to sessionStorage:', e)
}
}
}
@Injectable()
export class TranslationLoader implements L10nTranslationLoader {
- public get(language: string): Observable<Record<string, any>> {
+ 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)) {
+ dJs.locale(language)
+ this.bsLocale.use(language)
+ } else {
+ return of({})
+ }
// Dynamically load the JSON file for the requested language
return from(import(`./${language}.json`)).pipe(
map((module: any) => module?.default ?? module ?? {}),
@@ -88,10 +180,15 @@ export class TranslationLoader implements L10nTranslationLoader {
@Injectable()
export class TranslationMissing implements L10nMissingTranslationHandler {
- public handle(key: string, value?: string, params?: any): string | any {
- // Log missing translations and return the key by default
- // to make it easier to spot during development
- console.error('translation missing: ', key, value, params)
+ protected locale = inject<L10nLocale>(L10N_LOCALE)
+
+ 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')) {
+ // Skip missing-translation logs for English since it's the default language
+ return key
+ }
+ console.error('translation missing: ', key, value, params, this.locale)
return key ?? 'no translation'
}
}