反应本机谷歌翻译

Raj*_*at 2 reactive-programming reactjs react-native

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

小智 5

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

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

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

  1. 对于动态内容/文本,请使用该包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!',
  },
};
Run Code Online (Sandbox Code Playgroud)

谷歌翻译示例:

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);
});
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..!谢谢

〜普拉兹