Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/sml/translator
Browse files Browse the repository at this point in the history
  • Loading branch information
graysky committed Mar 17, 2009
2 parents 44ecbcc + d21fca4 commit d7d8bed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ class TranslatorError < StandardError #:nodoc:
# Used as a visible marker. Default is "]]"
@@pseudo_append = " ]]"

def self.missing_translation_callback(exception, key, options = {})
@@missing_translation_callback.call(exception, key, options) if !@@missing_translation_callback.nil?
end

# Set an optional block that gets called when there's a missing translation.
# Block takes two required parameters:
# exception (original exception that was raised for the failed translation)
# key (key that was missing)
# options (hash of options sent to translator)
# Example:
# set_missing_translation_callback do |ex, key, options|
# logger.info("Failed to find #{key}")
# end
def self.set_missing_translation_callback(&block)
@@missing_translation_callback = block
end

# Performs lookup with a given scope. The scope should be an array of strings or symbols
# ordered from highest to lowest scoping. For example, for a given PicturesController
# with an action "show" the scope should be ['pictures', 'show'] which happens automatically.
Expand Down Expand Up @@ -234,11 +251,15 @@ def translate_with_context(key, options={})
# Call the original translate method
str = translate_without_context(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>
#
raise if Translator.strict_mode? && str =~ /span class\=\"translation_missing\"/

if str =~ /span class\=\"translation_missing\"/
# In strict mode, do not allow TranslationHelper to add "translation missing"
raise if Translator.strict_mode?
Translator.missing_translation_callback(exc, key, options)
end

str
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/translator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,24 @@ def test_fallback
assert_equal I18n.translate('blog_posts.index.title'), assigns(:page_title)
end

# Test that we can set up a callback for missing translations
def test_missing_translation_callback
test_exception = nil
test_key = nil
test_options = nil

Translator.set_missing_translation_callback do |ex, key, options|
test_exception = ex
test_key = key
test_options = options
end

get :missing_translation
assert_response :success
assert_equal "missing_string", test_key
assert_not_nil test_options
assert_not_nil test_exception
end


end

0 comments on commit d7d8bed

Please sign in to comment.