public async translate()

in client/src/services/api/translation.ts [53:85]


  public async translate(englishWords: string[], primaryLanguage: string, targetLanguage: string,
                         maxTranslations: number = 0): Promise<WordTranslation[]> {
    const lowercaseWords = englishWords.map((w) => w.toLowerCase());
    const newRequest: TranslateRequest = { words: lowercaseWords, primaryLanguage, targetLanguage };
    if (this.lastRequest && this.lastResponse && APITranslationService.requestsAreEqual(this.lastRequest, newRequest)) {
      // use cached results
      return Promise.resolve(this.lastResponse);
    }
    const response = await this.http.post<TranslationResponse[]>(this.config.endpointURL, {
      english_words: lowercaseWords,
      primary_language: primaryLanguage,
      target_language: targetLanguage
    }).toPromise();
    let translations = response.map(tr => ({
      english: tr.english_word,
      original: tr.primary_word,
      translation: tr.translation,
      transliteration: tr.transliteration,
      soundURL: APITranslationService.formatSoundURL(tr.sound_link)
    }));
    // add any missing translations
    lowercaseWords.forEach((w) => {
      if (!translations.find((tr) => tr.english === w)) {
        translations.push({ original: '', english: w, translation: '', transliteration: '', soundURL: '' });
      }
    });
    // filter out empty translations
    translations = translations.filter(tr => tr.english);
    // cache results
    this.lastRequest = newRequest;
    this.lastResponse = translations;
    return translations;
  }