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

TypeError: Cannot read property 'type' of undefined

Open AhammedMishal1 opened this issue 1 year ago • 7 comments

got error for this function const onTranslate = async () => { const _result = await translate('en', 'fr', value); setResult(_result); };
WARN Possible Unhandled Promise Rejection (id: 12): TypeError: Cannot read property 'type' of undefined TypeError: Cannot read property 'type' of undefined

I'm using normal react native jsx, also the real-time conversion is crashing the app.

AhammedMishal1 avatar Mar 20 '23 09:03 AhammedMishal1

Same issue. on both Android and iOS, RN 0.70.6

MariusCatanoiu avatar Mar 20 '23 10:03 MariusCatanoiu

better use the google translation API directly its working here the example code. `` const [translatedText, setTranslatedText] = useState(''); const handleTranslate = async () => { const text = 'how are you doing today 77'; const targetLanguage = 'hi'; const translatedText = await translateText(text, targetLanguage); setTranslatedText(translatedText); };

const translateText = async (text, targetLanguage) => { const apiKey = 'AIzaSyCVL6U956LvuHCeMEaXKVvWssRY7DJmvZU'; const url = https://translation.googleapis.com/language/translate/v2?key=${apiKey}; const data = { q: text, target: targetLanguage, }; const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); const json = await response.json(); return json.data.translations[0].translatedText; }; <Text>Original text: Hello, world!</Text> <Button title="Translate" onPress={handleTranslate} /> <Text>Translated text: {translatedText}</Text> ``

ahammedmishal avatar Mar 20 '23 11:03 ahammedmishal

the call the function onPress={handleTranslate} inside a button

ahammedmishal avatar Mar 20 '23 11:03 ahammedmishal

@AhammedMishal1 @MariusCatanoiu how about this?

 onTranslate = async () => {
const _result = await translate('en', 'fr', value, { type: 'google' });
setResult(_result);
};

KoreanThinker avatar Mar 26 '23 14:03 KoreanThinker

@AhammedMishal1 @MariusCatanoiu how about this?

 onTranslate = async () => {
const _result = await translate('en', 'fr', value, { type: 'google' });
setResult(_result);
};

did need google API for this to work ?

AhammedMishal1 avatar May 26 '23 10:05 AhammedMishal1

@AhammedMishal1 @MariusCatanoiu how about this?

onTranslate = async () => {

const _result = await translate('en', 'fr', value, { type: 'google' });

setResult(_result);

};

did need google API for this to work ?

No. It's working by crawling.

KoreanThinker avatar May 26 '23 10:05 KoreanThinker

@AhammedMishal1 @MariusCatanoiu how about this?

onTranslate = async () => {

const _result = await translate('en', 'fr', value, { type: 'google' });

setResult(_result);

};

did need google API for this to work ?

No. It's working by crawling.

As a provider of the npm library, it's important to consider the possibility of the web page you're scraping for translations changing its tags in the future. In such cases, will the library still function correctly, or will the code need to be updated to accommodate the changes in the web page's structure?

AhammedMishal1 avatar May 31 '23 17:05 AhammedMishal1