如何在flutter中加载所有dart DateFormat区域设置?

7cwmlq89  于 2022-12-16  发布在  Flutter
关注(0)|答案(5)|浏览(178)

在Flutter应用程序中,intl包只能加载15种语言。但是Dart DateFormat支持更多语言。我不需要翻译,但需要正确的DateFormat。如何加载所有DateFormat区域设置?

vpfxa7rd

vpfxa7rd1#

在最高级别的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字母代码,如果适用,还使用国家/地区变量。例如:ja, en_US, en_GB, zh_HK, zh_CN。有时它使用3个字母的代码。要查找完整列表,只需在文件date_symbol_data_local.dart中搜索DateSymbols

j9per5c4

j9per5c42#

Flutter中所有可用语言环境的列表可以从intl包的顶层属性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"
];
uoifb46i

uoifb46i3#

我在使用任何DateFormat之前设置Intl.systemLocale解决了这个问题:

Intl.systemLocale = await findSystemLocale();

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

qacovj5a

qacovj5a4#

引用@kapace评论:
您应该使用语言标记,以确保您考虑到区域设置的国家代码。例如在加拿大,语言是相同的enen-US,但他们使用en-CA区域设置的日期格式略有不同。
例如:

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
fkvaft9z

fkvaft9z5#

只需像这样添加语言代码(在我的例子中,“ar”代表阿拉伯语)

DateFormat('MMMM', 'ar')  .format(DateTime.now());

字符串

相关问题