add language service
This commit is contained in:
parent
884df60cb8
commit
9c2c429df3
@ -4,7 +4,7 @@ import { MatIconModule } from '@angular/material/icon';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||||
import { SortByPipe } from './sorting';
|
||||
import { HumanizePipe } from './utils';
|
||||
import { CapitalizePipe, HumanizePipe } from './utils';
|
||||
import {
|
||||
HiddenActionComponent,
|
||||
LogoComponent,
|
||||
@ -24,11 +24,9 @@ import { IqserIconsModule } from './icons';
|
||||
import { IqserButtonsModule } from './buttons';
|
||||
import { IqserScrollbarModule } from './scrollbar';
|
||||
import { IqserEmptyStatesModule } from './empty-states';
|
||||
import { LogPipe } from './utils/pipes/log.pipe';
|
||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatDialogModule } from '@angular/material/dialog';
|
||||
import { CapitalizePipe } from './utils/pipes/capitalize.pipe';
|
||||
import { KeycloakAngularModule } from 'keycloak-angular';
|
||||
import { MatCheckboxModule } from '@angular/material/checkbox';
|
||||
import { UploadFileComponent } from './upload-file/upload-file.component';
|
||||
@ -69,12 +67,12 @@ const components = [
|
||||
DragDropFileUploadDirective,
|
||||
];
|
||||
|
||||
const pipes = [SortByPipe, HumanizePipe, CapitalizePipe, LogPipe];
|
||||
const pipes = [SortByPipe, HumanizePipe, CapitalizePipe];
|
||||
|
||||
@NgModule({
|
||||
declarations: [...components, ...pipes],
|
||||
imports: [CommonModule, ...matModules, ...modules, FormsModule, ReactiveFormsModule, KeycloakAngularModule, MatProgressBarModule],
|
||||
exports: [...components, ...pipes, ...modules, LogPipe],
|
||||
exports: [...components, ...pipes, ...modules],
|
||||
})
|
||||
export class CommonUiModule {
|
||||
static forRoot<T extends BaseUserPreferenceService>(options?: CommonUiOptions<T>): ModuleWithProviders<CommonUiModule> {
|
||||
|
||||
@ -6,3 +6,4 @@ export * from './composite-route.guard';
|
||||
export * from './stats.service';
|
||||
export * from './entities-map.service';
|
||||
export * from './base-user-preference.service';
|
||||
export * from './language.service';
|
||||
|
||||
63
src/lib/services/language.service.ts
Normal file
63
src/lib/services/language.service.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import 'dayjs/locale/en';
|
||||
import 'dayjs/locale/de';
|
||||
import dayjs, { Dayjs } from 'dayjs';
|
||||
import { DateAdapter } from '@angular/material/core';
|
||||
import arraySupport from 'dayjs/plugin/arraySupport';
|
||||
import localeData from 'dayjs/plugin/localeData';
|
||||
import { registerLocaleData } from '@angular/common';
|
||||
import localeDe from '@angular/common/locales/de';
|
||||
import { BaseUserPreferenceService } from './base-user-preference.service';
|
||||
|
||||
dayjs.extend(arraySupport);
|
||||
dayjs.extend(localeData);
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LanguageService {
|
||||
constructor(
|
||||
private readonly _translateService: TranslateService,
|
||||
private readonly _userPreferenceService: BaseUserPreferenceService,
|
||||
private readonly _dateAdapter: DateAdapter<Dayjs>,
|
||||
) {
|
||||
registerLocaleData(localeDe);
|
||||
_translateService.addLangs(['en', 'de']);
|
||||
_translateService.setDefaultLang('en');
|
||||
}
|
||||
|
||||
get currentLanguage() {
|
||||
return this._translateService.currentLang;
|
||||
}
|
||||
|
||||
async set(language: string): Promise<void> {
|
||||
dayjs.locale(language);
|
||||
this._dateAdapter.setLocale(language);
|
||||
await firstValueFrom(this._translateService.use(language));
|
||||
}
|
||||
|
||||
isAvailable(language: string): boolean {
|
||||
return this._translateService.getLangs().includes(language);
|
||||
}
|
||||
|
||||
async setInitialLanguage(): Promise<void> {
|
||||
const defaultLang = this.#getDefaultLanguage();
|
||||
document.documentElement.lang = defaultLang;
|
||||
this._translateService.setDefaultLang(defaultLang);
|
||||
await this.set(defaultLang);
|
||||
}
|
||||
|
||||
async change(language: string) {
|
||||
await this._userPreferenceService.saveLanguage(language);
|
||||
document.documentElement.lang = language;
|
||||
await this.set(language);
|
||||
}
|
||||
|
||||
#getDefaultLanguage() {
|
||||
const preferredLang = this._userPreferenceService.getLanguage();
|
||||
|
||||
return this.isAvailable(preferredLang) ? preferredLang : 'en';
|
||||
}
|
||||
}
|
||||
1
src/lib/standalone/index.ts
Normal file
1
src/lib/standalone/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './log.pipe';
|
||||
@ -2,6 +2,7 @@ import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({
|
||||
name: 'log',
|
||||
standalone: true,
|
||||
})
|
||||
export class LogPipe implements PipeTransform {
|
||||
transform<T>(value: T, message = ''): T {
|
||||
@ -2,6 +2,7 @@ export * from './functions';
|
||||
export * from './operators';
|
||||
export * from './auto-unsubscribe.directive';
|
||||
export * from './pipes/humanize.pipe';
|
||||
export * from './pipes/capitalize.pipe';
|
||||
export * from './types/events.type';
|
||||
export * from './types/utility-types';
|
||||
export * from './types/tooltip-positions.type';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user