-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 add support for translation override
- Loading branch information
Showing
5 changed files
with
70 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,58 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { Observable } from 'rxjs'; | ||
import { I18nService } from './shared/i18n.service'; | ||
import { Observable, of, zip } from 'rxjs'; | ||
import { I18nService, CustomLoader } from './shared/i18n.service'; | ||
import { EventService } from './shared/event.service'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { map, publishReplay, refCount } from 'rxjs/operators'; | ||
import { map, mergeMap, switchMap } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LanguageGuard implements CanActivate { | ||
|
||
// | ||
private applicationLanguages: Observable<string[]>; | ||
private eventLanguages: {[name:string]: Observable<string[]>} = {}; | ||
|
||
// | ||
|
||
constructor(private i18nService: I18nService, private eventService: EventService, private translate: TranslateService) { | ||
constructor(private i18nService: I18nService, private eventService: EventService, private translate: TranslateService, private customLoader: CustomLoader) { | ||
} | ||
|
||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { | ||
|
||
const persisted = this.i18nService.getPersistedLanguage(); | ||
|
||
//set before calling, to avoid any strange flashes | ||
if (persisted) { | ||
if (persisted && this.translate.currentLang != persisted) { | ||
this.translate.use(persisted); | ||
} | ||
|
||
const eventShortName = next.params['eventShortName']; | ||
const req = eventShortName ? this.getForEvent(eventShortName) : this.getForApp(); | ||
|
||
return req.pipe(map(availableLanguages => { | ||
this.useLanguage(availableLanguages, persisted); | ||
return true; | ||
})); | ||
return req.pipe(switchMap(availableLanguages => { | ||
const lang = this.extractLang(availableLanguages, persisted); | ||
return this.i18nService.useTranslation(eventShortName, lang); | ||
})) | ||
} | ||
|
||
private getForEvent(eventShortName: string): Observable<string[]> { | ||
|
||
if (!this.eventLanguages[eventShortName]) { | ||
this.eventLanguages[eventShortName] = this.eventService.getAvailableLanguageForEvent(eventShortName).pipe(publishReplay(1), refCount()) | ||
} | ||
|
||
return this.eventLanguages[eventShortName]; | ||
return this.eventService.getAvailableLanguageForEvent(eventShortName); | ||
} | ||
|
||
private getForApp(): Observable<string[]> { | ||
|
||
if (!this.applicationLanguages) { | ||
this.applicationLanguages = this.i18nService.getAvailableLanguages().pipe(map(languages => languages.map(l => l.locale)), publishReplay(1), refCount()); | ||
} | ||
|
||
return this.applicationLanguages; | ||
return this.i18nService.getAvailableLanguages().pipe(map(languages => languages.map(l => l.locale))); | ||
} | ||
|
||
private useLanguage(availableLanguages: string[], persisted: string): void { | ||
private extractLang(availableLanguages: string[], persisted: string): string { | ||
var lang; | ||
if (availableLanguages.indexOf(persisted) >= 0) { | ||
this.translate.use(persisted); | ||
lang = persisted; | ||
} else if (availableLanguages.indexOf(this.translate.getBrowserLang()) >= 0) { | ||
this.translate.use(this.translate.getBrowserLang()); | ||
lang = this.translate.getBrowserLang(); | ||
} else { | ||
this.translate.use(availableLanguages[0]); | ||
lang = availableLanguages[0]; | ||
} | ||
return lang; | ||
} | ||
|
||
} |
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