Class: CopyTunerClient::TranslationLog

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/translation_log.rb

Class Method Summary collapse

Class Method Details

.add(key, result) ⇒ Object



15
16
17
# File 'lib/copy_tuner_client/translation_log.rb', line 15

def self.add(key, result)
  translations[key] = result if initialized? && !translations.key?(key)
end

.clearObject



7
8
9
# File 'lib/copy_tuner_client/translation_log.rb', line 7

def self.clear
  Thread.current[:translations] = {}
end

.initialized?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/copy_tuner_client/translation_log.rb', line 11

def self.initialized?
  !Thread.current[:translations].nil?
end

.install_hookObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/copy_tuner_client/translation_log.rb', line 19

def self.install_hook
  I18n.class_eval do
    class << self
      def translate_with_copy_tuner_hook(*args)
        key = args[0]
        options  = args.last.is_a?(Hash) ? args.last : {}
        scope = options[:scope]
        scope = scope.dup if scope.is_a?(Array) || scope.is_a?(String)
        result = translate_without_copy_tuner_hook(*args)

        if key.is_a?(Array)
          key.zip(result).each { |k, v| CopyTunerClient::TranslationLog.add(I18n.normalize_keys(nil, k, scope).join('.'), v) unless v.is_a?(Array) }
        else
          CopyTunerClient::TranslationLog.add(I18n.normalize_keys(nil, key, scope).join('.'), result) unless result.is_a?(Array)
        end
        result
      end
      if CopyTunerClient.configuration.enable_middleware?
        alias_method :translate_without_copy_tuner_hook, :translate
        alias_method :translate, :translate_with_copy_tuner_hook
        alias :t :translate
      end
    end
  end
end

.translationsObject



3
4
5
# File 'lib/copy_tuner_client/translation_log.rb', line 3

def self.translations
  Thread.current[:translations]
end