Kya*_*Tun 6 internationalization dart flutter
在Flutter应用程序中,intl包只加载15种语言.但是Dart DateFormat支持更多.我不需要翻译,但需要正确DateFormat.如何加载所有DateFormat语言环境?
Ric*_*eap 15
在最高的StatefulWidget中添加这些导入
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
Run Code Online (Sandbox Code Playgroud)
在其状态中,覆盖initState并添加
@override
void initState() {
super.initState();
initializeDateFormatting();
}
Run Code Online (Sandbox Code Playgroud)
例如
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),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
Dart主要使用ISO 631 2字母代码,如果适用,可使用国家/地区变体.例如:ja, en_US, en_GB, zh_HK, zh_CN.偶尔它使用3个字母的代码.要查找完整列表,只需DateSymbols在文件date_symbol_data_local.dart中搜索
bic*_*erd 11
我在使用任何 DateFormat 之前解决了设置 Intl.sytemLocale 的问题:
Intl.systemLocale = await findSystemLocale();
Run Code Online (Sandbox Code Playgroud)
我不必打电话initializeDateFormatting(),我正在使用intl: ^0.17.0.
Oma*_*sam 11
只需添加这样的语言代码(在我的例子中,“ar”代表阿拉伯语)
DateFormat('MMMM', 'ar') .format(DateTime.now());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8278 次 |
| 最近记录: |