Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap translate defaults to use translate helper features #6032

Merged
merged 1 commit into from
Apr 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module TranslationHelper
# you know what kind of output to expect when you call translate in a template.
def translate(key, options = {})
options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
options[:default] = wrap_translate_defaults(options[:default]) if options[:default]
if html_safe_translation_key?(key)
html_safe_options = options.dup
options.except(*I18n::RESERVED_KEYS).each do |name, value|
Expand Down Expand Up @@ -83,6 +84,21 @@ def scope_key_by_partial(key)
def html_safe_translation_key?(key)
key.to_s =~ /(\b|_|\.)html$/
end

def wrap_translate_defaults(defaults)
new_defaults = []
defaults = Array(defaults)
while key = defaults.shift
if key.is_a?(Symbol)
new_defaults << lambda { |_, options| translate key, options.merge(:default => defaults) }
break
else
new_defautls << key
end
end

new_defaults
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= t('.missing', :default => :'.foo') %>
25 changes: 24 additions & 1 deletion actionpack/test/template/translation_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def setup
:translations => {
:templates => {
:found => { :foo => 'Foo' },
:array => { :foo => { :bar => 'Foo Bar' } }
:array => { :foo => { :bar => 'Foo Bar' } },
:default => { :foo => 'Foo' }
},
:foo => 'Foo',
:hello => '<a>Hello World</a>',
Expand Down Expand Up @@ -71,6 +72,10 @@ def test_finds_array_of_translations_scoped_by_partial
assert_equal 'Foo Bar', @view.render(:file => 'translations/templates/array').strip
end

def test_default_lookup_scoped_by_partial
assert_equal 'Foo', view.render(:file => 'translations/templates/default').strip
end

def test_missing_translation_scoped_by_partial
expected = '<span class="translation_missing" title="translation missing: en.translations.templates.missing.missing">Missing</span>'
assert_equal expected, view.render(:file => 'translations/templates/missing').strip
Expand Down Expand Up @@ -102,4 +107,22 @@ def test_translate_with_html_count
def test_translation_returning_an_array_ignores_html_suffix
assert_equal ["foo", "bar"], translate(:'translations.array_html')
end

def test_translate_with_default_named_html
translation = translate(:'translations.missing', :default => :'translations.hello_html')
assert_equal '<a>Hello World</a>', translation
assert translation.html_safe?
end

def test_translate_with_two_defaults_named_html
translation = translate(:'translations.missing', :default => [:'translations.missing_html', :'translations.hello_html'])
assert_equal '<a>Hello World</a>', translation
assert translation.html_safe?
end

def test_translate_with_last_default_named_html
translation = translate(:'translations.missing', :default => [:'translations.missing', :'translations.hello_html'])
assert_equal '<a>Hello World</a>', translation
assert translation.html_safe?
end
end