Sha256: 988380943331d6ced163c51aa91cbc9845fe56ca1abe9c8c4cf54efc4f7964bb

Contents?: true

Size: 1.23 KB

Versions: 14

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'set'
module I18n::Tasks::PluralKeys
  PLURAL_KEY_SUFFIXES = Set.new %w[zero one two few many other]
  PLURAL_KEY_RE = /\.(?:#{PLURAL_KEY_SUFFIXES.to_a * '|'})$/

  def collapse_plural_nodes!(tree)
    tree.leaves.map(&:parent).compact.uniq.each do |node|
      children = node.children
      next unless plural_forms?(children)
      node.value    = children.to_hash
      node.children = nil
      node.data.merge! children.first.data
    end
    tree
  end

  # @param [String] key i18n key
  # @param [String] locale to pull key data from
  # @return [String] the base form if the key is a specific plural form (e.g. apple for apple.many), the key otherwise.
  def depluralize_key(key, locale = base_locale)
    return key if key !~ PLURAL_KEY_RE
    key_name = last_key_part(key)
    parent_key = key[0..- (key_name.length + 2)]
    nodes = tree("#{locale}.#{parent_key}").presence || (locale != base_locale && tree("#{base_locale}.#{parent_key}"))
    if nodes && plural_forms?(nodes)
      parent_key
    else
      key
    end
  end

  def plural_forms?(s)
    s.present? && s.all? { |node| node.leaf? && plural_suffix?(node.key) }
  end

  def plural_suffix?(key)
    PLURAL_KEY_SUFFIXES.include?(key)
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
i18n-tasks-0.9.27 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.26 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.25 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.24 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.23 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.22 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.21 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.20 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.19 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.18 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.17 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.16 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.15 lib/i18n/tasks/plural_keys.rb
i18n-tasks-0.9.14 lib/i18n/tasks/plural_keys.rb