Sha256: 3400c5a44b019b307f62e07135544adae08045503d686d5c918988e03492a476

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "i18n/backend/base"
require "i18n/backend/flatten"

module Decidim
  module TermCustomizer
    class I18nBackend
      using I18n::HashRefinements

      (class << self; self; end).class_eval { public :include }

      module Implementation
        include I18n::Backend::Base

        # Get available locales from the translations hash
        def available_locales
          Translation.available_locales
        rescue ::ActiveRecord::StatementInvalid
          []
        end

        def initialized?
          !@translations.nil?
        end

        # Clean up translations hash on reload!
        def reload!
          @translations = nil
          super
        end

        def translations
          return @translations if @translations
          return {} unless TermCustomizer.loader

          @translations = TermCustomizer.loader.translations_hash
        end

        protected

        # Looks up a translation from the translations hash. Returns nil if
        # either key is nil, or locale, scope or key do not exist as a key in
        # the nested translations hash. Splits keys or scopes containing dots
        # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same
        # as <tt>%w(currency format)</tt>.
        def lookup(locale, key, scope = [], options = EMPTY_HASH)
          keys = I18n.normalize_keys(locale, key, scope, options[:separator])

          keys.inject(translations) do |result, inner_key|
            return nil unless result.is_a?(Hash)

            unless result.has_key?(inner_key)
              inner_key = inner_key.to_s.to_sym
              return nil unless result.has_key?(inner_key)
            end
            result = result[inner_key]
            result = resolve(locale, inner_key, result, options.merge(scope: nil)) if result.is_a?(Symbol)
            result
          end
        end
      end

      include Implementation
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
decidim-term_customizer-0.23.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.22.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.21.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.20.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.19.1 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.19.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.18.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.17.1 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.17.0 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.16.6 lib/decidim/term_customizer/i18n_backend.rb
decidim-term_customizer-0.16.5 lib/decidim/term_customizer/i18n_backend.rb