react-native-translator icon indicating copy to clipboard operation
react-native-translator copied to clipboard

Getting [TypeError: undefined is not a function]

Open rajkamal-360 opened this issue 1 year ago • 2 comments

code :

const onTranslate = async () => { try { const _result = await translate('en', 'es', value, { type: 'google', timeout: 10000, }); setResult(_result); console.log(_result); } catch (error) { console.log('translation error', error); } };

setValue(t)} placeholder="type" />

rajkamal-360 avatar Oct 25 '23 06:10 rajkamal-360

same error with me

alitele avatar Dec 09 '23 09:12 alitele

same error with me

U can use this....

all you need is to activate the translation service from firebase console.

import {StyleSheet, Text, View} from 'react-native'; import React from 'react'; import axios from 'axios'; import {IRTH_GOOGLE_API_KEY} from '../storage/storage_keys';

export const GoogleTranslate = async (text, targetLang) => { try { const response = await axios.post( 'https://translation.googleapis.com/language/translate/v2', {}, { params: { q: text, target: targetLang, key: IRTH_GOOGLE_API_KEY, }, }, );

const translatedText = response.data.data.translations[0].translatedText;
return translatedText;

} catch (error) { console.log(error); throw error; // You can choose to handle the error as needed } };

usage :

async function translateText(lang) { try { const translatedText = await GoogleTranslate(overview, lang); setESOverview(translatedText); } catch (error) { console.error('Error:', error); setESOverview(''); } } if (review_language == 'en') { translateText('es'); } else if (review_language == 'es') { translateText('en'); }

rajkamal-360 avatar Feb 08 '24 18:02 rajkamal-360