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中定义的的localeName bool 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,);
相关文章:
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 MyMaterialLocaliza…...

HTTPS 原理技术
HTTPS原理技术 背景简介原理总结 背景 随着年龄的增长,很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来,技术出身的人总是很难放下一些执念,遂将这些知识整理成文,以纪念曾经努力学习奋斗的日子。本文内容并非完全原创&am…...
Linux基础指令及其作用之压缩与解压
压缩与解压targzip示例输出解释 gunzipzipunzip 压缩与解压 tar tar xzf 是一个常用的命令组合,用于解压缩由 gzip 压缩的 tarball 文件。下面是对这个命令的详细说明: tar:这是一个用于在 Linux 和类 Unix 系统上创建、查看或提取归档文件…...

ORA-08189: 因为未启用行移动功能, 不能闪回表问题
在执行闪回恢复误删数据出现“ORA-08189: 因为未启用行移动功能, 不能闪回表”的错误提示。 ORA-08189 错误表示你尝试对一个表执行闪回操作,但该表没有启用行移动(ROW MOVEMENT)功能。行移动是Oracle中的一个特性,它允许表中的行…...

html+CSS部分基础运用9
项目1 参会注册表 1.设计参会注册表页面,效果如图9-1所示。 图9-1 参会注册表页面 项目2 设计《大学生暑期社会实践调查问卷》 1.设计“大学生暑期社会实践调查问卷”页面,如图9-2所示。 图9-2 大学生暑期社会调查表页面 2.调查表前导语的…...
五大元素之一,累不累——Java内部类
目录 简略版: 详解版: 使用场景: 内部类的优点: 内部类的分类: 一. 成员内部类 1.创建对象 2.访问方法 3. 外部类名.this. 二. 静态内部类 1. 创建对象 2. 访问特点 三. 局部内部类 四. 匿名内部类 …...

YAML快速编写示例
一、案例 1.1 自主式创建service关联上方的pod 资源名称my-nginx-kkk命名空间my-kkk容器镜像nginx:1.21容器端口80标签njzb:my-kkk 1.1.1 创建一个demo文件夹 1.1.2 创建并获取模版文件 1.1.3 查看服务并编写yaml文件 1.1.4 编写yaml文件并部署,查看服务是否运行成…...
2024 江苏省大学生程序设计大赛 2024 Jiangsu Collegiate Programming Contest(FGKI)
题目来源:https://codeforces.com/gym/105161 文章目录 F - Download Speed Monitor题意思路编程 G - Download Time Monitor题意思路编程 K - Number Deletion Game题意思路编程 I - Integer Reaction题意思路编程 写在前面:今天打的训练赛打的很水&…...

【C语言】基于C语言实现的贪吃蛇游戏
【C语言】基于C语言实现的贪吃蛇游戏 🔥个人主页:大白的编程日记 🔥专栏:C语言学习之路 文章目录 【C语言】基于C语言实现的贪吃蛇游戏前言一.最终实现效果一.Win32 API介绍1.1Win32 API1.2控制台程序1.3控制台屏幕上的坐标COORD…...

代码审计(工具Fortify 、Seay审计系统安装及漏洞验证)
源代码审计 代码安全测试简介 代码安全测试是从安全的角度对代码进行的安全测试评估。(白盒测试;可看到源代码) 结合丰富的安全知识、编程经验、测试技术,利用静态分析和人工审核的方法寻找代码在架构和编码上的安全缺陷…...

cocos creator 3.x 手搓背包拖拽装备
项目背景: 游戏背包 需要手动 拖拽游戏装备到 装备卡槽中,看了下网上资料很少。手搓了一个下午搞定,现在来记录下实现步骤; 功能拆分: 一个完整需求,我们一般会把它拆分成 几个小步骤分别造零件。等都造好了…...

运维开发.Kubernetes探针与应用
运维系列 Kubernetes探针与应用 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite:http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/qq_28550263…...

Spring 框架:Java 企业级开发的基石
文章目录 序言Spring 框架的核心概念Spring 框架的主要模块Spring Boot:简化 Spring 开发Spring Cloud:构建微服务架构实际案例分析结论 序言 Spring 框架自 2002 年发布以来,已经成为 Java 企业级开发的标准之一。它通过提供全面的基础设施…...

