当前位置: 首页 > news >正文

flutter 自定义本地化-GlobalMaterialLocalizations(重写本地化日期转换)

1. 创建自定义 GlobalMaterialLocalizations

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:kittlenapp/utils/base/date_time_util.dart';///[auth] kittlen
///[createTime] 2024-05-31 11:40
///[description]class MyMaterialLocalizationZh extends MaterialLocalizationZh {MyMaterialLocalizationZh({super.localeName = 'my_local', ///自定义本地化名required super.fullYearFormat,required super.compactDateFormat,required super.shortDateFormat,required super.mediumDateFormat,required super.longDateFormat,required super.yearMonthFormat,required super.shortMonthDayFormat,required super.decimalFormat,required super.twoDigitZeroPaddedFormat});///以下方法为对zh本地化的重写String formatCompactDate(DateTime date) {// Assumes yyyy-mm-ddreturn DateTimeUtil.formatDate(date);}DateTime? parseCompactDate(String? inputString) {if (inputString == null) {return null;}// Assumes yyyy-mm-ddfinal List<String> inputParts = inputString.split('-');if (inputParts.length != 3) {return null;}final int? year = int.tryParse(inputParts[0], radix: 10);if (year == null || year < 1) {return null;}final int? month = int.tryParse(inputParts[1], radix: 10);if (month == null || month < 1 || month > 12) {return null;}final int? day = int.tryParse(inputParts[2], radix: 10);if (day == null || day < 1 || day > _getDaysInMonth(year, month)) {return null;}return DateTime(year, month, day);}int _getDaysInMonth(int year, int month) {if (month == DateTime.february) {final bool isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);if (isLeapYear) {return 29;}return 28;}const List<int> daysInMonth = <int>[31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];return daysInMonth[month - 1];}
}

2.创建对应的LocalizationsDelegate管理本地配置

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;import 'package:flutter_localizations/src/l10n/generated_material_localizations.dart';
import 'package:flutter_localizations/src/utils/date_localizations.dart' as util;
import 'package:kittlenapp/localzations/my_material_localization_zh.dart';///[auth] kittlen
///[createTime] 2024-05-31 12:13
///[description]class MyLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {static const LocalizationsDelegate<MaterialLocalizations> delegate = MyLocalizationsDelegate();const MyLocalizationsDelegate();///自定义本地化名,GlobalMaterialLocalizations中定义的的localeNamebool isSupported(Locale locale) => locale.languageCode == 'my_local';static Future<MaterialLocalizations>? _loadedTranslations;Future<MaterialLocalizations> load(Locale locale) {assert(isSupported(locale));if (_loadedTranslations != null) {return _loadedTranslations!;}util.loadDateIntlDataIfNotLoaded();Locale baseLocal = Locale('zh', 'CH'); ///重写zh的配置final String localeName = intl.Intl.canonicalizedLocale(baseLocal.toString());assert(baseLocal.toString() == localeName,'Flutter does not support the non-standard locale form $baseLocal (which ''might be $localeName',);intl.DateFormat fullYearFormat;intl.DateFormat compactDateFormat;intl.DateFormat shortDateFormat;intl.DateFormat mediumDateFormat;intl.DateFormat longDateFormat;intl.DateFormat yearMonthFormat;intl.DateFormat shortMonthDayFormat;if (intl.DateFormat.localeExists(localeName)) {fullYearFormat = intl.DateFormat.y(localeName);compactDateFormat = intl.DateFormat.yMd(localeName);shortDateFormat = intl.DateFormat.yMMMd(localeName);mediumDateFormat = intl.DateFormat.MMMEd(localeName);longDateFormat = intl.DateFormat.yMMMMEEEEd(localeName);yearMonthFormat = intl.DateFormat.yMMMM(localeName);shortMonthDayFormat = intl.DateFormat.MMMd(localeName);} else if (intl.DateFormat.localeExists(baseLocal.languageCode)) {fullYearFormat = intl.DateFormat.y(baseLocal.languageCode);compactDateFormat = intl.DateFormat.yMd(baseLocal.languageCode);shortDateFormat = intl.DateFormat.yMMMd(baseLocal.languageCode);mediumDateFormat = intl.DateFormat.MMMEd(baseLocal.languageCode);longDateFormat = intl.DateFormat.yMMMMEEEEd(baseLocal.languageCode);yearMonthFormat = intl.DateFormat.yMMMM(baseLocal.languageCode);shortMonthDayFormat = intl.DateFormat.MMMd(baseLocal.languageCode);} else {fullYearFormat = intl.DateFormat.y();compactDateFormat = intl.DateFormat.yMd();shortDateFormat = intl.DateFormat.yMMMd();mediumDateFormat = intl.DateFormat.MMMEd();longDateFormat = intl.DateFormat.yMMMMEEEEd();yearMonthFormat = intl.DateFormat.yMMMM();shortMonthDayFormat = intl.DateFormat.MMMd();}intl.NumberFormat decimalFormat;intl.NumberFormat twoDigitZeroPaddedFormat;if (intl.NumberFormat.localeExists(localeName)) {decimalFormat = intl.NumberFormat.decimalPattern(localeName);twoDigitZeroPaddedFormat = intl.NumberFormat('00', localeName);} else if (intl.NumberFormat.localeExists(locale.languageCode)) {decimalFormat = intl.NumberFormat.decimalPattern(locale.languageCode);twoDigitZeroPaddedFormat = intl.NumberFormat('00', locale.languageCode);} else {decimalFormat = intl.NumberFormat.decimalPattern();twoDigitZeroPaddedFormat = intl.NumberFormat('00');}_loadedTranslations = SynchronousFuture(MyMaterialLocalizationZh(fullYearFormat: fullYearFormat,compactDateFormat: compactDateFormat,shortDateFormat: shortDateFormat,mediumDateFormat: mediumDateFormat,longDateFormat: longDateFormat,yearMonthFormat: yearMonthFormat,shortMonthDayFormat: shortMonthDayFormat,decimalFormat: decimalFormat,twoDigitZeroPaddedFormat: twoDigitZeroPaddedFormat));return _loadedTranslations!;}bool shouldReload(MyLocalizationsDelegate old) => false;String toString() => 'GlobalMaterialLocalizations.delegate(${kMaterialSupportedLanguages.length} locales)';
}

