gossamer/intl/relative_time_format
Locale-aware relative-time formatting via the JavaScript
Intl.RelativeTimeFormat. Reusing a built
RelativeTimeFormat across many calls is
significantly faster than building one per call.
Types
The configuration for a
RelativeTimeFormat.
pub opaque type Builder
Whether the formatter renders numeric values verbatim or
substitutes locale-specific wording. Maps the JavaScript
numeric option.
pub type Numeric {
Always
Auto
}
Constructors
-
AlwaysAlways render the numeric value (
"in 1 day", the default). -
AutoSubstitute locale-specific wording for special values (
"tomorrow"instead of"in 1 day").
A single segment of a formatted relative-time string, returned by
the format_*_to_parts family. The unit field is Some for
segments that quantify a time unit (the integer portion of
"in 1 day") and None for literal connective text.
pub type Part {
Part(kind: PartKind, value: String, unit: option.Option(Unit))
}
Constructors
-
Part(kind: PartKind, value: String, unit: option.Option(Unit))
The kind of a Part. Mirrors the relevant subset of
JavaScript Intl.RelativeTimeFormatPart.type.
pub type PartKind {
Literal
Integer
Decimal
Fraction
Group
MinusSign
PlusSign
Infinity
Nan
}
Constructors
-
LiteralA literal connective text (
"in "," ago"). -
IntegerAn integer digit run.
-
DecimalThe decimal separator (
"."or","per locale). -
FractionThe fractional portion of a number.
-
GroupA digit-group separator (
","or" "per locale). -
MinusSignA minus sign for negative durations.
-
PlusSignA plus sign.
-
InfinityThe literal infinity symbol (
"∞"). -
NanThe literal NaN representation.
A configured formatter that renders relative time spans
("in 2 hours", "yesterday", etc.) in a locale-specific way.
See Intl.RelativeTimeFormat on MDN.
pub type RelativeTimeFormat
The options the runtime resolved for a
RelativeTimeFormat, including the defaults
it filled in. locale is the BCP 47 tag chosen from the requested
priority list (e.g., "en-US").
pub type ResolvedOptions {
ResolvedOptions(
locale: String,
style: intl.LabelStyle,
numeric: Numeric,
numbering_system: String,
)
}
Constructors
-
ResolvedOptions( locale: String, style: intl.LabelStyle, numeric: Numeric, numbering_system: String, )
Values
pub fn build(builder: Builder) -> Result(RelativeTimeFormat, Nil)
Constructs a RelativeTimeFormat from the
configured builder. Returns Error(Nil) if any locale tag is
structurally invalid.
pub fn format_duration(
formatter: RelativeTimeFormat,
value: duration.Duration,
) -> String
Formats a gleam/time/duration.Duration as relative time,
decomposing into the largest applicable unit
(duration.hours(-2) → "2 hours ago"). Sub-second durations
clamp to zero seconds because Intl.RelativeTimeFormat doesn’t
accept sub-second units; the rendered phrasing then depends on the
formatter’s Numeric setting.
pub fn format_duration_to_parts(
formatter: RelativeTimeFormat,
value: duration.Duration,
) -> List(Part)
Formats a gleam/time/duration.Duration and returns its
decomposition into Parts. Uses the same unit
decomposition as format_duration.
pub fn format_float(
formatter: RelativeTimeFormat,
value: Float,
in unit: Unit,
) -> String
Formats a Float value as a relative time in the given unit
(format_float(rtf, -1.0, in: Day) → "1 day ago").
pub fn format_float_to_parts(
formatter: RelativeTimeFormat,
value: Float,
in unit: Unit,
) -> List(Part)
Formats a Float value and returns its decomposition into
segments.
pub fn format_int(
formatter: RelativeTimeFormat,
value: Int,
in unit: Unit,
) -> String
Formats an Int value as a relative time in the given unit.
pub fn format_int_to_parts(
formatter: RelativeTimeFormat,
value: Int,
in unit: Unit,
) -> List(Part)
Formats an Int value as a list of Parts.
pub fn new(locales: List(String)) -> Builder
Creates a Builder for the given locale priority list. The
runtime picks the first locale it supports; pass an empty list to
use the runtime’s default locale.
pub fn resolved_options(
formatter: RelativeTimeFormat,
) -> ResolvedOptions
The locale, style, numeric mode, and numbering system the runtime resolved from the builder’s configuration.
pub fn supported_locales_of(
locales: List(String),
) -> Result(List(String), Nil)
Filters locales to those the runtime supports for relative-time
formatting, preserving the input order. Returns Error(Nil) if any
locale tag is structurally malformed.
pub fn with_locale_matcher(
builder: Builder,
locale_matcher: intl.LocaleMatcher,
) -> Builder
Sets the locale-matching algorithm used to pick a locale from the priority list.
pub fn with_numeric(
builder: Builder,
numeric: Numeric,
) -> Builder
Sets whether the formatter renders numeric values verbatim or substitutes locale-specific wording for special values.
pub fn with_style(
builder: Builder,
style: intl.LabelStyle,
) -> Builder
Sets the verbosity of the unit labels.