在Docker中使用GPU
一、安装nvidia-container-toolkit 总之一句话:nvidia-docker和nvidia-docker2,nvidia-container-runtime 已经被英伟达迭代了,可以认为nvidia-container-toolkit是nvidia-docker和nvidia-docker2, nvidia-container-runtime 的替…...

vue3 前端实现导出下载pdf文件
这样的数据实现导出 yourArrayBufferOrByteArray 就是后端返回数据 // 创建Blob对象const blob new Blob([new Uint8Array(res)], { type: application/pdf })// 创建一个表示该Blob的URLconst url URL.createObjectURL(blob);// 创建一个a标签用于下载const a document.cr…...
AI智能体研发之路-模型篇(五):pytorch vs tensorflow框架DNN网络结构源码级对比
博客导读: 《AI—工程篇》 AI智能体研发之路-工程篇(一):Docker助力AI智能体开发提效 AI智能体研发之路-工程篇(二):Dify智能体开发平台一键部署 AI智能体研发之路-工程篇(三&am…...

电商物流查询解决方案助力提升消费者体验
截至2023年12月,中国网络购物用户规模达9.15亿人,占网民整体的83.8%。这一庞大的数字不仅展现了电子商务的蓬勃发展,也标志着数字零售企业营销战略的转变——从以产品和流量为核心,到用户为王的新阶段。因此,提升消费者…...

【深度密码】神经网络算法在机器学习中的前沿探索
目录 🚝前言 🚍什么是机器学习 1. 基本概念 2. 类型 3. 关键算法 4. 应用领域 5. 工作流程 🚋什么是神经网络 基本结构 🚂神经网络的工作原理 前向传播(Forward Propagation): 损失函…...

搭载算能 BM1684 芯片,面向AI推理计算加速卡
搭载算能 BM1684 芯片,是面向AI推理的算力卡。可集成于服务器、工控机中,高效适配市场上所有AI算法,实现视频结构化、人脸识别、行为分析、状态监测等应用,为智慧城市、智慧交通、智慧能源、智慧金融、智慧电信、智慧工业等领域进…...
Python开发 我的世界 Painting-the-World: Minecraft 像素图片生成器
简介 Painting-the-World 是一款创新的工具,专为《我的世界》(Minecraft) 玩家及创作者设计,旨在将数字图片转变为游戏内的像素艺术。通过利用 RCON (Remote Console) 协议,本项目可以直接与《我的世界》服务器对话,根据输入的图…...

IDEA运行Tomcat出现乱码问题解决汇总
最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…...
【网络】每天掌握一个Linux命令 - iftop
在Linux系统中,iftop是网络管理的得力助手,能实时监控网络流量、连接情况等,帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
6月5日,2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席,并作《智能体在安全领域的应用实践》主题演讲,分享了在智能体在安全领域的突破性实践。他指出,百度通过将安全能力…...

Mac下Android Studio扫描根目录卡死问题记录
环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中,提示一个依赖外部头文件的cpp源文件需要同步,点…...

使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台
🎯 使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台 📌 项目背景 随着大语言模型(LLM)的广泛应用,开发者常面临多个挑战: 各大模型(OpenAI、Claude、Gemini、Ollama)接口风格不统一;缺乏一个统一平台进行模型调用与测试;本地模型 Ollama 的集成与前…...

企业如何增强终端安全?
在数字化转型加速的今天,企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机,到工厂里的物联网设备、智能传感器,这些终端构成了企业与外部世界连接的 “神经末梢”。然而,随着远程办公的常态化和设备接入的爆炸式…...
Xen Server服务器释放磁盘空间
disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...

论文笔记——相干体技术在裂缝预测中的应用研究
目录 相关地震知识补充地震数据的认识地震几何属性 相干体算法定义基本原理第一代相干体技术:基于互相关的相干体技术(Correlation)第二代相干体技术:基于相似的相干体技术(Semblance)基于多道相似的相干体…...

三分算法与DeepSeek辅助证明是单峰函数
前置 单峰函数有唯一的最大值,最大值左侧的数值严格单调递增,最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值,最小值左侧的数值严格单调递减,最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...
MySQL 主从同步异常处理
阅读原文:https://www.xiaozaoshu.top/articles/mysql-m-s-update-pk MySQL 做双主,遇到的这个错误: Could not execute Update_rows event on table ... Error_code: 1032是 MySQL 主从复制时的经典错误之一,通常表示ÿ…...