【问题标题】:Translation with Flutter and Dart使用 Flutter 和 Dart 进行翻译
【发布时间】:2020-09-18 14:33:42
【问题描述】:

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

问题是我不知道如何在 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

我们将非常感谢一些帮助或想法。

感谢一切!

【问题讨论】:

    标签: api flutter dart translation translate


    【解决方案1】:

    您可以使用 AWS Translate(免费一年),我为我的生产使用的应用程序创建了一个插件。之前我用 Flutter MethodChannel 使用 Android 原生和 IOS 原生 AWS 官方包,后来我决定把它打包,以便像你这样的人可以得到帮助。

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

    阅读有关 AWS 翻译的更多信息:https://aws.amazon.com/translate/

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

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

    可以在此处找到我的 AWS 翻译插件的示例应用程序:https://github.com/Blasanka/aws_translate_plugin/tree/master/example(您必须拥有 AWS 帐户才能获取池 ID 和区域)。

    如果您只是想尝试谷歌翻译(无需登录但无法在生产应用中使用),请尝试https://pub.dev/packages/translator

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

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2018-09-11
      • 2014-07-14
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2018-04-30
      • 2015-07-22
      • 2021-06-04
      相关资源
      最近更新 更多