import { ITranslationService } from "./ITranslationService" ; import { HttpClient , HttpClientConfiguration } from "@microsoft/sp-http" ; import { ILanguage } from "../models/ILanguage" ; import { IDictionary } from "../models/IDictionary" ; import { ITranslatorLanguage } from "../models/ITranslatorLanguage" ; import { IDetectedLanguage } from "../models/IDetectedLanguage" ; import { ITranslationResult } from "../models/ITranslationResult" ; import { IBreakSentenceResult } from "../models/IBreakSentenceResult" ; export class TranslationService implements ITranslationService { private httpClient : HttpClient ; private apiKey : string ; private headers : Headers ; private host : string ; param httpClient param apiKey param apiRegion param regionSpecifier constructor ( httpClient , apiKey , apiRegion = undefined , regionSpecifier = undefined ) { this . httpClient = httpClient ; this . apiKey = apiKey ; this . host = regionSpecifier ? `api-${ regionSpecifier }.cognitive.microsofttranslator.com` : "api.cognitive.microsofttranslator.com" ; this . headers = new Headers ( ) ; this . headers . append ( "Content-type" , "application/json" ) ; this . headers . append ( "Ocp-Apim-Subscription-Key" , this . apiKey ) ; if ( apiRegion ) { this . headers . append ( "Ocp-Apim-Subscription-Region" , apiRegion ) ; } } public async getAvailableLanguages ( supportedLanguages ) { const httpClient = this . httpClient ; const path = "languages?api-version=3.0&scope=dictionary" ; const result = await httpClient . get ( `https://${ this . host }/${ path }` , new HttpClientConfiguration ( { } ) , { headers : this . headers } ) ; if ( ! result . ok ) { const resultData = await result . json ( ) ; throw new Error ( resultData . error . message ) ; } const translatorLanguages = ( await result . json ( ) ) . dictionary ; const languages = supportedLanguages . map ( ( languageCode ) => { if ( translatorLanguages [ languageCode ] ) { return { label : translatorLanguages [ languageCode ] . nativeName , code : languageCode } ; } } ) ; return languages ; } public async detectLanguage ( text ) { const httpClient = this . httpClient ; const path = "detect?api-version=3.0" ; const body = JSON . stringify ( [ { Text : text } ] ) ; const result = await httpClient . post ( `https://${ this . host }/${ path }` , new HttpClientConfiguration ( { } ) , { headers : this . headers , body : body } ) ; if ( ! result . ok ) { const resultData = await result . json ( ) ; throw new Error ( resultData . error . message ) ; } const detectLanguageInfo = await result . json ( ) ; if ( detectLanguageInfo . some ( ( langInfo ) => langInfo . score >= 0.8 && langInfo . isTranslationSupported ) ) { return detectLanguageInfo . filter ( ( langInfo ) => langInfo . score >= 0.8 && langInfo . isTranslationSupported ) [ 0 ] ; } return null ; } public async translate ( sourceText , languageCode , asHtml ) { const httpClient = this . httpClient ; const path = `translate?api-version=3.0&to=${ languageCode }&textType=${ asHtml ? "html" : "plain" }` ; const body = JSON . stringify ( [ { Text : sourceText } ] ) ; const result = await httpClient . post ( `https://${ this . host }/${ path }` , new HttpClientConfiguration ( { } ) , { headers : this . headers , body : body } ) ; if ( ! result . ok ) { const resultData = await result . json ( ) ; throw new Error ( resultData . error . message ) ; } const translationInfo = await result . json ( ) ; if ( translationInfo . length > 0 ) { return translationInfo [ 0 ] ; } else { return null ; } } public async breakSentence ( sourceText ) { const httpClient = this . httpClient ; const path = `breaksentence?api-version=3.0` ; const body = JSON . stringify ( [ { Text : sourceText } ] ) ; const result = await httpClient . post ( `https://${ this . host }/${ path }` , new HttpClientConfiguration ( { } ) , { headers : this . headers , body : body } ) ; if ( ! result . ok ) { const resultData = await result . json ( ) ; throw new Error ( resultData . error . message ) ; } const breakSentenceInfo = await result . json ( ) ; if ( breakSentenceInfo . length > 0 ) { return breakSentenceInfo [ 0 ] ; } else { return null ; } } }