首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Flutter和Dart进行翻译

使用Flutter和Dart进行翻译
EN

Stack Overflow用户
提问于 2020-05-31 15:09:46
回答 2查看 3.8K关注 0票数 1

我目前正在从事一个涉及翻译的项目,我正在构建一个使用框架Flutter和语言飞镖的应用程序。

问题是,我不知道如何在dart中用我的应用程序翻译文本。

我尝试使用各种平台,比如google translate API,Firebase,Yandex……但是我没有钱,这是个问题。

所以我尝试了这个https://docs.microsoft.com/fr-fr/azure//cognitive-services/translator/reference/v3-0-translate#request-url,但是我得到了这个错误消息{"error":{"code":405000,"message":"The request method is not supported for the requested resource."}}

然后我尝试使用dart包:https://pub.dev/packages/localize_and_translate,它起作用了,但是它是用来翻译已经存在于json文件中的文本,而我需要翻译用户给我的文本。

我还试图获取其中一个谷歌翻译请求的内容,以获得答案,而不必手动转到网站,当我试图分析请求时,我只得到了这个链接:https://fonts.googleapis.com/css?lang=fr&family=Product+Sans%7CRoboto:400,700

一些帮助或想法肯定会非常感谢。

谢谢你所做的一切!

EN

回答 2

Stack Overflow用户

发布于 2020-05-31 16:07:26

你可以使用AWS Translate(一年免费),这是我为我的二手生产应用程序创建的插件。早些时候,我将Android原生和IOS原生亚马逊网络服务官方包与Flutter MethodChannel一起使用,后来我决定将其打包,这样其他像你这样的人就可以从那里获得帮助。

这就是这个包:https://pub.dev/packages/aws_translate

阅读更多关于亚马逊网络服务翻译的信息:https://aws.amazon.com/translate/

下面是一个如何使用它的简单示例:

代码语言:javascript
运行
复制
AwsTranslate awsTranslate = AwsTranslate(
    poolId: poolId, // your pool id here
    region: Regions.AP_NORTHEAST_2); // your region here

// from parameter is default to ``auto``
String textToTranslate = 'If you press on this text, I can translate this text for you.';
String translated = await awsTranslate.translateText(textToTranslate, to: 'es');
if (!mounted) return;
print(textToTranslate);
setState(() => textToTranslate = translated);

我的亚马逊网络服务翻译插件的示例应用程序可以在这里找到:https://github.com/Blasanka/aws_translate_plugin/tree/master/example (你必须有亚马逊网络服务账户才能获得池id和区域)。

如果你只是想尝试谷歌翻译(没有任何登录,但你不能在生产应用程序中使用),试试https://pub.dev/packages/translator

翻译器的工作示例可以在这里找到:https://github.com/gabrielpacheco23/google-translator/blob/master/example/main.dart

票数 1
EN

Stack Overflow用户

发布于 2021-11-08 10:50:33

另一个包,使用谷歌翻译,我们在生产中使用它,效果很好。https://pub.dev/packages/google_cloud_translation

代码语言:javascript
运行
复制
class _MyHomePageState extends State<MyHomePage> {
  late Translation _translation;
  final String _text =
      'Toda persona tiene derecho a la educación. La educación debe ser gratuita, al menos en lo concerniente a la instrucción elemental y fundamental. La instrucción elemental será obligatoria. La instrucción técnica y profesional habrá de ser generalizada; el acceso a los estudios superiores será igual para todos, en función de los méritos respectivos.';
  TranslationModel _translated = TranslationModel(translatedText: '', detectedSourceLanguage: '');

  @override
  void initState() {
    _translation = Translation(
      apiKey: 'YOUR_API_KEY',
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Translate demo'),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                'Initial text',
                style: Theme.of(context).textTheme.headline3,
              ),
              Text(_text),
              SizedBox(height: 30),
              Text('Translated text', style: Theme.of(context).textTheme.headline3),
              Text(_translated.translatedText, style: TextStyle(color: Colors.blueAccent)),
              Text('Detected language - ${_translated.detectedSourceLanguage}',
                  style: TextStyle(color: Colors.red)),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          _translated = await _translation.translate(text: _text, to: 'en');
          setState(() {});
        },
        tooltip: 'Translate',
        child: Icon(Icons.language),
      ),
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62112787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档