Class: Foxtail::Function::DateTime

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

Overview

Wraps a datetime value with formatting options The raw value is preserved for selector matching

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 datetime 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
# File 'lib/foxtail/function/datetime.rb', line 11

def self.convert_options(options)
  result = {}

  options.each do |key, value|
    case key
    when :dateStyle
      result[:date_style] = value.to_sym
    when :timeStyle
      result[:time_style] = value.to_sym
    when :timeZone
      result[:time_zone] = value.to_s
    else
      warn "Unknown DATETIME option: #{key}"
    end
  end

  result
end

Instance Method Details

#format(bundle:) ⇒ String

Format the datetime using ICU4X

Parameters:

Returns:

  • (String)

    The formatted datetime



33
34
35
36
# File 'lib/foxtail/function/datetime.rb', line 33

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