Skip to content

Commit

Permalink
fix: use an observable for getting translations
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryT-CG committed Jan 29, 2024
1 parent 394f063 commit bbe86cb
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 129 deletions.
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
"@ngneat/falso": "^6.4.0",
"@ngx-translate/core": "^14.0.0",
"@ngx-translate/http-loader": "^7.0.0",
"@onecx/accelerator": "^4.1.0",
"@onecx/integration-interface": "^4.1.0",
"@onecx/keycloak-auth": "^4.1.0",
"@onecx/portal-integration-angular": "^4.1.0",
"@onecx/portal-layout-styles": "^4.1.0",
"@onecx/accelerator": "^4.1.2",
"@onecx/integration-interface": "^4.1.2",
"@onecx/keycloak-auth": "^4.1.2",
"@onecx/portal-integration-angular": "^4.1.2",
"@onecx/portal-layout-styles": "^4.1.2",
"file-saver": "^2.0.5",
"i18n-iso-countries": "^7.6.0",
"ngx-color": "^8.0.3",
Expand Down
5 changes: 3 additions & 2 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { AppComponent } from './app.component'
import { RouterTestingModule } from '@angular/router/testing'
import { NO_ERRORS_SCHEMA } from '@angular/core'

import { AppComponent } from './app.component'

describe('AppComponent', () => {
beforeEach(async () => {
Expand Down
27 changes: 24 additions & 3 deletions src/app/onecx-theme-remote.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { HttpClient } from '@angular/common/http'
import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'
import { MissingTranslationHandler, TranslateLoader, TranslateModule } from '@ngx-translate/core'

import { addInitializeModuleGuard, InitializeModuleGuard, PortalCoreModule } from '@onecx/portal-integration-angular'
import {
addInitializeModuleGuard,
AppStateService,
ConfigurationService,
createTranslateLoader,
PortalCoreModule,
PortalMissingTranslationHandler
} from '@onecx/portal-integration-angular'

const routes: Routes = [
{
Expand All @@ -10,9 +19,21 @@ const routes: Routes = [
}
]
@NgModule({
imports: [PortalCoreModule.forMicroFrontend(), RouterModule.forChild(addInitializeModuleGuard(routes))],
imports: [
PortalCoreModule.forMicroFrontend(),
RouterModule.forChild(addInitializeModuleGuard(routes)),
TranslateModule.forRoot({
isolate: true,
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient, AppStateService]
},
missingTranslationHandler: { provide: MissingTranslationHandler, useClass: PortalMissingTranslationHandler }
})
],
exports: [],
providers: [InitializeModuleGuard],
providers: [ConfigurationService],
schemas: []
})
export class OneCXThemeModule {
Expand Down
9 changes: 5 additions & 4 deletions src/app/shared/label.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router'
import { TranslateService } from '@ngx-translate/core'
import { Observable } from 'rxjs'
import { Observable, map } from 'rxjs'

//dont use `providedIn root` - wont work when we are in shell
@Injectable()
export class LabelResolver implements Resolve<string> {
constructor(private translate: TranslateService) {}
/* eslint-disable @typescript-eslint/no-unused-vars */
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): string | Observable<string> | Promise<string> {
return route.data['breadcrumb'] ? this.translate.instant(route.data['breadcrumb']) : route.routeConfig?.path
resolve(route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): string | Observable<string> | Promise<string> {
return route.data['breadcrumb']
? this.translate.get(route.data['breadcrumb']).pipe(map((t) => t.toString()))
: route.routeConfig?.path ?? ''
}
}
31 changes: 4 additions & 27 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, NgModule } from '@angular/core'
import { CommonModule } from '@angular/common'
import { HttpClient } from '@angular/common/http'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import {
MissingTranslationHandler,
MissingTranslationHandlerParams,
TranslateLoader,
TranslateModule,
TranslateService
} from '@ngx-translate/core'
import { TranslateModule, TranslateService } from '@ngx-translate/core'
import { ColorSketchModule } from 'ngx-color/sketch'
import { ErrorTailorModule } from '@ngneat/error-tailor'

Expand Down Expand Up @@ -37,28 +30,20 @@ import { ToastModule } from 'primeng/toast'
import {
AppStateService,
ConfigurationService,
createTranslateLoader,
PortalDialogService,
PortalApiConfiguration
} from '@onecx/portal-integration-angular'

import { Configuration } from '../generated'
import { Configuration } from 'src/app/generated'
import { LabelResolver } from './label.resolver'
import { environment } from '../../environments/environment'
import { environment } from 'src/environments/environment'
import { ImageContainerComponent } from './image-container/image-container.component'
import { ThemeColorBoxComponent } from './theme-color-box/theme-color-box.component'

export function apiConfigProvider(configService: ConfigurationService, appStateService: AppStateService) {
return new PortalApiConfiguration(Configuration, environment.apiPrefix, configService, appStateService)
}

export class MyMissingTranslationHandler implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
console.log(`Missing translation for ${params.key}`)
return params.key
}
}

@NgModule({
declarations: [ImageContainerComponent, ThemeColorBoxComponent],
imports: [
Expand Down Expand Up @@ -86,15 +71,7 @@ export class MyMissingTranslationHandler implements MissingTranslationHandler {
TabViewModule,
TableModule,
ToastModule,
TranslateModule.forRoot({
isolate: true,
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient, AppStateService]
},
missingTranslationHandler: { provide: MissingTranslationHandler, useClass: MyMissingTranslationHandler }
}),
TranslateModule,
ErrorTailorModule.forRoot({
controlErrorsOn: { async: true, blur: true, change: true },
errors: {
Expand Down
2 changes: 1 addition & 1 deletion src/app/theme/theme-designer/theme-designer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ocx-page-header
header="{{ theme ? theme.name : 'Theme Designer' }}"
[subheader]="'THEME.DESIGNER.' + mode + '.SUBHEADER' | translate"
[actions]="actions"
[actions]="(actions$ | async) ?? []"
[figureImage]="fetchingLogoUrl"
[figureBackground]="!fetchingLogoUrl"
></ocx-page-header>
Expand Down
Loading

0 comments on commit bbe86cb

Please sign in to comment.