【问题标题】:I have translation API, how can I localize my flutter app using API我有翻译 API,如何使用 API 本地化我的颤振应用程序
【发布时间】:2021-10-09 12:29:34
【问题描述】:

// 这是我的 api 响应。

{ “状态码”:200, “状态”:“成功”, "message": "翻译数据", “结果”: [ { “你好你好” } ] }

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    不建议使用 API 来本地化您应用的离线内容,因为有时用户必须等待太多 API 才能响应(也有可能永远不会响应)。 最好的方法是制作字典,因为您已经有离线文本并且很容易翻译它们。那么您可以完全离线更改文本。

    编辑:

    如果您别无选择,以下代码可能对您有所帮助:

    import 'package:flutter/material.dart';
    import 'dart:ui' as ui show TextHeightBehavior;
    
    class LocalizedText extends StatefulWidget {
      final String? data;
    
      final TextStyle? style;
    
      final StrutStyle? strutStyle;
    
      final TextAlign? textAlign;
    
      final TextDirection? textDirection;
    
      final Locale? locale;
    
      final bool? softWrap;
    
      final TextOverflow? overflow;
    
      final double? textScaleFactor;
    
      final int? maxLines;
    
      final String? semanticsLabel;
    
      final TextWidthBasis? textWidthBasis;
    
      final ui.TextHeightBehavior? textHeightBehavior;
    
      const LocalizedText(this.data,
          {Key? key,
          this.style,
          this.strutStyle,
          this.textAlign,
          this.textDirection,
          this.locale,
          this.softWrap,
          this.overflow,
          this.textScaleFactor,
          this.maxLines,
          this.semanticsLabel,
          this.textWidthBasis,
          this.textHeightBehavior})
          : assert(
              data != null,
              'A non-null String must be provided to a Text widget.',
            ),
            super(key: key);
      @override
      _LocalizedTextState createState() => _LocalizedTextState();
    }
    
    class _LocalizedTextState extends State<LocalizedText> {
      bool _isLoading = true;
      String? _translatedText;
      @override
      void initState() {
        super.initState();
        callApi();
      }
    
      @override
      Widget build(BuildContext context) {
        return Text(
          _isLoading
              ? "Loding..." //Your Loading Text here, You can also use 'widget.data!' to show original text while waiting for Api result
              : _translatedText ??
                  widget
                      .data!, //if the Api doesn't respond or fail, the original text will apear
          key: widget.key,
          locale: widget.locale,
          maxLines: widget.maxLines,
          overflow: widget.overflow,
          semanticsLabel: widget.semanticsLabel,
          softWrap: widget.softWrap,
          strutStyle: widget.strutStyle,
          style: widget.style,
          textAlign: widget.textAlign,
          textDirection: widget.textDirection,
          textHeightBehavior: widget.textHeightBehavior,
          textScaleFactor: widget.textScaleFactor,
          textWidthBasis: widget.textWidthBasis,
        );
      }
    
      void callApi() async {
        var response;
        //Call your API here
        _isLoading = false;
        if (response["statusCode"] == 200) {
          _translatedText = response["result"]...;  //You should set your translated text from Api to _translatedText
        }
        setState(() {});
      }
    }
    

    像这样使用它:LocalizedText("Hello")

    如果您想进一步优化它,您可以将翻译的单词保存在本地并在调用 Api 之前检查它们。

    【讨论】:

    • 这是一个基于生产的应用程序,所以我必须使用 API 进行翻译。
    • 我编辑了答案,检查一下。
    猜你喜欢
    • 2019-02-10
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2020-06-20
    • 2021-08-07
    相关资源
    最近更新 更多