import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Language } from '@app/models/language' ; import * as fromConfig from '@app/store/reducers' ; import { Translation } from '@models/translation' ; import { select , Store } from '@ngrx/store' ; import { throwError } from 'rxjs' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/operator/map' ; import { Observable } from 'rxjs/Observable' ; import { catchError , map } from 'rxjs/operators' ; @ Injectable ( { providedIn : 'root' , } ) export class TranslateService { /** URL of the Yandex API */ SERVICE_URL = 'https://translate.yandex.net/api/v1.5/tr.json' ; /** Yandex Translate API key */ apiKey : string ; param http constructor ( private http , private store ) { this . store . pipe ( select ( fromConfig . getConfig ) ) . subscribe ( config => { this . apiKey = config . apiKey ; } ) ; } param word param fromLang param toLang detectLanguage ( word , fromLang ) { const requestUrl = this . createRequest ( word , fromLang ) ; return this . http . get < any > ( requestUrl ) . pipe ( map ( response => { console . log ( `Detected language: ${ response . lang }` ) ; return response . lang ; } ) , catchError ( err => this . handleError ( err ) ) ) ; } /**
     * Return json list with available languages from Yandex API
     */ getLanguagesList ( ) { return this . http . get < any > ( this . getLanguagesUrl ( ) ) . pipe ( map ( res => this . sortLanguages ( res . langs ) ) , catchError ( err => this . handleError ( err ) ) ) ; } param languages returns sortLanguages ( languages ) { let sortedLangs = [ ] ; for ( const key in languages ) { sortedLangs . push ( { key : key , value : languages [ key ] , } ) ; } sortedLangs . sort ( ( a , b ) => a . value . localeCompare ( b . value ) ) ; sortedLangs = sortedLangs . map ( item => { return new Language ( item . key , item . value ) ; } ) ; return sortedLangs ; } param word param fromLang param toLang getTranslation ( word , fromLang , toLang ) { const requestUrl = this . createRequest ( word , fromLang , toLang ) ; return this . http . get < any > ( requestUrl ) . pipe ( map ( response => { return new Translation ( response . code , response . lang , response . text ) ; } ) , catchError ( err => this . handleError ( err ) ) ) ; } param word param fromLang param toLang createRequest ( word , fromLang = undefined , toLang = undefined ) { if ( fromLang === 'ad' ) { return this . getAutoDetectLanguageUrl ( ) + '&text=' + encodeURIComponent ( word ) ; } else { return this . getTranslateUrl ( ) + '&text=' + encodeURIComponent ( word ) + '&lang=' + fromLang + '-' + toLang ; } } /**
     * Return URL for language request from Yandex Translate API
     */ getLanguagesUrl ( ) { return this . SERVICE_URL + '/getLangs?key=' + this . apiKey + '&ui=en' ; } /**
     * Return base part of URL for translation request
     */ getTranslateUrl ( ) { return this . SERVICE_URL + '/translate?key=' + this . apiKey ; } /**
     * Return URL for auto detect API endpoint
     */ getAutoDetectLanguageUrl ( ) { return this . SERVICE_URL + '/detect?key=' + this . apiKey ; } param errorResponse private handleError ( errorResponse ) { console . error ( errorResponse ) ; return throwError ( errorResponse . error . message || 'Please check your network connection' ) ; } }