import { localStorageUtil } from '../../../utils/local-storage.utils'; import { Component, OnInit, Input, OnDestroy } from '@angular/core'; import { LanguageService } from '../../../services/router-language/route-language.service'; import { ActivatedRoute } from '@angular/router'; import { ManageSubscriptionsContainer } from '../../../containers/manage-subs/manage-subscriptions.container'; import { map, tap, filter } from 'rxjs/operators'; import { Subscription } from 'rxjs'; const langMappings = { // It's counter intuitive but in english should point to french and in french it should point to english en: 'Français', fr: 'English' }; @Component({ selector: 'sc-lang-toggle-micro', templateUrl: './language-toggle.component.html' }) export class LanguageToggleComponent extends ManageSubscriptionsContainer implements OnInit, OnDestroy { otherLanguageText: string; suppressDataClear: boolean; translatedURL: string; secondaryRoutes = [ 'local-pro', 'pro-local', 'home-depot-installer', 'installateur-home-depot', 'weekly-flyer', 'circulaire-hebdomadaire', 'results', 'resultats' ]; publicProfile = ['profile', 'profil']; @Input() noFlag?: boolean; @Input() oabLanguagetoggle?: boolean; productSub: Subscription; constructor( private route: ActivatedRoute, private langService: LanguageService) { super(); } ngOnInit() { if (this.route.snapshot.data['product']) { const lang = this.langService.getLanguage(); this.productSub = this.route.data.pipe( map(data => data['product']), filter(product => product && product.urls), tap(product => { this.translatedURL = lang === 'en' ? product.urls.fr : product.urls.en; }) ).subscribe(); this.subscriptions.push(this.productSub); } else { const projectIdentifierTranlations = this.langService.getProjectIdentifierTranlations(); const projectIdentifiers = this.langService.getPath().split('/'); const projectIdentifier = projectIdentifiers[1]; const projectIdentifierTranslation = projectIdentifierTranlations[projectIdentifier]; this.translatedURL = this.langService.getPath().replace(projectIdentifier, projectIdentifierTranslation); const secondProjectIdentifier = projectIdentifiers[2]; if (this.isPRorISRoute(secondProjectIdentifier)) { const secondProjectIdentifierTranslation = projectIdentifierTranlations[secondProjectIdentifier]; this.translatedURL = this.translatedURL.replace(secondProjectIdentifier, secondProjectIdentifierTranslation); const profileRoute = projectIdentifiers[3]; if (this.isPublicProfile(profileRoute)) { const profileTranslation = projectIdentifierTranlations[profileRoute]; this.translatedURL = this.translatedURL.replace(profileRoute, profileTranslation); } } // Add query string to end this.translatedURL = `${this.translatedURL}${this.langService.getQueryString()}`; } this.otherLanguageText = langMappings[this.langService.getLanguage()]; this.route.data.subscribe(route => { this.suppressDataClear = route.suppressDataClear; }); } isPRorISRoute(route) { return route && this.secondaryRoutes.indexOf(route) > -1; } isPublicProfile(route) { return route && this.publicProfile.indexOf(route) > -1; } toggleLanguage() { if (!this.suppressDataClear) { localStorageUtil.clearLocalStorage('serviceConnect'); } this.translatedURL ? this.redirectToURL(this.translatedURL) : this.langService.toggleLanguageForPageReload(); } redirectToURL(URL: string) { if (typeof window !== 'undefined') { window.location.href = URL; } } ngOnDestroy() { super.ngOnDestroy(); } }