Class: ActionView::Base

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

Instance Method Summary collapse

Instance Method Details

#translate_with_annotation(key, options = {}) ⇒ Object

Redefine the translate method in ActionView (contributed by TranslationHelper) that is context-aware of what view (or partial) is being rendered. Initial scoping will be scoped to [:controller_name :view_name]



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/annotranslate.rb', line 284

def translate_with_annotation(key, options={})
  # default to an empty scope
  scope = []

  # In the case of a missing translation, fall back to letting TranslationHelper
  # put in span tag for a translation_missing.
  begin
    AnnoTranslate.translate_with_annotation(scope, @virtual_path, key, options.merge({:raise => true}))
  rescue AnnoTranslate::AnnoTranslateError, I18n::MissingTranslationData => exc
    # Call the original translate method
    str = translate_without_annotation(key, options)

    # View helper adds the translation missing span like:
    # In strict mode, do not allow TranslationHelper to add "translation missing" span like:
    # <span class="translation_missing">en, missing_string</span>
    if str =~ /span class\=\"translation_missing\"/
      # In strict mode, do not allow TranslationHelper to add "translation missing"
      raise if AnnoTranslate.strict_mode?

      # Invoke callback if it is defined
      AnnoTranslate.missing_translation_callback(exc, key, options)
    end

    str
  end
end