skip to Main Content

I want to display today’s date in "Marathi" language.But the numbers from date are not displaying in Marathi language.
I have written following code lines to achieve it.
(flutter-3.0.4 stable)

DateTime currentDate = DateTime.now();
String todaysDate = DateFormat('d MMMM, yyyy', "mr_IN").format(currentDate);

Actual output is:

20 डिसेंबर, 2022

But I want to display:

२० डिसेंबर, २०२२

As we can see, numbers from date are not in Marathi language.

I have also wrote the below extension method but that also not resolve that issue:

extension ExtendedString on String {
  String get toMarathi {
    String dateInMarathi = this;
    if (dateInMarathi.contains("0")) {
      dateInMarathi = dateInMarathi.replaceAll("0", "०");
    }
    if (dateInMarathi.contains("1")) {
      dateInMarathi = dateInMarathi.replaceAll("1", "१");
    }
    if (dateInMarathi.contains("2")) {
      dateInMarathi = dateInMarathi.replaceAll("2", "२");
    }
    if (dateInMarathi.contains("3")) {
      dateInMarathi = dateInMarathi.replaceAll("3", "३");
    }
    if (dateInMarathi.contains("4")) {
      dateInMarathi = dateInMarathi.replaceAll("4", "४");
    }
    if (dateInMarathi.contains("5")) {
      dateInMarathi = dateInMarathi.replaceAll("5", "५");
    }
    if (dateInMarathi.contains("6")) {
      dateInMarathi = dateInMarathi.replaceAll("6", "६");
    }
    if (dateInMarathi.contains("7")) {
      dateInMarathi = dateInMarathi.replaceAll("7", "७");
    }
    if (dateInMarathi.contains("8")) {
      dateInMarathi = dateInMarathi.replaceAll("8", "८");
    }
    if (dateInMarathi.contains("9")) {
      dateInMarathi = dateInMarathi.replaceAll("9", "९");
    }
    return dateInMarathi;
  }
}

2

Answers


  1. Try the following code:

    extension ExtendedString on String {
      String toMarathi {
        return this.replaceAll("0", "०").replaceAll("1", "१").replaceAll("2", "२").replaceAll("3", "३").replaceAll("4", "४").replaceAll("5", "५").replaceAll("6", "६").replaceAll("7", "७").replaceAll("8", "८").replaceAll("9", "९");
      }
    }
    

    "${DateTime.now().day}, ${DateTime.now().month}, ${DateTime.now().year}".toMarathi()
    
    Login or Signup to reply.
  2. Try below code refer some links GlobalMaterialLocalizations, initializeDateFormatting:

    import below package for

    import 'package:intl/date_symbol_data_local.dart';
    

    add initializeDateFormatting() inside initState

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

    your date code

    DateTime currentDate = DateTime.now();
    String todaysDate = DateFormat('d MMMM, yyyy', "mr").format(currentDate);
    
    print(todaysDate);
    

    Your Widget:

    Text(todaysDate),
    

    Full Code:

    import 'package:flutter/material.dart';
    import 'package:intl/intl.dart';
    import 'package:intl/date_symbol_data_local.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          debugShowCheckedModeBanner: false,
          title: _title,
          home: MyStatefulWidget(),
        );
      }
    }
    
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({super.key});
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      @override
      void initState() {
        super.initState();
        initializeDateFormatting();
      }
    
      DateTime currentDate = DateTime.now();
    
      @override
      Widget build(BuildContext context) {
        String todaysDate = DateFormat('d MMMM, yyyy', "mr").format(currentDate);
        print(todaysDate);
        return Scaffold(
          body: Center(
            child: Text(todaysDate),
          ),
        );
      }
    }
    

    Result-> image

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