反应原生谷歌翻译 [英] react-native google translater

查看:82
本文介绍了反应原生谷歌翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have developed an android mobile app using react-native. My native language for the app is English but I want to make it available in Mandarin Chinese also. So my question is how can I convert my static and fetched data into Mandarin Chinese. please share a solution.

推荐答案

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

Yes, you can do it. For changing the Application language to multi-language app,i.e., also include Mandarin Chinese.

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

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

  1. 对于动态内容/文本,使用包 google-translate-api

链接:https://www.npmjs.com/package/google-translate-api

I18n 示例:

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 翻译示例:

Google-translator Example:

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

希望它有帮助..!谢谢

Hope it helps..! Thank you

~普拉兹

这篇关于反应原生谷歌翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