38

In Flutter app, intl package load only 15 languages. However Dart DateFormat support many more. I do not need translation, but need correct DateFormat. How to load all DateFormat locale?

Shady Mohamed Sherif
  • 15,003
  • 4
  • 45
  • 54
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83

5 Answers5

62

In your highest StatefulWidget add these imports

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

in its State, override initState and add

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

For example

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 mostly uses the 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

The cs language displays this

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • This will not work in Flutter for loading `cz` locale. Invoking `new DateFormat.yMMMMd('cz');` will cause exception in Flutter. – Kyaw Tun Apr 22 '18 at 10:55
  • There isn't a cz locale. Which language are you trying to find? – Richard Heap Apr 22 '18 at 11:08
  • I updated the answer with some examples of valid language codes and variants, and how to find the complete list. Let me know if there's a language missing. – Richard Heap Apr 22 '18 at 16:32
  • 1
    I will test and let you known. By default Flutter package [only 15 languages](https://flutter.io/tutorials/internationalization/#setting-up). The one I want to use in not in that list. – Kyaw Tun Apr 28 '18 at 13:39
  • That's a country code, not a language code. I believe the language code for the Czech language is cs. – Richard Heap Apr 29 '18 at 00:24
  • 1
    `initializeDateFormatting` will return a future that is complete once the locale data is available. So you should wait for it to complete before date formatting. [see DateFormat class](https://docs.flutter.io/flutter/intl/DateFormat-class.html) – Touré Holder Feb 24 '19 at 11:45
  • 3
    Flutter 2.5, I'm getting an `UnsupportedError` "Unsupported operation: Cannot modify unmodifiable map" when trying to use `initializeDateFormatting()`. Anyone else seeing this? – Dave Martorana Aug 23 '22 at 14:42
12

The list of all available locales in Flutter can be take from top-level property availableLocalesForDateFormatting of intl package.

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"
];
BambinoUA
  • 6,126
  • 5
  • 35
  • 51
11

I solved it setting the Intl.sytemLocale before using any DateFormat:

Intl.systemLocale = await findSystemLocale();

I didn't had to call initializeDateFormatting() and I'm using intl: ^0.17.0.

bicicleteroNerd
  • 361
  • 3
  • 10
9

just add the language code like this (in my case 'ar' for Arabic)

  DateFormat('MMMM', 'ar')  .format(DateTime.now());
Omar Essam El-Din
  • 1,415
  • 14
  • 17
  • 1
    Thanks for that. In case someone needs to find out the current locale: DateFormat('EEEE', Localizations.localeOf(context).languageCode); – Schotti100 Apr 27 '23 at 19:00
4

Quoting @kapace comment:

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.

For example:

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
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440