javascript - Translation failed TypeError: google_translate_api_x__WEBPACK_IMPORTED_MODULE_0__ is not a function - Stack Overflo

admin2025-03-20  6

I attempted to run this code for my translation web app created from react:

import translate from 'google-translate-api-x';

export async function translateText(text, sourceLang, targetLang) {
  try {
    const response = await translate(`${text}`, {
      from: `${sourceLang}`, 
      to: `${targetLang}`, 
      autoCorrect: true
    });

    // Extract the translated text from the response
    return response.text;
  } catch (error) {
    console.error("Error during translation:", error);
    throw error;
  }
}

It keeps giving me the error: Translation failed TypeError: google_translate_api_x__WEBPACK_IMPORTED_MODULE_0__ is not a function

I read the API README and copied the way that they have used the function and didn't get the same result. I think this problem may lie in export async function I am a JavaScript/React beginner so I do not have much knowledge on how to solve the issue.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742421507a212965.html

最新回复(0)