Class: Foxtail::ICU4XCache

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache
Includes:
Singleton
Defined in:
lib/foxtail/icu4x_cache.rb

Overview

Singleton cache for ICU4X formatter and rules instances.

ICU4X formatters and rules internally load and parse locale data, making instance creation non-trivial. This cache stores instances keyed by locale and options to avoid repeated instantiation.

Thread safety is provided by Dry::Core::Cache, which uses Concurrent::Map internally.

Examples:

cache = Foxtail::ICU4XCache.instance
formatter = cache.number_formatter(locale)
formatter.format(1234)  #=> "1,234"

Instance Method Summary collapse

Instance Method Details

#datetime_formatter(locale, **options) ⇒ ICU4X::DateTimeFormat

Returns a cached ICU4X::DateTimeFormat instance.

Parameters:

  • locale (ICU4X::Locale)

    The locale for formatting

  • options (Hash)

    Formatting options passed to ICU4X::DateTimeFormat.new

Returns:

  • (ICU4X::DateTimeFormat)

    Cached formatter instance



40
41
42
43
44
# File 'lib/foxtail/icu4x_cache.rb', line 40

def datetime_formatter(locale, **options)
  self.class.fetch_or_store(:datetime_formatter, locale, options) do
    ICU4X::DateTimeFormat.new(locale, **options)
  end
end

#number_formatter(locale, **options) ⇒ ICU4X::NumberFormat

Returns a cached ICU4X::NumberFormat instance.

Parameters:

  • locale (ICU4X::Locale)

    The locale for formatting

  • options (Hash)

    Formatting options passed to ICU4X::NumberFormat.new

Returns:

  • (ICU4X::NumberFormat)

    Cached formatter instance



29
30
31
32
33
# File 'lib/foxtail/icu4x_cache.rb', line 29

def number_formatter(locale, **options)
  self.class.fetch_or_store(:number_formatter, locale, options) do
    ICU4X::NumberFormat.new(locale, **options)
  end
end

#plural_rules(locale, type: :cardinal) ⇒ ICU4X::PluralRules

Returns a cached ICU4X::PluralRules instance.

Parameters:

  • locale (ICU4X::Locale)

    The locale for plural rules

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

    Plural rule type (:cardinal or :ordinal)

Returns:

  • (ICU4X::PluralRules)

    Cached rules instance



51
52
53
54
55
# File 'lib/foxtail/icu4x_cache.rb', line 51

def plural_rules(locale, type: :cardinal)
  self.class.fetch_or_store(:plural_rules, locale, type) do
    ICU4X::PluralRules.new(locale, type:)
  end
end