开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载ResourceKeys.DateHumanize是 Humanizer 2.x 时代DateTime.Humanize()本地化管线的资源键入口类它以常量与约定方法封装了现在从未以及各时间单位过去/未来短语的资源键生成规则。本文将以 Humanizer.Localisation.ResourceKeys.DateHumanize.md 为骨架结合仓库源码说明该类如何与 Formatter、策略算法配合工作以及它在现代主线版本中的演进去向。一、类概览资源键的集中定义点在 Humanizer 2.11.10 版本的 API 中ResourceKeys.DateHumanize 被定义为public static class ResourceKeys.DateHumanize其职责用文档原文概括为封装获取 DateTime.Humanize 资源键所需的逻辑Encapsulates the logic required to get the resource keys for DateTime.Humanize。它是一个静态类继承链为System.Object→DateHumanize内部包含两个静态常量字段和一个静态方法成员类型用途Neverpublic const string表示从未发生场景的资源键Nowpublic const string表示当前时刻场景的资源键GetResourceKey(TimeUnit, Tense, int)public static string按约定生成某个时间单位、时态、数量组合对应的资源键该类与同版本 API 中 ResourceKeys.TimeSpanHumanize 一并构成 Humanizer 2.x 的资源键命名约定层所有本地化短语都通过固定的键名模式被 Formatter 查表取用。二、静态常量字段Now 与 NeverResourceKeys.DateHumanize.Neverpublic const string Never DateHumanize_Never;该字段对应日期从未发生的本地化短语键。在DateTime?为null的场景下Humanizer 的 DateHumanizeExtensions.cs 会直接调用 Formatter 的DateHumanize_Never()输出类似 never 的文案public static string Humanize(this DateTime? input, bool? utcDate null, DateTime? dateToCompareAgainst null, CultureInfo? culture null) { if (input.HasValue) { return Humanize(input.Value, utcDate, dateToCompareAgainst, culture); } return Configurator .GetFormatter(culture) .DateHumanize_Never(); }可见Never常量命名的正是DateHumanize_Never()这一 Formatter 方法背后的资源键而 IFormatter.cs 将该方法声明为string DateHumanize_Never();与常量值一一对应。ResourceKeys.DateHumanize.Nowpublic const string Now DateHumanize_Now;该字段对应当前时刻的本地化短语键。在默认实现 DefaultFormatter.cs 中public virtual string DateHumanize_Now() phraseTable.DateNow ?? now;DateHumanize_Now()被IDateTimeHumanizeStrategy等策略在时差为 0即时间差小于最小可表示单位时调用输出 now 之类的文案。测试侧CoverageGapTests.cs 验证了DateHumanize(TimeUnit.Day, Tense.Future, 0)应返回 now印证Now键对应的正是零差量场景。三、GetResourceKey 方法按约定生成资源键类中最具技术含量的成员是静态方法GetResourceKey其签名如下public static string GetResourceKey(Humanizer.Localisation.TimeUnit timeUnit, Humanizer.Localisation.Tense timeUnitTense, int count1);参数语义参数类型说明timeUnitTimeUnit时间单位timeUnitTenseTense该单位属于未来还是过去countint默认1单位数量默认即一One返回值返回值为string按文档示例形如DateHumanize_SingleMinuteAgo结合两个常量字段DateHumanize_Never、DateHumanize_Now的命名风格可以推断资源键遵循统一的DateHumanize_前缀 单复数标记 时间单位 时态标记的约定模式前缀DateHumanize_、数量为单数时的Single标记、单位Minute、过去时态后缀Ago。这正是文档所称的accordning to convention按约定生成的含义——键名本身携带了时间单位、时态与单复数语义Formatter 无需记忆散落的资源标识符只需按同样约定反向解析即可取用对应短语。配套枚举GetResourceKey的两个枚举参数在当前仓库中的定义如下TimeUnit.csMillisecond、Second、Minute、Hour、Day、Week、Month、Year八个单位覆盖从毫秒到年的完整时间尺度Tense.cs仅Future如 in 2 days与Past如 2 days ago两个取值。四、资源键的消费方IFormatter 与 DefaultFormatter在 2.x 架构中资源键并不是直接被扩展方法引用的而是通过IFormatter接口的方法约定间接消费。当前仓库的 IFormatter.cs 中与日期相关的契约如下string DateHumanize_Now(); string DateHumanize_Never(); string DateHumanize(TimeUnit timeUnit, Tense timeUnitTense, int unit);默认实现 DefaultFormatter.cs 中的DateHumanize负责最终渲染相对日期短语public virtual string DateHumanize(TimeUnit timeUnit, Tense timeUnitTense, int unit) TryFormatDateFromPhraseTable(timeUnit, timeUnitTense, unit, out var result) ? result : throw new InvalidOperationException($Missing generated relative-date phrase for {Culture.Name} and unit {timeUnit}.);可以看到当前主线版本的DefaultFormatter已经改从LocalePhraseTable生成式短语表读取短语而在 2.11.10 的 API 文档中DefaultFormatter暴露的是protected virtual string GetResourceKey(string resourceKey)与GetResourceKey(string resourceKey, int number)这类基于资源键的钩子——这正是ResourceKeys.DateHumanize服务的对象子类化 Formatter 时开发者重写GetResourceKey即可将某个约定键重定向到自定义资源。五、键背后的本地化数据relativeDate 短语表资源键所指向的短语内容在 en.yml 的surfaces.phrases.relativeDate节点中集中维护。以英语为例phrases: relativeDate: now: now today: today never: never past: millisecond: single: one millisecond ago multiple: afterCount: ago forms: default: milliseconds minute: single: a minute ago multiple: afterCount: ago forms: singular: minute default: minutes day: single: yesterday ... future: day: single: tomorrow multiple: afterCount: from now forms: singular: day default: days从这份配置可以读出资源键约定背后的完整语义矩阵每个TimeUnitmillisecond到year× 每个Tensepast/future各有一个短语节点短语区分single数量为 1与multiple数量大于 1两种形态multiple下再细分单数/默认复数词形forms.singular/forms.default数量词与单位词通过afterCount如 ago、from now拼接对应DateHumanize_SingleMinuteAgo这类键名中Ago后缀的语义day单位存在特例过去单数用 yesterday、未来单数用 tomorrow而非机械拼接。这意味着GetResourceKey(TimeUnit.Minute, Tense.Past, 1)生成的键指向的正是 a minute ago 这条短语而GetResourceKey(TimeUnit.Day, Tense.Future, 2)则对应 2 days from now。六、策略算法资源键在 Humanize 管线中的位置ResourceKeys.DateHumanize生成的键最终服务于DateTime.Humanize()的完整调用链。以默认策略为例DateTimeHumanizeAlgorithms.cs 中当比较结果为今天时会调用DateHumanize_Today其余情况则根据TimeUnit与Tense委托 Formatter 渲染短语用户调用dateTime.Humanize()DateHumanizeExtensions.cs进入Configurator.DateTimeHumanizeStrategy.Humanize(input, comparisonBase, culture)策略算法计算出时间差与目标单位、时态DefaultFormatter.DateHumanize(timeUnit, tense, unit)按约定定位短语并完成数量词渲染。对应地测试 DateHumanizeDefaultStrategyTests.cs 对秒、分钟、小时、天、月、年六个单位的过去/未来两时态做了全覆盖验证例如 2 天前应输出 2 days ago、28 天后应输出对应的未来表述这些预期文案与relativeDate配置、资源键约定三者互为印证。七、演进观察从资源键到生成式短语表需要特别说明的是ResourceKeys.DateHumanize是 Humanizer 2.x API 快照versioned_docs/version-2.11.10中的类型。从当前主线源码看src/Humanizer下已不再存在ResourceKeys类资源定位被 LocalePhraseTable 与 DefaultFormatter 的生成式短语表取代迁移文档 version-4-migration.mdx 也明确提到旧版基于字符串键的Format与GetResourceKey钩子在新版本中已被移除。因此对于仍在使用 2.x 并依赖ResourceKeys.DateHumanize自定义 Formatter 的项目迁移到新版本时需要将基于资源键的GetResourceKey重写改为覆盖DateHumanize、DateHumanize_Now、DateHumanize_Never等直接返回短语的虚方法将原先散落在资源文件中的键值对迁移到surfaces.phrases.relativeDate结构化的 YAML 短语数据中。八、总结ResourceKeys.DateHumanize虽只是一个静态类、两个常量与一个约定方法却是理解 Humanizer 2.x 本地化架构的钥匙常量字段Now/Never覆盖了现在与从未两个零差量边界场景约定方法GetResourceKey(TimeUnit, Tense, int)用DateHumanize_前缀统一了八个时间单位、两个时态与单复数形态的键名空间消费链路经由IFormatter.DateHumanize与DefaultFormatter完成从键到本地化短语的落地演进方向则是从字符串键查表走向生成式短语表最终在 4.x 中彻底告别字符串键钩子。对于希望深度定制DateTime.Humanize输出的开发者理解这套键约定与短语数据结构是编写自定义 Formatter 或向新短语表体系迁移的第一步。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer 资源键体系解析ResourceKeys.DateHumanize 与 DateTime.Humanize 的本地化键约定Humanizer 资源键体系解析ResourceKeys.DateHumanize 与 DateTime.Humanize 的本地化键约定 Humanize开发工具LEANN 如何在 CUDA GPU 上启用 FlashLib 后端实现高吞吐向量搜索LEANN 如何在 CUDA GPU 上启用 FlashLib 后端实现高吞吐向量搜索 如果你的语料已经建好 LEANN 索引、机器上有 CUDA GPU并开发工具Humanizer 资源键机制解析深入 ResourceKeys.TimeSpanHumanize 与 TimeSpan.Humanize 的本地化约定Humanizer 资源键机制解析深入 ResourceKeys.TimeSpanHumanize 与 TimeSpan.Humanize 的本地化约定 本篇开发工具上一篇HLS视频下载终极实操这款浏览器扩展如何零门槛把在线视频流永久存进硬盘下一篇B站视频下载免费工具三步解锁4K高清与大会员专属内容完整教程创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考