【问题标题】:I cannot get my api call from google-translate to work我无法从 google-translate 获取我的 api 调用来工作
【发布时间】:2020-11-23 13:31:12
【问题描述】:

我无法弄清楚如何使用我的 google-translate API 显示我正在翻译的单词......我想显示来自函数 newText 的翻译后的单词,以显示在我调用该函数的 Text 标签中......(翻译它的词也来自 TextInput '要翻译的词'

const Main = ({ navigation }) => {
  let apiKey = "AIzaasdfasdfasdfasdfasdfsdc";
  let googleTranslate = require("google-translate")(apiKey);

  const [text, setText] = useState("");

  const newText = () => {
    googleTranslate.translate(text, "es", function (err, translation) {
      return translation.translatedText;
    });
  };

  const onChangeText = (text) => setText(text);
  return (
    <View style={styles.screen}>
      <ImageBackground
        source={require("./assets/book.png")}
        style={styles.backgroundImage}
      >
        <View style={styles.innerText}>
          <Text style={{ fontSize: 20 }}>Welcome back Elisa, </Text>
          <Text>let's practice that pronunciation...</Text>
          <TextInput
            placeholder="Word to translate"
            style={styles.input}
            onChangeText={onChangeText}
          />
        </View>

        <Text style={styles.output}>{newText()}</Text>

        <View style={styles.button}>
          <Button
            title="START"
            onPress={() => navigation.navigate("BACK_HOME")}
          />
        </View>
      </ImageBackground>
    </View>
  );
};

【问题讨论】:

    标签: javascript reactjs react-native api google-translate


    【解决方案1】:

    我已将 newText() 转换为异步函数。

    您可以使用 onClick 事件调用 newText(),也可以使用 onChange,因为我在下面修改了您的代码。

    只要您的输入发生变化,就会调用 Google 翻译(这不是一个好主意,因为这意味着如果您输入 100 个字符,它就会翻译 100 次。

    我建议你添加一个这样的按钮。

    &lt;button onClick={() =&gt; newText(text)}&gt;Translate Me!&lt;/button&gt;

    const Main = ({ navigation }) => {
      let apiKey = "AIzaasdfasdfasdfasdfasdfsdc";
      let googleTranslate = require("google-translate")(apiKey);
    
      const [text, setText] = useState("");
      const [ translated, setTranslated ] = useState('');
    
      const newText = async (toBeTranslated) => {
        await googleTranslate.translate(toBeTranslated, "es", function (err, translation) {
          setTranslated(translation.translatedText)
        });
      };
    
      const onChangeText = (text) => {
         setText(text);
         //handle translation when text change.
         newText(text);
      }
      return (
        <View style={styles.screen}>
          <ImageBackground
            source={require("./assets/book.png")}
            style={styles.backgroundImage}
          >
            <View style={styles.innerText}>
              <Text style={{ fontSize: 20 }}>Welcome back Elisa, </Text>
              <Text>let's practice that pronunciation...</Text>
              <TextInput
                placeholder="Word to translate"
                style={styles.input}
                onChangeText={onChangeText}
              />
            </View>
    
            <Text style={styles.output}>{translated}</Text>
    
            <View style={styles.button}>
              <Button
                title="START"
                onPress={() => navigation.navigate("BACK_HOME")}
              />
            </View>
          </ImageBackground>
        </View>
      );
    };
    

    【讨论】:

    • 谢谢!它确实有效!但仅在浏览器上..它不能在模拟器上工作..给我一个与加密相关的错误......
    • 创建一个单独的问题,如果它不直接相关,我们会尝试从那里提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2022-01-15
    • 2014-07-28
    相关资源
    最近更新 更多