import { I18nConfig } from "./i18n.interfaces" ; import { Variables } from "./variables" ; const GoogleTranslate = require ( 'google-translate' ) ; interface GoogleError { error ? : GoogleError ; code ? : number ; message ? : string ; errors ? : GoogleError [ ] ; status ? : string ; } interface GoogleTranslation { originalText : string ; translatedText : string ; } export class AutoTranslator { private readonly varibales = new Variables ( ) ; constructor ( private config ) { } async translate ( input , locale ) { const apiKey = this . config . googleTranslateApiKey ; if ( ! apiKey ) { return Promise . reject ( "googleTranslateApiKey is not set." ) ; } try { const source = this . config . defaultLocale . substr ( 0 , 2 ) ; const target = locale . substr ( 0 , 2 ) ; const gt = GoogleTranslate ( apiKey ) ; return new Promise < string > ( ( resolve , reject ) => { gt . translate ( input , source , target , ( error , translation ) => { if ( error ) { if ( error . body ) { const ge = JSON . parse ( error . body ) as GoogleError ; const message = ge . message || ge . error && ge . error . message ; reject ( message ) ; } else { reject ( error ) ; } } else { const result = this . handleTranslation ( translation ) ; resolve ( result ) ; } } ) ; } ) ; } catch ( e ) { return Promise . reject ( e ) ; } } private handleTranslation ( translation ) { const originalVars = this . varibales . parseVariables ( translation . originalText ) || [ ] ; const translatedVars = this . varibales . parseVariables ( translation . translatedText ) || [ ] ; const result = this . varibales . replaceVariablesWithVariables ( translation . translatedText , translatedVars , originalVars ) ; return result ; } }