skip to Main Content

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

4

Answers


  1. Chosen as BEST ANSWER

    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.


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

    Add a line of code to the initState() method

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search