-1

I want to change the language for the weekdays in intl: enter image description here

to German. In my code, I do this:

final weekdayFormatter = DateFormat('E');
final date = mostRecentWeekday(_currentDate, 1).add(Duration(days: i)); "<-- Function to get current monday"
...
 Text(weekdayFormatter.format(date)),

So I want Mo Di Mi Do which stands for Montag Dienstag Mittwoch [...]

I tried this function: initializeDateFormatting(); but I don't understand what parameters this function needs, especially the path parameter... all posts I were able to found are outdated

Fabian M
  • 154
  • 14

4 Answers4

0

Hello you can try it initializeDateFormatting('de_DE', null);

Add a line of code to the initState() method

Zozo
  • 23
  • 1
  • 7
  • dont work, using Intl 0.18 there can't be a Null value. – Fabian M Feb 02 '23 at 10:18
  • The argument type 'Null' can't be assigned to the parameter type 'String' on @override `void initState() { super.initState(); initializeDateFormatting('de_DE', null); }` – Fabian M Feb 02 '23 at 10:20
  • https://stackoverflow.com/questions/49807687/how-to-load-all-dart-dateformat-locale-in-flutter try to watch this post, I think you will find a solution – Zozo Feb 02 '23 at 10:27
  • What is the problem with switching to 0.17 or do you fundamentally want to use the latest version ? – Zozo Feb 02 '23 at 10:29
  • Yeah, beacause switching to 0.17 i have to run my flutter project without sounded nullsafty – Fabian M Feb 02 '23 at 10:31
  • I'm happily building with full null safety with intl 0.17. It is labelled as a null safe version. – GrahamD Feb 02 '23 at 10:35
  • have you tried replacing null with ' ' – Zozo Feb 02 '23 at 10:38
  • Now I don't understand the world. Even I use intl 17 it still says string cant be null – Fabian M Feb 02 '23 at 10:38
  • initializeDateFormatting('de_DE', ' '); – Zozo Feb 02 '23 at 10:38
  • you with '' it says: `Locale de has not been initialized. Call initializeDateFormatting(de, ) first` – Fabian M Feb 02 '23 at 10:39
  • This package has multiple methods named `initializeDateFormatting`. If you import the one in `date_symbol_data_file.dart` then you'd hit this error. The example in the README uses `date_symbol_data_file.dart` which has an optional second argument so that looks like it could be correct. It does look like the DateFormat documentation passes a null for all 3 examples, and likely at least 2 of those will need updates – Zozo Feb 02 '23 at 10:45
  • What package shall I import? Yeah mine looks like this: `Future initializeDateFormatting(String locale, String filePath) {` – Fabian M Feb 02 '23 at 10:48
  • `import 'package:intl/date_symbol_data_local.dart';` – Zozo Feb 02 '23 at 10:52
  • You can see more details here:[link] https://github.com/dart-lang/intl/blob/master/README.md – Zozo Feb 02 '23 at 10:56
0

First in your route MaterialApp Widget add this parameter :

      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],

then use this script as language changer anywhere you need:

    initializeDateFormatting('de_DE');
    final weekdayFormatter = DateFormat('E', 'de_DE');
    final date = DateTime.now();
    print(weekdayFormatter.format(date)); // it will print Do
  • okay intersting since I added these things inside material I get a different error: I also have to use: `initializeDateFormatting('de_DE', '')` because otherwise I cant compile. Anyway: new error: `Class '_Map' has no instance method 'initLocale'. Receiver: _Map len:94 Tried calling: initLocale("de_DE")` – Fabian M Feb 02 '23 at 10:45
  • What Intl version are you using and how does your initializeDateFormatting looks inside the library ? Mine: `Future initializeDateFormatting(String locale, String filePath) {` – Fabian M Feb 02 '23 at 10:47
  • flutter_localizations from sdk which depends on intl 0.17.0, intl 0.17.0 is required. this means that if you want to remain on intl 0.18 you cannot use localizations. you need to do it yourself mannually – Nastaran Mohammadi Feb 02 '23 at 11:08
0

I solved it by do:

  final weekdayFormatter = DateFormat.E('de_DE');

instead of:

final weekdayFormatter = DateFormat('E');

also:

      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],

inside Matrerial app and

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

inside pub file

and using intl 17.

Fabian M
  • 154
  • 14
0

Make sure you use the right import of initializeDateFormatting. The easiest is to use import 'package:intl/date_symbol_data_local.dart', for web and for custom file loading other variants exist in date_symbol_data_http_request.dart and date_symbol_data_file.dart.

mosuem
  • 1
  • 1