在 Flutter 应用中,intl 包仅加载15 种语言。然而 Dart DateFormat支持更多。我不需要翻译,但需要正确DateFormat
的。如何加载所有DateFormat
语言环境?
问问题
48072 次
5 回答
44
在您的最高 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 个字母的代码。要查找完整列表,只需DateSymbols
在文件date_symbol_data_local.dart中搜索
于 2018-04-19T02:00:26.493 回答
7
Flutter 中所有可用语言环境的列表可以从包的顶级属性availableLocalesForDateFormatting中获取intl
。
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"
];
于 2019-05-16T20:41:38.943 回答
6
导入它
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);
于 2020-10-02T09:09:48.070 回答
1
引用@kapace评论:
您应该使用语言标签以确保您考虑区域设置的国家代码。就像在加拿大,语言与 相同
en
,en-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
于 2021-09-02T19:52:16.303 回答
1
我在使用任何 DateFormat 之前解决了它设置 Intl.sytemLocale 的问题:
Intl.systemLocale = await findSystemLocale();
我不必打电话initializeDateFormatting()
,我正在使用intl: ^0.17.0
.
于 2021-11-08T20:54:22.533 回答