Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly inject LOCALE_ID in angular-accelerator.module #337

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SrcDirective } from './directives/src.directive'
import { DynamicPipe } from './pipes/dynamic.pipe'
import { OcxTimeAgoPipe } from './pipes/ocxtimeago.pipe'
import { AppConfigService } from './services/app-config-service'
import { DynamicLocaleId } from './utils/dynamic-locale-id'

export class AngularAcceleratorMissingTranslationHandler implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
Expand Down Expand Up @@ -69,9 +70,7 @@ export class AngularAcceleratorMissingTranslationHandler implements MissingTrans
providers: [
{
provide: LOCALE_ID,
useFactory: (UserService: UserService) => {
return UserService.lang$.getValue()
},
useClass: DynamicLocaleId,
deps: [UserService],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BrowserModule } from '@angular/platform-browser'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { importProvidersFrom } from '@angular/core'
import { LOCALE_ID, importProvidersFrom } from '@angular/core'
import { Meta, moduleMetadata, applicationConfig, StoryFn } from '@storybook/angular'
import { TableModule } from 'primeng/table'
import { ButtonModule } from 'primeng/button'
Expand All @@ -13,8 +13,10 @@ import { MockAuthModule } from '../../mock-auth/mock-auth.module'
import { HAS_PERMISSION_CHECKER, IfPermissionDirective } from '../../directives/if-permission.directive'
import { ColumnType } from '../../model/column-type.model'
import { MenuModule } from 'primeng/menu'
import { DynamicLocaleId } from '../../utils/dynamic-locale-id'

type DataTableInputTypes = Pick<DataTableComponent, 'rows' | 'columns' | 'emptyResultsMessage' | 'selectedRows'>

const DataTableComponentSBConfig: Meta<DataTableComponent> = {
title: 'DataTableComponent',
component: DataTableComponent,
Expand All @@ -25,6 +27,11 @@ const DataTableComponentSBConfig: Meta<DataTableComponent> = {
importProvidersFrom(BrowserAnimationsModule),
{ provide: UserService, useClass: MockUserService },
{ provide: HAS_PERMISSION_CHECKER, useClass: MockUserService },
{
provide: LOCALE_ID,
useClass: DynamicLocaleId,
deps: [UserService],
},
],
}),
moduleMetadata({
Expand Down Expand Up @@ -57,25 +64,34 @@ const defaultComponentArgs: DataTableInputTypes = {
nameKey: 'Available',
sortable: false,
},
{
id: 'expiration',
columnType: ColumnType.DATE,
nameKey: 'Expiration Date',
sortable: true,
}
],
rows: [
{
id: 1,
product: 'Apples',
amount: 2,
available: false,
expiration: new Date(2021, 5, 4),
},
{
id: 2,
product: 'Bananas',
amount: 10,
available: true,
expiration: new Date(2021, 6, 4),
},
{
id: 3,
product: 'Strawberries',
amount: 5,
available: false,
expiration: new Date(2021, 7, 4),
},
],
emptyResultsMessage: 'No results',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { HttpClient, provideHttpClient, withInterceptorsFromDi } from '@angular/
import { TranslateHttpLoader } from '@ngx-translate/http-loader'
import { provideAppStateServiceMock } from '@onecx/angular-integration-interface/mocks'
import { TranslateCombinedLoader } from './utils/translate.combined.loader'
import { registerLocaleData } from '@angular/common'
import localeDE from '@angular/common/locales/de'

export function translateLoader(http: HttpClient) {
return new TranslateCombinedLoader(new TranslateHttpLoader(http, `./assets/i18n/`, '.json'))
Expand All @@ -27,6 +29,7 @@ export function translateLoader(http: HttpClient) {
})
export class StorybookTranslateModule {
constructor(translateService: TranslateService) {
registerLocaleData(localeDE)
const lang = translateService.getBrowserLang()
const supportedLanguages = ['de', 'en']
if (lang && supportedLanguages.includes(lang)) {
Expand Down
18 changes: 18 additions & 0 deletions libs/angular-accelerator/src/lib/utils/dynamic-locale-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UserService } from '@onecx/angular-integration-interface'

export class DynamicLocaleId {
constructor(private userService: UserService) {
Object.getOwnPropertyNames(String.prototype).forEach((k) => {
if (k != 'valueOf') {
;(this as any)[k] = function (...args: any[]) {
const str = this.valueOf()
return str[k](...args)
}
}
})
}

valueOf() {
return this.userService.lang$.getValue()
}
}
Loading