Skip to content
This repository
Browse code

Implemented lambda support for #translate.

  • Loading branch information...
commit 1113c6068a44e3aa70b3733d52ce3212d573503e 1 parent bde2b62
Clemens Kofler clemens authored committed

Showing 2 changed files with 42 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  lib/i18n/backend/simple.rb
  2. +41 0 test/simple_backend_test.rb
1  lib/i18n/backend/simple.rb
@@ -31,6 +31,7 @@ def translate(locale, key, options = {})
31 31 values = options.reject { |name, value| reserved.include?(name) }
32 32
33 33 entry = lookup(locale, key, scope) || resolve(locale, key, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
  34 + entry = entry.call(values) if entry.is_a? Proc
34 35 entry = pluralize(locale, entry, count)
35 36 entry = interpolate(locale, entry, values)
36 37 entry
41 test/simple_backend_test.rb
@@ -196,6 +196,47 @@ def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_da
196 196 end
197 197 end
198 198
  199 +class I18nSimpleBackendTranslateLambdaTest < Test::Unit::TestCase
  200 + include I18nSimpleBackendTestSetup
  201 +
  202 + def test_translate_simple_proc
  203 + setup_proc_translation
  204 + assert_equal 'bar=bar, baz=baz, foo=foo', @backend.translate('en', :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
  205 + end
  206 +
  207 + def test_translate_proc_in_defaults
  208 + setup_proc_translation
  209 + assert_equal 'bar=bar, baz=baz, foo=foo', @backend.translate('en', :does_not_exist, :default => :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
  210 + assert_equal 'bar=bar, baz=baz, foo=foo', @backend.translate('en', :does_not_exist, :default => [:does_not_exist_2, :does_not_exist_3, :a_lambda], :foo => 'foo', :bar => 'bar', :baz => 'baz')
  211 + end
  212 +
  213 + def test_translate_proc_with_pluralize
  214 + setup_proc_translation
  215 + params = { :zero => 'zero', :one => 'one', :other => 'other' }
  216 + assert_equal 'zero', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 0))
  217 + assert_equal 'one', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 1))
  218 + assert_equal 'other', @backend.translate('en', :lambda_for_pluralize, params.merge(:count => 2))
  219 + end
  220 +
  221 + def test_translate_proc_with_interpolate
  222 + setup_proc_translation
  223 + assert_equal 'bar baz foo', @backend.translate('en', :lambda_for_interpolate, :foo => 'foo', :bar => 'bar', :baz => 'baz')
  224 + end
  225 +
  226 + private
  227 + def setup_proc_translation
  228 + @backend.store_translations 'en', {
  229 + :a_lambda => lambda { |attributes|
  230 + attributes.keys.sort_by(&:to_s).collect { |key| "#{key}=#{attributes[key]}"}.join(', ')
  231 + },
  232 + :lambda_for_pluralize => lambda { |attributes| attributes },
  233 + :lambda_for_interpolate => lambda { |attributes|
  234 + "{{#{attributes.keys.sort_by(&:to_s).join('}} {{')}}}"
  235 + }
  236 + }
  237 + end
  238 +end
  239 +
199 240 class I18nSimpleBackendLookupTest < Test::Unit::TestCase
200 241 include I18nSimpleBackendTestSetup
201 242

0 comments on commit 1113c60

Please sign in to comment.
Something went wrong with that request. Please try again.