KoreanThinker / react-native-translator

🌐 Unlimited free google translate component & hook (Unofficial)
https://www.npmjs.com/package/react-native-translator
MIT License
81 stars 9 forks source link

TypeError: Cannot read property 'type' of undefined #17

Open AhammedMishal1 opened 1 year ago

AhammedMishal1 commented 1 year ago

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.

MariusCatanoiu commented 1 year ago

Same issue. on both Android and iOS, RN 0.70.6

ahammedmishal commented 1 year ago

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; };

Original text: Hello, world!
ahammedmishal commented 1 year ago

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

KoreanThinker commented 1 year ago

@AhammedMishal1 @MariusCatanoiu how about this?

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

@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 ?

KoreanThinker commented 1 year ago

@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.

AhammedMishal1 commented 1 year ago

@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?