Home > OS >  Flutter Intl (0.18!): Changing language for weekday
Flutter Intl (0.18!): Changing language for weekday

Time:02-02

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

CodePudding user response:

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

Add a line of code to the initState() method

CodePudding user response:

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

CodePudding user response:

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.

  • Related