Class: ICU4X::ListFormat

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

Overview

Formats lists of items according to locale-specific conventions.

Examples:

Conjunction (and)

formatter = ICU4X::ListFormat.new(locale, type: :conjunction)
formatter.format(["A", "B", "C"])  #=> "A, B, and C"

Disjunction (or)

formatter = ICU4X::ListFormat.new(locale, type: :disjunction)
formatter.format(["A", "B", "C"])  #=> "A, B, or C"

Unit list

formatter = ICU4X::ListFormat.new(locale, type: :unit)
formatter.format(["5 lb", "12 oz"])  #=> "5 lb, 12 oz"

Instance Method Summary collapse

Constructor Details

#initialize(locale, provider: nil, type: :conjunction, style: :long) ⇒ ListFormat

Creates a new ListFormat instance.

Parameters:

  • locale (Locale)

    the locale for formatting

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

    data provider (uses default if nil)

  • type (Symbol) (defaults to: :conjunction)

    list type: ‘:conjunction`, `:disjunction`, or `:unit`

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

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

Raises:

  • (DataError)

    if data for the locale is unavailable



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

def initialize(locale, provider: nil, type: :conjunction, style: :long); end

Instance Method Details

#format(list) ⇒ String

Formats a list of strings.

Parameters:

  • list (Array<String>)

    the list items to format

Returns:

  • (String)

    the formatted list string



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

def format(list); end

#format_to_parts(list) ⇒ Array<FormattedPart>

Formats a list of strings and returns an array of parts.

Each part contains a type and value, allowing for custom styling or processing of individual components.

Examples:

parts = formatter.format_to_parts(["Apple", "Banana", "Cherry"])
# => [
#   #<ICU4X::FormattedPart type=:element value="Apple">,
#   #<ICU4X::FormattedPart type=:literal value=", ">,
#   #<ICU4X::FormattedPart type=:element value="Banana">,
#   #<ICU4X::FormattedPart type=:literal value=", and ">,
#   #<ICU4X::FormattedPart type=:element value="Cherry">
# ]

Reconstruct the formatted string

parts.map(&:value).join  #=> "Apple, Banana, and Cherry"

Parameters:

  • list (Array<String>)

    the list items to format

Returns:



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

def format_to_parts(list); end

#resolved_optionsHash

Returns the resolved options for this instance.

Returns:

  • (Hash)

    options hash with keys:

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

    • ‘:type` [Symbol] the list type

    • ‘:style` [Symbol] the format style



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

def resolved_options; end