Class: ICU4X::RelativeTimeFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/icu4x/yard_docs.rb

Overview

Formats relative time expressions (e.g., “3 days ago”, “in 2 hours”).

Examples:

formatter = ICU4X::RelativeTimeFormat.new(locale)
formatter.format(-1, :day)   #=> "1 day ago"
formatter.format(2, :hour)   #=> "in 2 hours"

With auto numeric

formatter = ICU4X::RelativeTimeFormat.new(locale, numeric: :auto)
formatter.format(-1, :day)   #=> "yesterday"
formatter.format(0, :day)    #=> "today"

Han decimal numerals via locale extension

locale = ICU4X::Locale.parse("ja-u-nu-hanidec")
formatter = ICU4X::RelativeTimeFormat.new(locale, provider: provider)
formatter.format(-3, :day)  #=> "三 日前"

Instance Method Summary collapse

Constructor Details

#initialize(locale, provider: nil, style: :long, numeric: :always) ⇒ RelativeTimeFormat

Creates a new RelativeTimeFormat instance.

Parameters:

  • locale (Locale)

    the locale for formatting

  • provider (DataProvider, nil) (defaults to: nil)

    data provider (uses default if nil)

  • style (Symbol) (defaults to: :long)

    format style: ‘:long`, `:short`, or `:narrow`

  • numeric (Symbol) (defaults to: :always)

    numeric display: ‘:always` or `:auto`

Raises:

  • (DataError)

    if data for the locale is unavailable



675
# File 'lib/icu4x/yard_docs.rb', line 675

def initialize(locale, provider: nil, style: :long, numeric: :always); end

Instance Method Details

#format(value, unit) ⇒ String

Formats a relative time value.

Examples:

formatter.format(-3, :day)    #=> "3 days ago"
formatter.format(1, :week)    #=> "in 1 week"

Parameters:

  • value (Integer)

    the relative time value (negative for past, positive for future)

  • unit (Symbol)

    time unit: ‘:second`, `:minute`, `:hour`, `:day`, `:week`, `:month`, `:quarter`, or `:year`

Returns:

  • (String)

    the formatted relative time string



688
# File 'lib/icu4x/yard_docs.rb', line 688

def format(value, unit); end

#format_to_parts(value, unit) ⇒ Array<FormattedPart>

Note:

The current ICU4X experimental RelativeTimeFormatter does not provide separate part annotations for the numeric value. The entire formatted string is returned as a single ‘:literal` part.

Formats a relative time value and returns an array of parts.

Examples:

parts = formatter.format_to_parts(-3, :day)
# => [#<ICU4X::FormattedPart type=:literal value="3 days ago">]

Reconstruct the formatted string

parts.map(&:value).join  #=> "3 days ago"

Parameters:

  • value (Integer)

    the relative time value (negative for past, positive for future)

  • unit (Symbol)

    time unit: ‘:second`, `:minute`, `:hour`, `:day`, `:week`, `:month`, `:quarter`, or `:year`

Returns:



708
# File 'lib/icu4x/yard_docs.rb', line 708

def format_to_parts(value, unit); end

#resolved_optionsHash

Returns the resolved options for this instance.

Returns:

  • (Hash)

    options hash with keys:

    • ‘:locale` [String] the resolved locale identifier

    • ‘:style` [Symbol] the format style

    • ‘:numeric` [Symbol] the numeric display mode



717
# File 'lib/icu4x/yard_docs.rb', line 717

def resolved_options; end