-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: observe for/and change langauge from outside
- Loading branch information
Showing
11 changed files
with
165 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,5 +136,8 @@ | |
} | ||
} | ||
} | ||
}, | ||
"cli": { | ||
"analytics": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...x-cookie-consent/src/lib/services/ngx-cookie-eventbus/ngx-cookie-eventbus.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { NgxCookieEventbusService } from './ngx-cookie-eventbus.service'; | ||
|
||
describe('NgxCookieEventbusService', () => { | ||
let service: NgxCookieEventbusService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(NgxCookieEventbusService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should have languageChangedSubject', () => { | ||
expect(service.languageChangedSubject).toBeTruthy(); | ||
}); | ||
|
||
it('should have languageChanged$', () => { | ||
expect(service.languageChanged$).toBeTruthy(); | ||
}); | ||
|
||
it('should have languageUpdatedSubject', () => { | ||
expect(service.languageUpdatedSubject).toBeTruthy(); | ||
}); | ||
|
||
it('should have languageUpdated$', () => { | ||
expect(service.languageUpdated$).toBeTruthy(); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...ts/ngx-cookie-consent/src/lib/services/ngx-cookie-eventbus/ngx-cookie-eventbus.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NgxCookieEventbusService { | ||
public languageChangedSubject: Subject<boolean>; | ||
public languageChanged$: Observable<boolean>; | ||
|
||
public languageUpdatedSubject: Subject<string>; | ||
public languageUpdated$: Observable<string>; | ||
|
||
|
||
constructor() { | ||
this.languageChangedSubject = new Subject<boolean>(); | ||
this.languageChanged$ = this.languageChangedSubject.asObservable(); | ||
|
||
this.languageUpdatedSubject = new Subject<string>(); | ||
this.languageUpdated$ = this.languageUpdatedSubject.asObservable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 26 additions & 2 deletions
28
...ects/ngx-cookie-consent/src/lib/services/ngx-cookie-manager/ngx-cookie-manager.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { NgxCookieService } from '../ngx-cookie/ngx-cookie.service'; | ||
import { NgxCookieConsentConfigService } from '../../config/ngx-cookie-consent-config.service'; | ||
import { NgxCookieEventbusService } from '../ngx-cookie-eventbus/ngx-cookie-eventbus.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NgxCookieManagerService { | ||
cookieUpdated$ = new Subject<{name: string, state: boolean}>(); | ||
languageChanged$: Observable<string>; | ||
|
||
constructor( | ||
private cookieService: NgxCookieService, | ||
private cookieConsentConfig: NgxCookieConsentConfigService, | ||
) { } | ||
private cookieEventbusService: NgxCookieEventbusService, | ||
) { | ||
this.languageChanged$ = this.cookieEventbusService.languageUpdated$; | ||
} | ||
|
||
private getConfig(key: string): any { | ||
return (this.cookieConsentConfig as any)[key]; | ||
} | ||
|
||
private setConfig(key: string, value: string): void { | ||
(this.cookieConsentConfig as any)[key] = value; | ||
} | ||
|
||
private getPrefixedCookieName(name: string): string { | ||
return this.getConfig('cookiePrefix') + name; | ||
} | ||
|
||
getCookie(cookieName: string): boolean { | ||
return this.cookieService.get(this.getPrefixedCookieName(cookieName)) === true; | ||
} | ||
|
||
updateDisplayLanguage(locale: string): void { | ||
const availableLanguages = this.getConfig('availableLanguages'); | ||
|
||
if (!availableLanguages.includes(locale)) { | ||
return; | ||
} | ||
|
||
this.setConfig('defaultLanguage', locale); | ||
this.cookieEventbusService.languageChangedSubject.next(true); | ||
} | ||
|
||
getDisplayLanguage(): string { | ||
return this.getConfig('defaultLanguage'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { NgxCookieManagerService } from 'projects/ngx-cookie-consent/src/public-api'; | ||
|
||
@Component({ | ||
selector: 'app-home', | ||
templateUrl: './home.component.html', | ||
styleUrls: ['./home.component.scss'] | ||
selector: 'app-home', | ||
templateUrl: './home.component.html', | ||
styleUrls: ['./home.component.scss'] | ||
}) | ||
export class HomeComponent implements OnInit { | ||
|
||
constructor() { } | ||
constructor( | ||
private cookieManager: NgxCookieManagerService | ||
) { | ||
} | ||
|
||
ngOnInit(): void { | ||
} | ||
ngOnInit(): void { | ||
} | ||
|
||
get currentLanguage(): string { | ||
return this.cookieManager.getDisplayLanguage(); | ||
} | ||
|
||
switchLanguage(lang: string): void { | ||
this.cookieManager.updateDisplayLanguage(lang); | ||
} | ||
|
||
} |