class SgtnClient::Formatters::PluralFormatter

Attributes

locale[R]

Public Class Methods

new(locale = TwitterCldr.locale) click to toggle source
# File lib/sgtn-client/formatters/plurals/plural_formatter.rb, line 12
def initialize(locale = TwitterCldr.locale)
  @locale = TwitterCldr.convert_locale(locale)
end

Public Instance Methods

num_s(string, replacements) click to toggle source
# File lib/sgtn-client/formatters/plurals/plural_formatter.rb, line 16
def num_s(string, replacements)
    reg = Regexp.union(
      /%<(\{.*?\})>/
    )
    string.gsub(reg) do
      count_placeholder, patterns = if $1
        pluralization_hash = JSON.parse($1)
        if pluralization_hash.is_a?(Hash) && pluralization_hash.size == 1
          pluralization_hash.first
        else
          raise ArgumentError.new('expected a Hash with a single key')
        end
      else
        raise ArgumentError.new('invalide format')
      end
      count  = replacements[count_placeholder.to_sym].to_s
      if patterns.is_a?(Hash)
        return TwitterCldr::Utils.deep_symbolize_keys(patterns)[count.to_sym]
      else
        raise ArgumentError.new('expected patterns to be a Hash')
      end
    end
  end