3.在main.dar中补充该LocalizationsDelegate

class _MyApp extends State<MyApp> with WidgetsBindingObserver {Widget build(BuildContext context) {MainInit.buildInit(context);final ThemeData theme = ThemeData();return MaterialApp(///...///国际化localizationsDelegates: const [MyLocalizationsDelegate.delegate, ///自定义的LocalizationsDelegateGlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,]///supportedLocales中不用加该自定义Locale}

4. 注意

pubspec.yaml

dependencies:flutter:sdk: flutterflutter_localizations: # 需要注意需要配置了该值sdk: flutter

使用时

      Localizations.override(context: context,locale: Locale('my_local', 'CH'),child: widget,);
http://www.lryc.cn/news/358975.html

相关文章:

  • HTTPS 原理技术
  • Linux基础指令及其作用之压缩与解压
  • ORA-08189: 因为未启用行移动功能, 不能闪回表问题
  • html+CSS部分基础运用9
  • 五大元素之一,累不累——Java内部类
  • YAML快速编写示例
  • 2024 江苏省大学生程序设计大赛 2024 Jiangsu Collegiate Programming Contest(FGKI)
  • 【C语言】基于C语言实现的贪吃蛇游戏
  • 代码审计(工具Fortify 、Seay审计系统安装及漏洞验证)
  • cocos creator 3.x 手搓背包拖拽装备
  • 运维开发.Kubernetes探针与应用
  • Spring 框架:Java 企业级开发的基石
  • 在Docker中使用GPU
  • vue3 前端实现导出下载pdf文件
  • AI智能体研发之路-模型篇(五):pytorch vs tensorflow框架DNN网络结构源码级对比
  • 电商物流查询解决方案助力提升消费者体验
  • 【深度密码】神经网络算法在机器学习中的前沿探索
  • 搭载算能 BM1684 芯片,面向AI推理计算加速卡
  • Python开发 我的世界 Painting-the-World: Minecraft 像素图片生成器
  • 【经验分享】盘点“食用“的写文素材
  • 实习碰到的问题w1
  • c#实现BPM系统网络传输接口,http协议,post
  • 如何修改开源项目中发现的bug?
  • 结构设计模式 - 代理设计模式 - JAVA
  • 企业了解这些cad图纸加密方法,再也不怕图纸被盗了!
  • # 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项
  • 神经网络与深度学习——第14章 深度强化学习
  • centOS 编译C/C++
  • java——网络原理初识
  • js怎么判断是否为手机号?js格式校验方法