Sha256: 9a3374230ba89d9b64206f5fd18af4429e97882bc792fa9cd64fb20f81ede5f7

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require_relative "pseudolocalization/version"
require_relative "pseudolocalization/pseudolocalizer"

module Pseudolocalization
  module I18n
    class Backend
      attr_reader :original_backend
      attr_accessor :ignores

      def initialize(original_backend)
        @original_backend = original_backend
        @ignores = []
        yield self if block_given?
      end

      def method_missing(name, *args, &block)
        if respond_to_missing?(name)
          original_backend.public_send(name, *args, &block)
        else
          super
        end
      end

      def respond_to_missing?(name, include_private = false)
        original_backend.respond_to?(name) || super
      end

      def translate(locale, key, options)
        return original_backend.translate(locale, key, options) if key_ignored?(key)

        ::Pseudolocalization::I18n::Pseudolocalizer.pseudolocalize(original_backend.translate(locale, key, options))
      end

      private

      def key_ignored?(key)
        return false unless ignores

        ignores.any? do |ignore|
          case ignore
          when Regexp
            key.to_s.match(ignore)
          when String
            File.fnmatch(ignore, key.to_s)
          else
            Rails.logger.tagged('Pseudolocalization I18n').error('Ignore type unsupported. Expects an array of (mixed) Regexp or Strings.')
            false
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pseudolocalization-0.9.0 lib/pseudolocalization.rb