搜索插件、代码仓与 APP 分类

插件详情v1.2.1

humanize

Humanize 是一个紧凑的工具包,可让 UI 字符串显得更自然。它通过帮助处理日期、数字、列表和英文及法文文本,将原始数据与人性化表达之间进行衔接。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1710
喜欢数
33
Pub 分数
145

安装插件

flutter pub add humanize

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Humanize 是一个轻量级的 Dart 包,可将原始数据转换为英文和法文的自然、易读文本。它提供了直观的辅助工具,用于格式化日期、数字、列表和一般文本,确保用户界面内容显得对话式且精致。非常适合需要本地化、用户友好输出而无需样板代码的应用程序。该包支持多种平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,使其在跨平台开发中具有高度灵活性。

适合场景

  • 将时间戳格式化为可读短语
  • 将数字转换为自然语言
  • 使用 'and' 在最后一个项目前创建用逗号分隔的列表
  • 为英语和法语使用者本地化用户界面文本
  • 提高应用程序中动态内容的可读性
  • 简化表单和仪表板中的字符串生成

主要特点

  • 支持英文和法文本地化
  • 日期、数字和列表的辅助工具
  • 具有直观方法的最小 API
  • 跨平台兼容性
  • 轻量且无依赖
  • 通过 Flutter pub add 轻松集成

适合对象

  • 面向英语和法语用户的应用
  • 寻求简洁、可读的UI字符串的开发者
  • 需要简单文本格式化但不需要大型库的项目
  • 重视本地化和用户体验的团队
  • 带有动态内容的 Flutter 应用程序显示
  • 希望设置简单的前端开发人员

使用建议

  • 仅限英语和法语
  • 不支持其他语言环境
  • 需要手动处理复数等边缘情况
  • 不适合复杂的国际化需求
  • 依赖于一致的输入格式
  • 可能需要额外的逻辑来处理非标准用例

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Humanize

pub.dev Star on Github Awesome Flutter Flutter Samples License: MIT

Humanize

Humanize for Dart & Flutter 👋

Humanize is a compact toolkit that makes UI strings feel natural. It bridges raw data and human-friendly phrasing with helpers for dates, numbers, lists, and text in English and French.

Table of Contents

Overview

Humanize focuses on pragmatic, readable output for UI strings. It packages natural phrases (e.g., “3 minutes ago”, “tomorrow”) and formatting helpers under a consistent API with light-touch locale support.

Feature Highlights

  • Relative time and calendar-friendly deltas via naturaltime, naturaldelta, naturalday
  • Flexible formatting for DateTime (ISO, RFC 2822, HTTP, timestamps, locale-aware)
  • Humanized lists and text helpers: naturalJoin, oxfordComma, capfirst, titlecase, truncate*
  • Number utilities with locale presets: intcomma, intword, ordinal, apNumber
  • File size display with filesizeformat
  • Calendar math helpers: addCalendarMonths, addCalendarYears
  • Lightweight i18n facade through HumanizeI18n

Getting Started

Installation

dependencies:
  humanize: latest_version
import 'package:humanize/humanize.dart';

Quick Start

print(naturaltime(DateTime.now().subtract(Duration(minutes: 3)), locale: 'en')); // 3 minutes ago
print(naturaldelta(Duration(hours: 2), locale: 'fr'));                            // 2 heures

final ref = DateTime(2007, 2, 17, 12);
print(naturalday(DateTime(2007, 2, 16), now: ref, locale: 'en', format: 'dd MMMM yyyy')); // yesterday

print(naturalJoin(['a','b','c'], locale: 'fr')); // a, b et c
print(oxfordComma(['a','b','c'], locale: 'en')); // a, b, and c

print(intcomma(4500.2));            // 4,500.2
print(intcomma(1234567, sep: ' ')); // 1 234 567
print(intword(1200000, locale: 'fr')); // 1,2 millions
print(filesizeformat(1536));        // 1.5 KB

Usage Guide

Dates & Times
final now = DateTime(2007, 2, 17, 16, 30, 0);
print(naturaltime(now.add(Duration(days: 9)), now: now)); // in 1 week, 2 days
print(naturaltime(now.subtract(Duration(days: 1, hours: 2)), now: now, locale: 'fr')); // il y a un jour, 2 heures

final dt = DateTime(2017,4,20,11,32);
print(dt.toISO());      // 2017-04-20T11:32:00.000-04:00
print(dt.toRFC2822());  // Thu, 20 Apr 2017 11:32:00 -0400
print(dt.toHTTP());     // Thu, 20 Apr 2017 15:32:00 GMT
print(dt.asLocale('fr').toHTTP()); // jeu., 20 avr. 2017 15:32:00 GMT (display)

// Locale formats
print(dt.toLocaleString());                                 // e.g., 4/20/2017
print(dt.toLocaleString(HumanDateFormat.DATETIME_FULL));    // April 20, 2017...
print(dt.asLocale('fr').toLocaleString(HumanDateFormat.DATETIME_FULL)); // 20 avril 2017...
Numbers
print(intcomma(4500));    // 4,500
print(intword(1500000, short: true)); // 1.5 M
print(ordinal(1, locale: 'fr')); // 1er
print(apNumber(5, locale: 'fr')); // cinq
Lists, Text & Filesize
print(naturalJoin(['a','b','c'], locale: 'fr')); // a, b et c
print(oxfordComma(['a','b','c']));               // a, b, and c
print(capfirst('hello'));                        // Hello
print(filesizeformat(1536));                     // 1.5 KB
Calendar Math
final d = DateTime(2017, 2, 13);
print(d.addCalendarMonths(1).toISODate());    // 2017-03-13
print(d.add(Duration(days: 30)).toISODate()); // 2017-03-15
i18n Facade
final hFr = HumanizeI18n('fr');
print(hFr.naturalTime(DateTime.now().subtract(Duration(minutes: 3))));
print(hFr.intComma(1234567)); // 1 234 567

Examples & Tests

Examples in example/: basic_usage.dart, i18n_facade.dart, intcomma_examples.dart, naturaltime_examples.dart, naturalday_examples.dart, ordinal_examples.dart, formatting_examples.dart, math_examples.dart

Run tests:

dart test

Notes & Limitations

  • Built-in locales: en, fr. PRs welcome to extend.
  • For strict formatting (currencies, complex calendars), combine with package:intl and use Humanize for human-friendly phrasing.
  • Localized RFC/HTTP outputs are intended for display only (not protocol-compliant).

Resources

  • License: see LICENSE
  • Repo: https://github.com/jeanluc243/humanize
  • Issues: https://github.com/jeanluc243/humanize/issues

Keywords

  • Dart string formatting
  • Flutter localization helpers
  • humanize dates and numbers
  • Dart natural language utilities
  • human-readable UI text toolkit

Maintainers

Support

If Humanize makes your UI sparkle, feel free to fuel the magic with a donation—anything from a cheeky 🍺 to the legendary MacBook Pro upgrade 💻😂. Tap the link, pick your vibe, and let's keep shipping joy: Buy Me a Coffee.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。