KoreanThinker / react-native-translator

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

Google Translator type not working #42

Open Dsalvat596 opened 1 month ago

Dsalvat596 commented 1 month ago

I am using the useTranslatorHook in a custom text component.

When i use the types 'papago' or 'kakao'

It works great. When I try the 'google' type, it just returns _result as 'Enter a URL'.

I really need the expanded language options of Google, can you look into this?

Thanks.

Here is my implementation:

const TranslatableText: React.FC<TranslatableTextProps> = ({
  children,
  ...props
}) => {
  const {translate} = useTranslator();
  const [loading, setLoading] = useState(false);
  const {selectedLanguage} = useContext(AppContext);

  const [result, setResult] = useState('');

  const onTranslate = useCallback(async () => {
    try {
      setLoading(true);
      const _result = await translate('en', selectedLanguage, children, {
        type: TRANSLATOR_TYPES[2],
        timeout: 10000,
      });
      setResult(_result);
    } catch (error) {
      console.log('ERRROR', error);
    } finally {
      setLoading(false);
    }
  }, [translate, selectedLanguage, children]);

  useEffect(() => {
    if (selectedLanguage === 'en') {
      setResult(children);
    } else {
      onTranslate();
    }
  }, [children, onTranslate, selectedLanguage]);

  return <Text {...props}>{loading ? children : result}</Text>;