当前位置: 首页 > 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,);

相关文章:

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原理技术 背景简介原理总结 背景 随着年龄的增长&#xff0c;很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来&#xff0c;技术出身的人总是很难放下一些执念&#xff0c;遂将这些知识整理成文&#xff0c;以纪念曾经努力学习奋斗的日子。本文内容并非完全原创&am…...

Linux基础指令及其作用之压缩与解压

压缩与解压targzip示例输出解释 gunzipzipunzip 压缩与解压 tar tar xzf 是一个常用的命令组合&#xff0c;用于解压缩由 gzip 压缩的 tarball 文件。下面是对这个命令的详细说明&#xff1a; tar&#xff1a;这是一个用于在 Linux 和类 Unix 系统上创建、查看或提取归档文件…...

ORA-08189: 因为未启用行移动功能, 不能闪回表问题

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

html+CSS部分基础运用9

项目1 参会注册表 1.设计参会注册表页面&#xff0c;效果如图9-1所示。 图9-1 参会注册表页面 项目2 设计《大学生暑期社会实践调查问卷》 1.设计“大学生暑期社会实践调查问卷”页面&#xff0c;如图9-2所示。 图9-2 大学生暑期社会调查表页面 2&#xff0e;调查表前导语的…...

五大元素之一,累不累——Java内部类

目录 简略版&#xff1a; 详解版&#xff1a; 使用场景&#xff1a; 内部类的优点&#xff1a; 内部类的分类&#xff1a; 一. 成员内部类 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文件并部署&#xff0c;查看服务是否运行成…...

2024 江苏省大学生程序设计大赛 2024 Jiangsu Collegiate Programming Contest(FGKI)

题目来源&#xff1a;https://codeforces.com/gym/105161 文章目录 F - Download Speed Monitor题意思路编程 G - Download Time Monitor题意思路编程 K - Number Deletion Game题意思路编程 I - Integer Reaction题意思路编程 写在前面&#xff1a;今天打的训练赛打的很水&…...

【C语言】基于C语言实现的贪吃蛇游戏

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

代码审计(工具Fortify 、Seay审计系统安装及漏洞验证)

源代码审计 代码安全测试简介 代码安全测试是从安全的角度对代码进行的安全测试评估。&#xff08;白盒测试&#xff1b;可看到源代码&#xff09; 结合丰富的安全知识、编程经验、测试技术&#xff0c;利用静态分析和人工审核的方法寻找代码在架构和编码上的安全缺陷&#xf…...

cocos creator 3.x 手搓背包拖拽装备

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

运维开发.Kubernetes探针与应用

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

Spring 框架:Java 企业级开发的基石

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

在Docker中使用GPU

一、安装nvidia-container-toolkit 总之一句话&#xff1a;nvidia-docker和nvidia-docker2&#xff0c;nvidia-container-runtime 已经被英伟达迭代了&#xff0c;可以认为nvidia-container-toolkit是nvidia-docker和nvidia-docker2&#xff0c; 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网络结构源码级对比

博客导读&#xff1a; 《AI—工程篇》 AI智能体研发之路-工程篇&#xff08;一&#xff09;&#xff1a;Docker助力AI智能体开发提效 AI智能体研发之路-工程篇&#xff08;二&#xff09;&#xff1a;Dify智能体开发平台一键部署 AI智能体研发之路-工程篇&#xff08;三&am…...

电商物流查询解决方案助力提升消费者体验

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

【深度密码】神经网络算法在机器学习中的前沿探索

目录 &#x1f69d;前言 &#x1f68d;什么是机器学习 1. 基本概念 2. 类型 3. 关键算法 4. 应用领域 5. 工作流程 &#x1f68b;什么是神经网络 基本结构 &#x1f682;神经网络的工作原理 前向传播&#xff08;Forward Propagation&#xff09;&#xff1a; 损失函…...

搭载算能 BM1684 芯片,面向AI推理计算加速卡

搭载算能 BM1684 芯片&#xff0c;是面向AI推理的算力卡。可集成于服务器、工控机中&#xff0c;高效适配市场上所有AI算法&#xff0c;实现视频结构化、人脸识别、行为分析、状态监测等应用&#xff0c;为智慧城市、智慧交通、智慧能源、智慧金融、智慧电信、智慧工业等领域进…...

Python开发 我的世界 Painting-the-World: Minecraft 像素图片生成器

简介 Painting-the-World 是一款创新的工具&#xff0c;专为《我的世界》(Minecraft) 玩家及创作者设计&#xff0c;旨在将数字图片转变为游戏内的像素艺术。通过利用 RCON (Remote Console) 协议&#xff0c;本项目可以直接与《我的世界》服务器对话&#xff0c;根据输入的图…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

ssc377d修改flash分区大小

1、flash的分区默认分配16M、 / # df -h Filesystem Size Used Available Use% Mounted on /dev/root 1.9M 1.9M 0 100% / /dev/mtdblock4 3.0M...

ESP32读取DHT11温湿度数据

芯片&#xff1a;ESP32 环境&#xff1a;Arduino 一、安装DHT11传感器库 红框的库&#xff0c;别安装错了 二、代码 注意&#xff0c;DATA口要连接在D15上 #include "DHT.h" // 包含DHT库#define DHTPIN 15 // 定义DHT11数据引脚连接到ESP32的GPIO15 #define D…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

DBAPI如何优雅的获取单条数据

API如何优雅的获取单条数据 案例一 对于查询类API&#xff0c;查询的是单条数据&#xff0c;比如根据主键ID查询用户信息&#xff0c;sql如下&#xff1a; select id, name, age from user where id #{id}API默认返回的数据格式是多条的&#xff0c;如下&#xff1a; {&qu…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

tree 树组件大数据卡顿问题优化

问题背景 项目中有用到树组件用来做文件目录&#xff0c;但是由于这个树组件的节点越来越多&#xff0c;导致页面在滚动这个树组件的时候浏览器就很容易卡死。这种问题基本上都是因为dom节点太多&#xff0c;导致的浏览器卡顿&#xff0c;这里很明显就需要用到虚拟列表的技术&…...

Java 二维码

Java 二维码 **技术&#xff1a;**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...