From 0c7e81ebb86ced335fa5e34a6256bc6a29b0daad Mon Sep 17 00:00:00 2001 From: Bertrand Zuchuat Date: Mon, 16 Sep 2024 10:32:09 +0200 Subject: [PATCH] chore(translate): improvement * Adds primeng locale package. * Refactors the translate service. * Initializes translate service on Core module. Co-Authored-by: Bertrand Zuchuat --- package-lock.json | 10 ++ package.json | 1 + .../ng-core-tester/src/app/app.component.ts | 12 +- projects/ng-core-tester/src/app/app.module.ts | 18 +-- projects/rero/ng-core/src/lib/core.module.ts | 22 +++- .../formly/primeng/select/select.spec.ts | 1 - .../lib/translate/date-translate-pipe.spec.ts | 62 +++++----- .../src/lib/translate/date-translate-pipe.ts | 18 +-- .../ng-core/src/lib/translate/primeng/de.json | 55 --------- .../ng-core/src/lib/translate/primeng/en.json | 61 ---------- .../ng-core/src/lib/translate/primeng/fr.json | 55 --------- .../ng-core/src/lib/translate/primeng/it.json | 61 ---------- .../lib/translate/translate-service.spec.ts | 74 ------------ .../src/lib/translate/translate-service.ts | 114 ++++++------------ 14 files changed, 111 insertions(+), 453 deletions(-) delete mode 100644 projects/rero/ng-core/src/lib/translate/primeng/de.json delete mode 100644 projects/rero/ng-core/src/lib/translate/primeng/en.json delete mode 100644 projects/rero/ng-core/src/lib/translate/primeng/fr.json delete mode 100644 projects/rero/ng-core/src/lib/translate/primeng/it.json delete mode 100644 projects/rero/ng-core/src/lib/translate/translate-service.spec.ts diff --git a/package-lock.json b/package-lock.json index c0538988..1d9d1792 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,7 @@ "ngx-toastr": "^18.0.0", "primeflex": "^3.3.1", "primeicons": "^7.0.0", + "primelocale": "^1.0.3", "primeng": "^17.18.0", "rxjs": "~7.8.1", "tslib": "^2.6.2", @@ -12853,6 +12854,15 @@ "resolved": "https://registry.npmjs.org/primeicons/-/primeicons-7.0.0.tgz", "integrity": "sha512-jK3Et9UzwzTsd6tzl2RmwrVY/b8raJ3QZLzoDACj+oTJ0oX7L9Hy+XnVwgo4QVKlKpnP/Ur13SXV/pVh4LzaDw==" }, + "node_modules/primelocale": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/primelocale/-/primelocale-1.0.3.tgz", + "integrity": "sha512-a0VuhQLgUge4IJG2RKpMJSDYHuYIAd2kcqQy/quyx8UyM1/m7Uvt9hFKoFL0QAbZZyHIVHobVmGWAOJN8d7COQ==", + "engines": { + "node": ">=12.0.0", + "npm": ">=6.0.0" + } + }, "node_modules/primeng": { "version": "17.18.9", "resolved": "https://registry.npmjs.org/primeng/-/primeng-17.18.9.tgz", diff --git a/package.json b/package.json index 53659e1f..90a2215a 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "ngx-toastr": "^18.0.0", "primeflex": "^3.3.1", "primeicons": "^7.0.0", + "primelocale": "^1.0.3", "primeng": "^17.18.0", "rxjs": "~7.8.1", "tslib": "^2.6.2", diff --git a/projects/ng-core-tester/src/app/app.component.ts b/projects/ng-core-tester/src/app/app.component.ts index 057a7d42..fc82771a 100644 --- a/projects/ng-core-tester/src/app/app.component.ts +++ b/projects/ng-core-tester/src/app/app.component.ts @@ -1,6 +1,6 @@ /* * RERO angular core - * Copyright (C) 2020 RERO + * Copyright (C) 2020-2024 RERO * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -15,7 +15,7 @@ * along with this program. If not, see . */ import { Component, OnInit, inject } from '@angular/core'; -import { CoreConfigService, RecordEvent, RecordService, TitleMetaService, TranslateService } from '@rero/ng-core'; +import { CoreConfigService, RecordEvent, RecordService, TitleMetaService } from '@rero/ng-core'; import { MenuItem, MessageService } from 'primeng/api'; /** @@ -28,15 +28,12 @@ import { MenuItem, MessageService } from 'primeng/api'; export class AppComponent implements OnInit { /** Service injection */ - translateService = inject(TranslateService); + // translateService = inject(TranslateService); configService = inject(CoreConfigService); titleMetaService = inject(TitleMetaService); recordService = inject(RecordService); messageService = inject(MessageService); - // Current lang of the application - lang: string = document.documentElement.lang; - // Available languages languages: string[]; @@ -48,16 +45,15 @@ export class AppComponent implements OnInit { // Application language menu languageMenu: MenuItem; + /** * Component initialization. * * - Initializes listener to record changes. - * - Initializes languages and current language. * - Sets title metadata. */ ngOnInit() { this.initializeEvents(); - this.translateService.setLanguage(this.lang); // Set default title window when application start const prefix = this.configService.prefixWindow; if (prefix) { diff --git a/projects/ng-core-tester/src/app/app.module.ts b/projects/ng-core-tester/src/app/app.module.ts index 0f117d10..683a4939 100644 --- a/projects/ng-core-tester/src/app/app.module.ts +++ b/projects/ng-core-tester/src/app/app.module.ts @@ -21,9 +21,7 @@ import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { TranslateLoader as BaseTranslateLoader, TranslateModule } from '@ngx-translate/core'; import { CoreConfigService, RecordModule, RecordService, RemoteAutocompleteService, TranslateLoader } from '@rero/ng-core'; -import { defineLocale } from 'ngx-bootstrap/chronos'; import { CollapseModule } from 'ngx-bootstrap/collapse'; -import { deLocale, enGbLocale, frLocale, itLocale } from 'ngx-bootstrap/locale'; import { TypeaheadModule } from 'ngx-bootstrap/typeahead'; import { MenubarModule } from 'primeng/menubar'; import { AppConfigService } from './app-config.service'; @@ -32,12 +30,12 @@ import { AppComponent } from './app.component'; import { AppDialogComponent } from './home/dialog/app-dialog.component'; import { HomeComponent } from './home/home.component'; import { ToastComponent } from './home/toast/toast.component'; +import { MenuComponent } from './menu/menu.component'; import { DetailComponent } from './record/document/detail/detail.component'; import { DocumentComponent } from './record/document/document.component'; import { EditorComponent } from './record/editor/editor.component'; import { RecordServiceMock } from './record/editor/record-service-mock'; import { SearchBarComponent } from './search-bar/search-bar.component'; -import { MenuComponent } from './menu/menu.component'; import { AppRemoteAutocompleteService } from './service/app-remote-autocomplete.service'; @NgModule({ @@ -86,16 +84,4 @@ import { AppRemoteAutocompleteService } from './service/app-remote-autocomplete. ], bootstrap: [AppComponent] }) -export class AppModule { - availableLocales = { - de: deLocale, - en: enGbLocale, - fr: frLocale, - it: itLocale - }; - constructor() { - Object.keys(this.availableLocales).forEach(locale => { - defineLocale(locale, this.availableLocales[locale]); - }); - } -} +export class AppModule {} diff --git a/projects/rero/ng-core/src/lib/core.module.ts b/projects/rero/ng-core/src/lib/core.module.ts index 81d1ed10..3e848395 100644 --- a/projects/rero/ng-core/src/lib/core.module.ts +++ b/projects/rero/ng-core/src/lib/core.module.ts @@ -16,9 +16,9 @@ */ import { CommonModule } from '@angular/common'; import { HttpClient } from '@angular/common/http'; -import { NgModule } from '@angular/core'; +import { APP_INITIALIZER, NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; -import { TranslateLoader as BaseTranslateLoader, TranslateModule } from '@ngx-translate/core'; +import { TranslateLoader as BaseTranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core'; import { BsDropdownModule } from 'ngx-bootstrap/dropdown'; import { ModalModule } from 'ngx-bootstrap/modal'; import { NgxSpinnerModule } from 'ngx-spinner'; @@ -47,6 +47,15 @@ import { TranslateLanguagePipe } from './translate/translate-language.pipe'; import { TranslateLoader } from './translate/translate-loader'; import { MenuComponent } from './widget/menu/menu.component'; import { SortListComponent } from './widget/sort-list/sort-list.component'; +import { NgCoreTranslateService } from './translate/translate-service'; +import { Observable, of } from 'rxjs'; + +function initializeAppFactory(translateService: NgCoreTranslateService): () => Observable { + return () => { + translateService.initialize(); + return of(true); + }; +} @NgModule({ declarations: [ @@ -115,7 +124,14 @@ import { SortListComponent } from './widget/sort-list/sort-list.component'; providers: [ ComponentCanDeactivateGuard, ConfirmationService, - MessageService + MessageService, + { provide: TranslateService, useValue: NgCoreTranslateService }, + { + provide: APP_INITIALIZER, + useFactory: initializeAppFactory, + deps: [NgCoreTranslateService], + multi: true + } ] }) export class CoreModule { } diff --git a/projects/rero/ng-core/src/lib/record/editor/formly/primeng/select/select.spec.ts b/projects/rero/ng-core/src/lib/record/editor/formly/primeng/select/select.spec.ts index 6f0caf44..48729647 100644 --- a/projects/rero/ng-core/src/lib/record/editor/formly/primeng/select/select.spec.ts +++ b/projects/rero/ng-core/src/lib/record/editor/formly/primeng/select/select.spec.ts @@ -43,7 +43,6 @@ describe('SelectComponent', () => { class: 'w-full', editable: false, filter: false, - filterBy: 'label', filterMatchMode: 'contains', group: false, options: [ diff --git a/projects/rero/ng-core/src/lib/translate/date-translate-pipe.spec.ts b/projects/rero/ng-core/src/lib/translate/date-translate-pipe.spec.ts index 2da49203..e7848c8f 100644 --- a/projects/rero/ng-core/src/lib/translate/date-translate-pipe.spec.ts +++ b/projects/rero/ng-core/src/lib/translate/date-translate-pipe.spec.ts @@ -1,6 +1,6 @@ /* * RERO angular core - * Copyright (C) 2020 RERO + * Copyright (C) 2020-2024 RERO * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -14,43 +14,45 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { EventEmitter } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { TranslateModule } from '@ngx-translate/core'; import { DateTranslatePipe } from './date-translate-pipe'; -import { TranslateService } from './translate-service'; -import en from '@angular/common/locales/en-GB'; -import { registerLocaleData } from '@angular/common'; +import { NgCoreTranslateService } from './translate-service'; -let dateTranslateService: DateTranslatePipe; +describe('DateTranslatePipePipe', () => { + let pipe: DateTranslatePipe; + let service: NgCoreTranslateService; -export interface LangChangeEvent { - lang: string; - translations: any; -} + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + TranslateModule.forRoot() + ], + providers: [ + DateTranslatePipe + ] + }); + service = TestBed.inject(NgCoreTranslateService); + service.initialize(); + pipe = TestBed.inject(DateTranslatePipe); + }); -class TranslateServiceMock { - get onLangChange() { - return new EventEmitter(); - } - get currentLanguage() { - return 'en'; - } -} + it('should return the english translation of the date (default)', () => { + expect(pipe.transform('2019-10-18 12:00:00')).toBe('18 Oct 2019'); + }); -describe('DateTranslatePipePipe', () => { - beforeEach(() => { - registerLocaleData(en); - dateTranslateService = new DateTranslatePipe( - new TranslateServiceMock() as unknown as TranslateService - ); + it('should return the French translation of the date', () => { + service.use('fr'); + expect(pipe.transform('2019-10-18 12:00:00')).toBe('18 oct. 2019'); }); - it('create an instance', () => { - const pipe = dateTranslateService; - expect(pipe).toBeTruthy(); + it('should return the German translation of the date', () => { + service.use('de'); + expect(pipe.transform('2019-10-18 12:00:00')).toBe('18.10.2019'); }); - it('create an instance', () => { - const pipe = dateTranslateService; - expect(pipe.transform('2019-10-18 12:00:00')).toBe('18 Oct 2019'); + it('should return the Italian translation of the date', () => { + service.use('it'); + expect(pipe.transform('2019-10-18 12:00:00')).toBe('18 ott 2019'); }); }); diff --git a/projects/rero/ng-core/src/lib/translate/date-translate-pipe.ts b/projects/rero/ng-core/src/lib/translate/date-translate-pipe.ts index 06eaca5e..5c6e47fe 100644 --- a/projects/rero/ng-core/src/lib/translate/date-translate-pipe.ts +++ b/projects/rero/ng-core/src/lib/translate/date-translate-pipe.ts @@ -15,31 +15,23 @@ * along with this program. If not, see . */ import { DatePipe } from '@angular/common'; -import { Inject, Pipe, PipeTransform } from '@angular/core'; -import { TranslateService } from './translate-service'; +import { inject, Pipe, PipeTransform } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; @Pipe({ name: 'dateTranslate', pure: false, // required to update the value when the promise is resolved }) export class DateTranslatePipe extends DatePipe implements PipeTransform { - /** - * Constructor. - * - * @param translateService Translate service. - */ - constructor( - @Inject(TranslateService) private translateService: TranslateService - ) { - super(translateService.currentLanguage); - } + + translateService = inject(TranslateService); transform(value: Date | string | number, format?: string, timezone?: string, locale?: string): string | null; transform(value: null | undefined, format?: string, timezone?: string, locale?: string): null; transform(value: Date | string | number | null | undefined, format?: string, timezone?: string, locale?: string): string | null { if (!locale) { - locale = this.translateService.currentLanguage; + locale = this.translateService.currentLang; } if (locale === 'en') { diff --git a/projects/rero/ng-core/src/lib/translate/primeng/de.json b/projects/rero/ng-core/src/lib/translate/primeng/de.json deleted file mode 100644 index 7c8f8d29..00000000 --- a/projects/rero/ng-core/src/lib/translate/primeng/de.json +++ /dev/null @@ -1,55 +0,0 @@ - -{ - "startsWith":"startet mit", - "contains":"enhält", - "notContains":"enthält nicht", - "endsWith":"endet mit", - "equals":"gleich", - "notEquals":"nicht gleich", - "noFilter":"kein Filter", - "lt":"weniger als", - "lte":"weniger oder gleich wie", - "gt":"größer als", - "gte":"größer oder gleich wie", - "dateIs":"Datum ist", - "dateIsNot":"Datum ist nicht", - "dateBefore":"Datum ist vor", - "dateAfter":"Datum ist nach", - "custom":"Benutzerdefiniert", - "apply":"Übernehmen", - "matchAll":"Passt auf alle", - "matchAny":"Passt auf einige", - "addRule":"Regel hinzufügen", - "removeRule":"Regel entfernen", - "accept": "Ja", - "reject": "Nein", - "choose": "Auswählen", - "upload": "Upload", - "cancel": "Abbruch", - "dayNames": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"], - "dayNamesShort": ["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"], - "dayNamesMin": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"], - "monthNames": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"], - "monthNamesShort": ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dez"], - "today": "Heute", - "clear": "Löschen", - "weekHeader": "Wo", - "firstDayOfWeek": 1, - "dateFormat": "dd.mm.yy", - "weak": "Schwach", - "medium": "Mittel", - "strong": "Stark", - "passwordPrompt": "Passwort eingeben", - "emptyFilterMessage":"Keine Einträge gefunden", - "emptyMessage": "Keine Einträge gefunden", - "aria": { - "trueLabel": "Richtig", - "falseLabel": "Falsch", - "nullLabel": "Nicht selektiert", - "pageLabel": "Seite", - "firstPageLabel": "Erste Seite", - "lastPageLabel": "Letzte Seite", - "nextPageLabel": "Nächste Seite", - "previousPageLabel": "Vorherige Seite" - } -} diff --git a/projects/rero/ng-core/src/lib/translate/primeng/en.json b/projects/rero/ng-core/src/lib/translate/primeng/en.json deleted file mode 100644 index 1129c9ff..00000000 --- a/projects/rero/ng-core/src/lib/translate/primeng/en.json +++ /dev/null @@ -1,61 +0,0 @@ - -{ - "startsWith":"Starts with", - "contains":"Contains", - "notContains":"Not contains", - "endsWith":"Ends with", - "equals":"Equals", - "notEquals":"Not equals", - "noFilter":"No Filter", - "filter":"Filter", - "lt":"Less than", - "lte":"Less than or equal to", - "gt":"Greater than", - "gte":"Greater than or equal to", - "dateIs":"Date is", - "dateIsNot":"Date is not", - "dateBefore":"Date is before", - "dateAfter":"Date is after", - "custom":"Custom", - "clear":"Clear", - "apply":"Apply", - "matchAll":"Match All", - "matchAny":"Match Any", - "addRule":"Add Rule", - "removeRule":"Remove Rule", - "accept":"Yes", - "reject":"No", - "choose":"Choose", - "upload":"Upload", - "cancel":"Cancel", - "close":"Close", - "dayNames":["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], - "dayNamesShort":["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], - "dayNamesMin":["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], - "monthNames":["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], - "monthNamesShort":["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], - "today":"Today", - "weekHeader":"Wk", - "firstDayOfWeek":0, - "dateFormat":"mm/dd/yy", - "weak":"Weak", - "medium":"Medium", - "strong":"Strong", - "passwordPrompt":"Enter a password", - "emptyFilterMessage":"No available options", - "emptyMessage":"No results found", - "aria": { - "trueLabel": "True", - "falseLabel": "False", - "nullLabel": "Not Selected", - "pageLabel": "Page", - "firstPageLabel": "First Page", - "lastPageLabel": "Last Page", - "nextPageLabel": "Next Page", - "previousPageLabel": "Previous Page", - "selectLabel": "Select", - "unselectLabel": "Unselect", - "expandLabel": "Expand", - "collapseLabel": "Collapse" - } -} diff --git a/projects/rero/ng-core/src/lib/translate/primeng/fr.json b/projects/rero/ng-core/src/lib/translate/primeng/fr.json deleted file mode 100644 index 75a405d6..00000000 --- a/projects/rero/ng-core/src/lib/translate/primeng/fr.json +++ /dev/null @@ -1,55 +0,0 @@ - -{ - "startsWith": "Commence par", - "contains": "Contient", - "notContains": "Ne contient pas", - "endsWith": "Se termine par", - "equals": "Egal à", - "notEquals": "Différent de", - "noFilter": "Aucun filtre", - "lt": "Inférieur à", - "lte": "Inférieur ou égal à", - "gt": "Supérieur à", - "gte": "Supérieur ou égal à", - "dateIs": "La date est", - "dateIsNot": "La date n'est pas", - "dateBefore": "Avant le", - "dateAfter": "Après le", - "custom": "Personnalisé", - "clear": "Effacer", - "apply": "Appliquer", - "matchAll": "Correspond à tous", - "matchAny": "Au moins un Correspond", - "addRule": "Ajouter une règle", - "removeRule": "Retirer une règle", - "accept": "Oui", - "reject": "Non", - "choose": "Choisir", - "upload": "Envoyer", - "cancel": "Annuler", - "dayNames": ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"], - "dayNamesShort": ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"], - "dayNamesMin": ["Di", "Lu", "Mar", "Mer", "Je", "Ve", "Sa"], - "monthNames": ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"], - "monthNamesShort": ["Jan", "Fev", "Mar", "Avr", "Mai", "Jun", "Jui", "Août", "Sept", "Oct", "Nov", "Dec"], - "today": "Aujourd'hui", - "weekHeader": "Sem", - "firstDayOfWeek": 1, - "dateFormat": "dd/mm/yy", - "weak": "Faible", - "medium": "Moyen", - "strong": "Fort", - "passwordPrompt": "Saisissez un mot de passe", - "emptyMessage": "Aucune option disponible", - "emptyFilterMessage": "Aucun résultat trouvé", - "aria": { - "trueLabel": "Vrai", - "falseLabel": "Faux", - "nullLabel": "Aucune sélection", - "pageLabel": "Page", - "firstPageLabel": "Première Page", - "lastPageLabel": "Dernière Page", - "nextPageLabel": "Page Suivante", - "previousPageLabel": "Page Précédente" - } -} diff --git a/projects/rero/ng-core/src/lib/translate/primeng/it.json b/projects/rero/ng-core/src/lib/translate/primeng/it.json deleted file mode 100644 index 6df5e28e..00000000 --- a/projects/rero/ng-core/src/lib/translate/primeng/it.json +++ /dev/null @@ -1,61 +0,0 @@ - -{ - "startsWith": "Inizia con", - "contains": "Contiene", - "notContains": "Non contiene", - "endsWith": "Finisce con", - "equals": "Equivale", - "notEquals": "Non uguale", - "noFilter": "Senza Filtro", - "filter": "Filtro", - "lt": "Meno di", - "lte": "Minore o uguale a", - "gt": "Più grande di", - "gte": "Maggiore o uguale a", - "dateIs": "La data è", - "dateIsNot": "La data non lo è", - "dateBefore": "La data è precedente", - "dateAfter": "La data è dopo", - "custom": "Personalizza", - "clear": "Cancella tutto", - "apply": "Applica", - "matchAll": "Abbina tutto", - "matchAny": "Abbina qualsiasi", - "addRule": "Aggiungi regola", - "removeRule": "Rimuovi regola", - "accept": "Si", - "reject": "No", - "choose": "Scegli", - "upload": "Carica", - "cancel": "Annulla", - "close": "Vicino", - "dayNames": ["Domenica", "Lunedi", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"], - "dayNamesShort": ["Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"], - "dayNamesMin": ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa"], - "monthNames": ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"], - "monthNamesShort": ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"], - "today": "Oggi", - "weekHeader": "Sett", - "firstDayOfWeek": 1, - "dateFormat": "dd/mm/yy", - "weak": "Debole", - "medium": "Medio", - "strong": "Forte", - "passwordPrompt": "Inserisci la password", - "emptyFilterMessage": "Nessuna opzione disponibile", - "emptyMessage": "nessun risultato trovato", - "aria": { - "trueLabel": "Vero", - "falseLabel": "Falso", - "nullLabel": "Non selezionato", - "pageLabel": "Pagina", - "firstPageLabel": "Prima pagina", - "lastPageLabel": "Ultima pagina", - "nextPageLabel": "Pagina successiva", - "previousPageLabel": "Pagina precedente", - "selectLabel": "Selezionare", - "unselectLabel": "Deseleziona", - "expandLabel": "Espandere", - "collapseLabel": "Crollo" - } -} diff --git a/projects/rero/ng-core/src/lib/translate/translate-service.spec.ts b/projects/rero/ng-core/src/lib/translate/translate-service.spec.ts deleted file mode 100644 index dee81c5a..00000000 --- a/projects/rero/ng-core/src/lib/translate/translate-service.spec.ts +++ /dev/null @@ -1,74 +0,0 @@ -/* - * RERO angular core - * Copyright (C) 2020 RERO - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -import { CoreConfigService } from '../core-config.service'; -import { TranslateService } from './translate-service'; -import { TranslateService as NgxTranslateService } from '@ngx-translate/core'; -import { BsLocaleService } from 'ngx-bootstrap/datepicker'; -import { of } from 'rxjs'; -import { PrimeNGConfig } from 'primeng/api'; - -class TranslateServiceMock { - private language; - - use(lang: string) { - this.language = lang; - return of({}); - } - - getBrowserLang() { - return 'en'; - } - - get currentLang() { - return this.language; - } - - addLangs(languages: Array) {} - - setDefaultLang(lang: string) {} -} - -class BsLocaleServiceMock { - use(lang: string) {} -} - -let appTranslateService; -describe('AppTranslateService', () => { - beforeEach(() => { - appTranslateService = new TranslateService( - new TranslateServiceMock() as unknown as NgxTranslateService, - new CoreConfigService(), - new BsLocaleServiceMock() as unknown as BsLocaleService, - new PrimeNGConfig() - ); - }); - it('should create an instance', () => { - expect(appTranslateService).toBeTruthy(); - }); - - it('should set language on appTranslateService', () => { - appTranslateService.setLanguage('fr').subscribe((res) => expect(res).toBeTruthy()); - }); - - it('should get Browser language on appTranslateService', () => { - expect(appTranslateService.getBrowserLang()).toBe('en'); - }); - - it('should get Browser language on appTranslateService', () => { - expect(appTranslateService.currentLanguage).toBe('en'); - }); -}); diff --git a/projects/rero/ng-core/src/lib/translate/translate-service.ts b/projects/rero/ng-core/src/lib/translate/translate-service.ts index 2908ce5f..460c221f 100644 --- a/projects/rero/ng-core/src/lib/translate/translate-service.ts +++ b/projects/rero/ng-core/src/lib/translate/translate-service.ts @@ -14,95 +14,57 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import { registerLocaleData } from '@angular/common'; import localeDe from '@angular/common/locales/de'; -import localeEnGB from '@angular/common/locales/en-GB'; +import localeEn from '@angular/common/locales/en-GB'; import localeFr from '@angular/common/locales/fr'; import localeIt from '@angular/common/locales/it'; -import { Injectable } from '@angular/core'; -import { TranslateService as NgxTranslateService } from '@ngx-translate/core'; -import moment from 'moment'; -import { defineLocale } from 'ngx-bootstrap/chronos'; -import { BsLocaleService } from 'ngx-bootstrap/datepicker'; -import { deLocale, enGbLocale, frLocale, itLocale } from 'ngx-bootstrap/locale'; -import { PrimeNGConfig } from 'primeng/api'; -import { Observable } from 'rxjs'; -import { CoreConfigService } from '../core-config.service'; -import primeNgDe from './primeng/de.json'; -import primeNgEn from './primeng/en.json'; -import primeNgFr from './primeng/fr.json'; -import primeNgIt from './primeng/it.json'; +import { inject, Injectable } from "@angular/core"; +import { TranslateService } from "@ngx-translate/core"; +import moment from "moment"; +import de from 'primelocale/de.json'; +import en from 'primelocale/en.json'; +import fr from 'primelocale/fr.json'; +import it from 'primelocale/it.json'; +import { PrimeNGConfig } from "primeng/api"; +import { Observable } from "rxjs"; +import { CoreConfigService } from "../core-config.service"; +import { registerLocaleData } from '@angular/common'; @Injectable({ providedIn: 'root' }) -export class TranslateService { - languages = { - de: { ngx: deLocale, angular: localeDe, primeng: primeNgDe }, - en: { ngx: enGbLocale, angular: localeEnGB, primeng: primeNgEn }, - fr: { ngx: frLocale, angular: localeFr, primeng: primeNgFr }, - it: { ngx: itLocale, angular: localeIt, primeng: primeNgIt } - }; +export class NgCoreTranslateService extends TranslateService { - /** - * Constructor. - * - * @param translateService Translate service. - * @param coreConfigService Configuration service. - * @param bsLocaleService Bootstrap locale service. - * @param primeNgConfig PrimeNGConfig service. - */ - constructor( - private translateService: NgxTranslateService, - private coreConfigService: CoreConfigService, - private bsLocaleService: BsLocaleService, - private primeNgConfig: PrimeNGConfig - ) { - this.init(); - } + private primengConfig = inject(PrimeNGConfig); + private coreConfigService = inject(CoreConfigService); - /** - * Set the current language. - * - * @param language the language code in iso format - * @returns an Observable of the translations - */ - setLanguage(language: string): Observable { - moment.locale(language); - this.bsLocaleService.use(language); - this.primeNgConfig.setTranslation(this.languages[language].primeng); - return this.translateService.use(language); - } + private locales = { + de: { angular: localeDe, primeng: de }, + en: { angular: localeEn, primeng: en }, + fr: { angular: localeFr, primeng: fr }, + it: { angular: localeIt, primeng: it } + }; - /** - * Return the translation of the given label. - * Proxy for "instant" method of the translate service of ngx-translate. - * @param key String to translate. - * @param interpolateParams Parameters to interpolate. - * @return Translated value as string. - */ - translate(key: string | Array, interpolateParams: any = null): string { - return this.translateService.instant(key, interpolateParams); - } + initialize(): void { + for (const [key, value] of Object.entries(this.locales)) { + registerLocaleData(value.angular, key); + } + const languages: Array = this.coreConfigService.languages; + super.addLangs(languages); - getBrowserLang() { - return this.translateService.getBrowserLang(); + super.onLangChange.subscribe(translation => { + this.use(translation.lang); + }); + super.setDefaultLang(this.coreConfigService.defaultLanguage); + if (!super.currentLang) { + super.use(this.coreConfigService.defaultLanguage); + } } - get currentLanguage(): string { - return this.translateService.currentLang - || this.coreConfigService.defaultLanguage - || 'en-GB'; - } + use(lang: string): Observable { + moment.locale(lang); + this.primengConfig.setTranslation(this.locales[lang].primeng[lang]); - private init() { - for (const [key, value] of Object.entries(this.languages)) { - defineLocale(key, value.ngx); - registerLocaleData(value.angular, key); - } - const languages: Array = this.coreConfigService.languages; - this.translateService.addLangs(languages); - this.translateService.setDefaultLang(this.coreConfigService.defaultLanguage); - this.primeNgConfig.setTranslation(this.languages[this.coreConfigService.defaultLanguage].primeng); + return super.use(lang); } }