如何在 flutter 中加载所有 dart DateFormat 语言环境?

How to load all dart DateFormat locale in flutter?

在 Flutter 应用程序中,仅加载国际包 15 languages. However Dart DateFormat 支持更多。我不需要翻译,但需要正确DateFormat。如何加载所有 DateFormat 语言环境?

在你最高的 StatefulWidget 中添加这些导入

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

在其状态中,覆盖 initState 并添加

  @override
  void initState() {
    super.initState();
    initializeDateFormatting();
  }

例如

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Intl Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Intl Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateFormat dateFormat;
  DateFormat timeFormat;

  @override
  void initState() {
    super.initState();
    initializeDateFormatting();
    dateFormat = new DateFormat.yMMMMd('cs');
    timeFormat = new DateFormat.Hms('cs');
  }

  void _refresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var dateTime = new DateTime.now();
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(dateFormat.format(dateTime)),
            new Text(timeFormat.format(dateTime)),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _refresh,
        tooltip: 'Refresh',
        child: new Icon(Icons.refresh),
      ),
    );
  }
}

Dart 主要使用 ISO 631 2 letter codes, with country variants if applicable. For example: ja, en_US, en_GB, zh_HK, zh_CN. Occasionally it uses a 3 letter code. To find the full list just search for DateSymbols in the file date_symbol_data_local.dart

Flutter 中所有可用语言环境的列表可以从 intl 包的 top-level 属性 availableLocalesForDateFormatting 中获取。

final availableLocalesForDateFormatting = const [
  "en_ISO",
  "af",
  "am",
  "ar",
  "ar_DZ",
  "ar_EG",
  "az",
  "be",
  "bg",
  "bn",
  "br",
  "bs",
  "ca",
  "chr",
  "cs",
  "cy",
  "da",
  "de",
  "de_AT",
  "de_CH",
  "el",
  "en",
  "en_AU",
  "en_CA",
  "en_GB",
  "en_IE",
  "en_IN",
  "en_MY",
  "en_SG",
  "en_US",
  "en_ZA",
  "es",
  "es_419",
  "es_ES",
  "es_MX",
  "es_US",
  "et",
  "eu",
  "fa",
  "fi",
  "fil",
  "fr",
  "fr_CA",
  "fr_CH",
  "ga",
  "gl",
  "gsw",
  "gu",
  "haw",
  "he",
  "hi",
  "hr",
  "hu",
  "hy",
  "id",
  "in",
  "is",
  "it",
  "it_CH",
  "iw",
  "ja",
  "ka",
  "kk",
  "km",
  "kn",
  "ko",
  "ky",
  "ln",
  "lo",
  "lt",
  "lv",
  "mk",
  "ml",
  "mn",
  "mr",
  "ms",
  "mt",
  "my",
  "nb",
  "ne",
  "nl",
  "no",
  "no_NO",
  "or",
  "pa",
  "pl",
  "ps",
  "pt",
  "pt_BR",
  "pt_PT",
  "ro",
  "ru",
  "si",
  "sk",
  "sl",
  "sq",
  "sr",
  "sr_Latn",
  "sv",
  "sw",
  "ta",
  "te",
  "th",
  "tl",
  "tr",
  "uk",
  "ur",
  "uz",
  "vi",
  "zh",
  "zh_CN",
  "zh_HK",
  "zh_TW",
  "zu"
];

导入它

import 'package:intl/intl.dart';  //for date format
import 'package:intl/date_symbol_data_local.dart';  //for date locale

并在初始化 DateFormat 时使用当前语言代码

String languageCode = Localizations.localeOf(context).languageCode;
String dateTime = DateFormat( "dd.MM.yy HH:mm", languageCode).format(this.date);

引用 @kapace 评论:

You should use language tag in order to be sure that you consider locale's country code. Like in Canada where language is same en as en-US, but they format dates slightly different using en-CA locale.

例如:

var tag = Localizations.maybeLocaleOf(context)?.toLanguageTag();

var date1 = DateFormat.yMMMd(tag).format(DateTime.now()); // Dec 31, 2000
var date2 = DateFormat('MM/dd, hh:mm a', tag).format(DateTime.now()); // 12/31, 11:00 PM

我解决了它在使用任何 DateFormat 之前设置 Intl.sytemLocale:

Intl.systemLocale = await findSystemLocale();

我不必调用 initializeDateFormatting(),我正在使用 intl: ^0.17.0