Class: I18n::TranslationSpawner

Inherits:
Object
  • Object
show all
Includes:
LambdaAccessor
Defined in:
lib/i18n_translation_spawner.rb

Defined Under Namespace

Classes: CannotDecodeTranslationFilePath

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cannot_decode_translation_file_path_handlerObject

Returns the value of attribute cannot_decode_translation_file_path_handler.



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

def cannot_decode_translation_file_path_handler
  @cannot_decode_translation_file_path_handler
end

#default_translationsObject

Returns the value of attribute default_translations.



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

def default_translations
  @default_translations
end

#exception_handlerObject

Returns the value of attribute exception_handler.



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

def exception_handler
  @exception_handler
end

#removable_prefixesObject

Returns the value of attribute removable_prefixes.



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

def removable_prefixes
  @removable_prefixes
end

#skip_localesObject

Returns the value of attribute skip_locales.



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

def skip_locales
  @skip_locales
end

#translations_handlerObject

Returns the value of attribute translations_handler.



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

def translations_handler
  @translations_handler
end

Instance Method Details

#call(exception, locale, key, options) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/i18n_translation_spawner.rb', line 98

def call(exception, locale, key, options)
  if exception_handler.respond_to?(:call)
    exception_handler.call(exception, locale, key, self, options)
  else
    handle_exception(exception, locale, key, options)
  end
end

#default_file_path_decoder_handler(*args) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/i18n_translation_spawner.rb', line 64

def default_file_path_decoder_handler(*args)
  key, locale = args
  if File.file?(path = File.join(Rails.root, "config/locales", "#{locale.to_s}.yml"))
    path
  else
    raise CannotDecodeTranslationFilePath
  end
end

#default_translation(key, locale) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/i18n_translation_spawner.rb', line 73

def default_translation(key, locale)
  if translations_handler.respond_to?(:call)
    translations_handler(key, locale, self)
  else
    _key = key.to_s.dup
    while _key.present?
      if (val = default_translations[_key]).present?
        if val.is_a?(Hash)
          return(val[locale].present? ? val[locale] : translation_for_key(key, locale))
        else
          return(val.to_s)
        end
      else
        _key.sub!(/([a-zA-Z0-9_]*\.?)/, '')
      end
    end
    translation_for_key(key, locale)
  end
end

#default_translation_for_key_handler(*args) ⇒ Object



93
94
95
96
# File 'lib/i18n_translation_spawner.rb', line 93

def default_translation_for_key_handler(*args)
  key, locale = args
  key.to_s.split('.').last.sub(/\A#{removable_prefixes.map { |prefix| prefix+'_' }.join('|')}/, '').humanize
end

#handle_exception(exception, locale, key, options) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/i18n_translation_spawner.rb', line 106

def handle_exception(exception, locale, key, options)
  case exception
    when I18n::MissingTranslationData
      spawn_translation_key(key, locale, options, exception)
    else
      raise exception
  end
end