Class: Foxtail::Function::Number

Inherits:
Value
  • Object
show all
Defined in:
lib/foxtail/function/number.rb

Overview

Wraps a numeric value with formatting options The raw value is preserved for selector matching (plural rules)

Instance Attribute Summary

Attributes inherited from Value

#options, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#to_s

Class Method Details

.convert_options(options) ⇒ Hash

Convert FTL/JS style number options to ICU4X options

Parameters:

  • options (Hash)

    FTL/JS style options (camelCase)

Returns:

  • (Hash)

    ICU4X style options (snake_case with symbols)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/foxtail/function/number.rb', line 11

def self.convert_options(options)
  result = {}

  options.each do |key, value|
    case key
    when :style
      result[:style] = value.to_sym
    when :currency
      result[:currency] = value.to_s
    when :minimumIntegerDigits
      result[:minimum_integer_digits] = Integer(value)
    when :minimumFractionDigits
      result[:minimum_fraction_digits] = Integer(value)
    when :maximumFractionDigits
      result[:maximum_fraction_digits] = Integer(value)
    when :useGrouping
      result[:use_grouping] = value
    else
      warn "Unknown NUMBER option: #{key}"
    end
  end

  result
end

Instance Method Details

#format(bundle:) ⇒ String

Format the number using ICU4X

Parameters:

Returns:

  • (String)

    The formatted number



39
40
41
42
# File 'lib/foxtail/function/number.rb', line 39

def format(bundle:)
  icu_options = self.class.convert_options(options)
  ICU4XCache.instance.number_formatter(bundle.locale, **icu_options).format(value)
end