import 'rxjs/add/operator/map'; import { Injectable, Injector, Inject, OnDestroy } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { TranslateService } from '@ngx-translate/core'; import { CookieService } from 'ngx-cookie'; import { LangConsts } from '../../constants/lang.constants'; import { domain } from '../../tokens'; import { REQUEST } from '@nguniversal/express-engine/tokens'; import { Subscription } from 'rxjs'; @Injectable() export class LanguageService implements OnDestroy { translateSub: Subscription; domain: string; constructor(private readonly translate: TranslateService, private cookieService: CookieService, private injector: Injector, @Inject(domain) private shadowedDomain ) { this.domain = shadowedDomain; } ngOnDestroy() { if (this.translateSub) { this.translateSub.unsubscribe(); } } getProjectIdentifierTranlations() { return { 'home-services': 'services-a-domicile', 'services-a-domicile': 'home-services', 'cart': 'panier', 'panier': 'cart', 'product': 'produit', 'produit': 'product', 'local-pro': 'pro-local', 'pro-local': 'local-pro', 'home-depot-installer': 'installateur-home-depot', 'installateur-home-depot': 'home-depot-installer', 'profile': 'profil', 'profil': 'profile', 'value-centre': 'centre-de-valeur', 'centre-de-valeur': 'value-centre', 'weekly-flyer': 'circulaire-hebdomadaire', 'circulaire-hebdomadaire': 'weekly-flyer', 'associate-tools': 'outils-associes', 'outils-associes': 'associate-tools', 'recherche': 'search', 'search': 'recherche', 'results': 'resultats', 'resultats': 'results', 'contact-us': 'contactez-nous', 'contactez-nous': 'contact-us', 'order-status': 'etat-de-la-commande', 'etat-de-la-commande': 'order-status', 'customer-support': 'service-a-la-clientele', 'service-a-la-clientele': 'customer-support', 'account': 'compte', 'compte': 'account' }; } getPath() { if (typeof window === 'undefined') { const request = this.injector.get(REQUEST); return request.url; } else { return window.location.pathname; } } getQueryString() { // Get the query string from the URL if (typeof window === 'undefined') { // TODO: future requirement, handle query string on server side // Not needed for Search module (first module with requirement, no SSR supported due to no need for web crawling/indexing) return ''; } else { return window.location.search; } } toggleLanguage(): Observable { const lang = this.getLanguage() === 'en' ? 'fr' : 'en'; return this.setLanguage(lang); } toggleLanguageForPageReload(): void { const lang = this.getLanguage() === 'en' ? 'fr' : 'en'; let url: string = this.getPath(); /* istanbul ignore next */ this.translateSub = this.translate.getTranslation(lang).subscribe((translationObj: boolean) => { if (url.substr(0, 1) === '/') { url = url.substr(1); } const translatedUrl = `/${url.split('/').map(segment => { return translationObj[segment] ? translationObj[segment] : segment; }).join('/')}`; if (typeof window !== 'undefined') { window.location.href = window.location.origin + translatedUrl; } }); } getLanguage(passedPath?: string): string { const path = passedPath || this.getPath(); // Add your project identifiers here. return path.includes('/services-a-domicile') || path.includes('/mon-compte') || path.includes('/pro-local') || path.includes('/produit') || path.includes('/panier') || path.includes('/circulaire-hebdomadaire') || path.includes('/outils-associes') || path.includes('/recherche') || path.includes('/contactez-nous') || path.includes('/etat-de-la-commande') || path.includes('/service-a-la-clientele') || path.includes('/compte') ? 'fr' : 'en'; } setLanguage(value: string): Observable { const expiryDate = new Date(); expiryDate.setDate(expiryDate.getDate() + 28); typeof document !== 'undefined' && this.cookieService.put(LangConsts.LANG_KEY, value, { expires: expiryDate, domain: this.domain, path: '/' }); return this.translate.use(value); } translateToOtherLanguage(lang) { return this.translate.getTranslation(lang); } translateURL(URL: string): string { if (URL.substr(0, 1) === '/') { URL = URL.substr(1); } return `/${URL.split('/').map(segment => { return this.translate.instant(segment); }).join('/')}`; } translateURLAsync(URL: string): Observable { if (URL.substr(0, 1) === '/') { URL = URL.substr(1); } return this.translate.get(URL.split('/')); } getTranslation(): Observable { return this.translate.getTranslation(this.getLanguage()); } }