dart 在Flutter中,日期中的数字未显示为马拉地语

bn31dyow  于 2022-12-20  发布在  Flutter
关注(0)|答案(2)|浏览(176)

我想用"马拉地语"显示今天的日期。但是日期中的数字不能用马拉地语显示。我写了下面的代码行来实现它。(flutter-3.0.4稳定)

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

实际输出为:

20 डिसेंबर, 2022

但我想显示:

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

正如我们所看到的,日期中的数字不是马拉地语。
我也写了下面的扩展方法,但也没有解决这个问题:

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;
  }
}
xuo3flqw

xuo3flqw1#

请尝试以下代码:

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()
eoigrqb6

eoigrqb62#

尝试下面的代码参考一些链接GlobalMaterialLocalizationsinitializeDateFormatting
导入以下程序包

import 'package:intl/date_symbol_data_local.dart';

initState内添加initializeDateFormatting()

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

您的日期代码

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

print(todaysDate);

您的小工具:

Text(todaysDate),

完整代码:

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),
      ),
    );
  }
}

结果-〉

相关问题