reactjs - react native 谷歌翻译

标签 reactjs reactive-programming react-native

我使用react-native开发了一个Android移动应用程序。我的应用程序的母语是英语,但我也想提供普通话版本。所以我的问题是如何将静态数据和获取的数据转换为普通话。请分享解决方案。

最佳答案

是的,你可以做到。 用于将应用程序语言更改为多语言应用程序,即还包括普通话。

  1. 对于静态内容/文本,请使用包react-native-i18n

链接:https://www.npmjs.com/package/react-native-i18n

  • 对于动态内容/文本,请使用 google-translate-api
  • 链接:https://www.npmjs.com/package/google-translate-api

    国际化示例:

    import I18n from 'react-native-i18n';
    
    class Demo extends React.Component {
      render() {
        return <Text>{I18n.t('greeting')}</Text>;
      }
    }
    
    // Enable fallbacks if you want `en-US` and `en-GB` to fallback to `en`
    I18n.fallbacks = true;
    
    I18n.translations = {
      en: {
        greeting: 'Hi!',
      },
      fr: {
        greeting: 'Bonjour!',
      },
    };
    

    Google 翻译示例:

    From automatic language detection to English:
    
    const translate = require('google-translate-api');
    
    translate('Ik spreek Engels', {to: 'en'}).then(res => {
        console.log(res.text);
        //=> I speak English
        console.log(res.from.language.iso);
        //=> nl
    }).catch(err => {
        console.error(err);
    });
    
    From English to Dutch with a typo:
    
    translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
        console.log(res.text);
        //=> Ik spreek Nederlands!
        console.log(res.from.text.autoCorrected);
        //=> true
        console.log(res.from.text.value);
        //=> I [speak] Dutch!
        console.log(res.from.text.didYouMean);
        //=> false
    }).catch(err => {
        console.error(err);
    });
    

    希望它有帮助..! 谢谢

    ~普拉兹

    关于reactjs - react native 谷歌翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53701231/

    相关文章:

    R Shiny 的应用程序,输入取决于更新的数据

    android - 如何设置 fable-elmish-react native 应用程序?

    react-native - React Native onEndReached Flatlist

    javascript - 完全被这个 reactjs 逻辑难住了,变量不更新组件的状态

    javascript - "Cannot import useState because there is no useState export in react"(使用 React Hooks 的流程)

    reactjs - React-native,清除 webview cookie?

    java - 有时间限制的 RxJava zip

    reactjs - React Js 时间选择器

    java - 如何从另一种方法动态地向 react 器热通量添加元素?

    javascript - React Native 中的绝对定位