当前位置: 首页 > 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;根据输入的图…...

【经验分享】盘点“食用“的写文素材

一、构建框架 简介 1. 身份 擅长领域 2. 博客内容 3. 目前示例&#xff1a; 阿里云专家博主&#xff0c;华为云-云享专家&#xff0c;专注前、后端开发 博客内容&#xff1a;前后端实战教学、源码剖析、常见面试知识解析、算法题解与心得、日常考研总结等 目前正在备战考研&…...

实习碰到的问题w1

1.vueelementUI在输入框中按回车键会刷新页面 当一个 form 元素中只有一个输入框时&#xff0c;在该输入框中按下回车应提交该表单。如果希望阻止这一默认 行为&#xff0c;可以在 <el-form> 标签上添加 submit.native.prevent 。 参考&#xff1a;element-ui 表单 form …...

c#实现BPM系统网络传输接口,http协议,post

BPM通过http协议实现网络传输&#xff0c;语言使用.net(c#)&#xff0c;在这里只提供一个接口&#xff0c;具体代码如下,请参照&#xff1a; public string MakeRequest(string parameters) { ServicePointManager.ServerCertificateValidationCallback new Syst…...

如何修改开源项目中发现的bug?

如何修改开源项目中发现的bug&#xff1f; 目录 如何修改开源项目中发现的bug&#xff1f;第一步&#xff1a;找到开源项目并建立分支第二步&#xff1a;克隆分支到本地仓库第三步&#xff1a;在本地对项目进行修改第四步&#xff1a;依次使用命令行进行操作注意&#xff1a;Gi…...

结构设计模式 - 代理设计模式 - JAVA

代理设计模式 一. 介绍二. 代码示例2.1 定义 CommandExecutor 类2.2 定义 CommandExecutorProxy代理类2.3 模拟客户端2.4 测试结果 三. 结论 前言 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子…...

企业了解这些cad图纸加密方法,再也不怕图纸被盗了!

在竞争激烈的商业环境中&#xff0c;企业的核心技术、设计图纸和创意是维持其市场地位和竞争优势的关键。CAD图纸作为产品设计的重要载体&#xff0c;其安全性自然成为企业关注的焦点。为了确保CAD图纸不被非法获取或盗用&#xff0c;企业需要采取一系列有效的加密方法。本文将…...

# 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项

为什么会突然想到写这么一个大杂烩的博文呢&#xff0c;必须要从笔者几年前的一次面试说起 当时的我年轻气盛&#xff0c;在简历上放了自己的博客地址&#xff0c;而面试官应该是翻了我的博客&#xff0c;好几道面试题都是围绕着我的博文来提问 其中一个问题&#xff0c;直接…...

神经网络与深度学习——第14章 深度强化学习

本文讨论的内容参考自《神经网络与深度学习》https://nndl.github.io/ 第14章 深度强化学习 深度强化学习 强化学习&#xff08;Reinforcement Learning&#xff0c;RL&#xff09;&#xff0c;也叫增强学习&#xff0c;是指一类从与环境交互中不断学习的问题以及解决这类问题…...

centOS 编译C/C++

安装C和C编译器 yum -y install gcc*查看CenterOS系统信息 cat /etc/system-releaseCentOS Linux release 8.2.2004 (Core)查看gcc版本 gcc --versiongcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4) Copyright (C) 2018 Free Software Foundation, Inc. This is free software…...

java——网络原理初识

T04BF &#x1f44b;专栏: 算法|JAVA|MySQL|C语言 &#x1faf5; 小比特 大梦想 目录 1.网络通信概念初识1.1 IP地址1.2端口号1.3协议1.3.1协议分层协议分层带来的好处主要有两个方面 1.3.2 TCP/IP五层 (或四层模型)1.3.3 协议的层和层之间是怎么配合工作的 1.网络通信概念初识…...