fabasoad/translation-action

View on GitHub
src/providers/DeeplProvider.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import ProviderBase from './ProviderBase'
import {SourceLanguageCode, TargetLanguageCode, Translator} from 'deepl-node'

export default class DeeplProvider extends ProviderBase {
  private translator: Translator

  constructor(apiKey: string) {
    super()
    this.translator = new Translator(apiKey)
  }

  async translate(text: string, lang: string): Promise<string[]> {
    const l: string[] = lang.split('-')
    const result = await this.translator.translateText<string>(
      text, l[0] as SourceLanguageCode, l[1] as TargetLanguageCode
    )
    return Promise.resolve([result.text])
  }
}