Sha256: 14e3302773a6fe5f1c698216763f2a2683c4787d07e41add0e21d3133b0f1310

Contents?: true

Size: 872 Bytes

Versions: 2

Compression:

Stored size: 872 Bytes

Contents

# frozen_string_literal: true

module Localer
  # Parse translations into hash:
  # key: translation key
  # value: hash of locale values
  class Parser < Service
    param :translations
    param :config, default: -> { Localer.config }

    attr_reader :data, :locales

    def call
      @data = {}
      @locales = []
      @translations.each do |(locale, translation)|
        @locales.push locale
        compare(locale, translation)
      end

      self
    end

    private

    def compare(locale, translation, prefix = "")
      if translation.is_a?(Hash)
        translation.each do |(key, value)|
          full_key = prefix + ".#{key}"
          next if full_key.start_with?(*config.exclude)
          compare(locale, value, full_key)
        end
      else
        @data[prefix] ||= {}
        @data[prefix][locale] = translation
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
localer-0.0.2 lib/localer/parser.rb
localer-0.0.1 lib/localer/parser.rb