Skip to content

Commit

Permalink
Introduce I18nService as translation wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerstolzenberg committed Mar 3, 2024
1 parent facd6b5 commit 8f6c20e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { TranslocoService } from '@ngneat/transloco';
import { NGXLogger } from 'ngx-logger';
import { Title } from '@angular/platform-browser';
import { I18nService } from './i18n/i18n.service';
import { firstValueFrom } from 'rxjs';

@Component({
selector: 'app-root',
Expand All @@ -10,14 +11,14 @@ import { Title } from '@angular/platform-browser';
})
export class AppComponent implements OnInit {
constructor(
private i18nService: TranslocoService,
private i18nService: I18nService,
private logger: NGXLogger,
private title: Title
) {
this.logger.info('Active lang: ' + this.i18nService.getActiveLang());
}

ngOnInit(): void {
this.i18nService.selectTranslate('header.title').subscribe(value => this.title.setTitle(value));
firstValueFrom(this.i18nService.translate('header.title')).then(value => this.title.setTitle(value));
}
}
4 changes: 2 additions & 2 deletions src/app/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { TranslocoService } from '@ngneat/transloco';
import { NGXLogger } from 'ngx-logger';
import { I18nService } from '../i18n/i18n.service';

@Component({
selector: 'app-header',
Expand All @@ -9,7 +9,7 @@ import { NGXLogger } from 'ngx-logger';
})
export class HeaderComponent {
constructor(
private i18nService: TranslocoService,
private i18nService: I18nService,
private logger: NGXLogger
) {}

Expand Down
4 changes: 3 additions & 1 deletion src/app/i18n/i18n.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isDevMode, NgModule } from '@angular/core';
import { I18nHttpLoaderService } from './i18n-http-loader.service';
import { provideHttpClient } from '@angular/common/http';
import { provideTranslocoPreloadLangs } from '@ngneat/transloco-preload-langs';
import { I18nService } from './i18n.service';

@NgModule({
exports: [TranslocoModule],
Expand All @@ -17,7 +18,8 @@ import { provideTranslocoPreloadLangs } from '@ngneat/transloco-preload-langs';
},
loader: I18nHttpLoaderService
}),
provideTranslocoPreloadLangs(['en', 'de'])
provideTranslocoPreloadLangs(['en', 'de']),
I18nService
]
})
export class I18nModule {
Expand Down
19 changes: 19 additions & 0 deletions src/app/i18n/i18n.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { TranslocoService } from '@ngneat/transloco';

@Injectable()
export class I18nService {
constructor(private readonly translocoService: TranslocoService) {}

getActiveLang() {
return this.translocoService.getActiveLang();
}

translate(key: string) {
return this.translocoService.selectTranslate<string>(key);
}

setActiveLang(languageCode: string) {
this.translocoService.setActiveLang(languageCode);
}
}
8 changes: 4 additions & 4 deletions src/app/map/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class MapService {
private readonly euBordersLayer: Promise<GeoJSON>;

constructor(
private http: HttpClient,
private log: NGXLogger,
private notificationService: NotificationService
private readonly http: HttpClient,
private readonly log: NGXLogger,
private readonly notificationService: NotificationService
) {
this.euBordersLayer = this.initEuBordersLayer();
}

private async initEuBordersLayer() {
return firstValueFrom(this.http.get<GeoJsonObject>('./assets/geo-data/european-borders.json2'))
return firstValueFrom(this.http.get<GeoJsonObject>('./assets/geo-data/european-borders.json'))
.then(jsonResponse => {
this.log.debug('Loaded borders json', jsonResponse);
return geoJson(jsonResponse, { style: euBorderStyle });
Expand Down
12 changes: 6 additions & 6 deletions src/app/notifications/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { TranslocoService } from '@ngneat/transloco';
import { NGXLogger } from 'ngx-logger';
import { HttpErrorResponse } from '@angular/common/http';
import { firstValueFrom } from 'rxjs';
import { I18nService } from '../i18n/i18n.service';

export const options = {
timeOut: 5000,
Expand All @@ -15,15 +15,15 @@ export const options = {
@Injectable()
export class NotificationService {
constructor(
private toastr: ToastrService,
private i18nService: TranslocoService,
private log: NGXLogger
private readonly toastrService: ToastrService,
private readonly i18nService: I18nService,
private readonly log: NGXLogger
) {}

async showError(message: string, error: HttpErrorResponse) {
this.log.error(message, error);
firstValueFrom(this.i18nService.selectTranslate('notifications.error-title')).then(title =>
this.toastr.error(error.message, title, options)
firstValueFrom(this.i18nService.translate('notifications.error-title')).then(title =>
this.toastrService.error(error.message, title, options)
);
}
}

0 comments on commit 8f6c20e

Please sign in to comment.