Permalink
Browse files

Fix regression with direct count interpolations

* Rails uses translations like "must be greater than %{count}"
* These translations accept a count but don't pluralize
* Commit 1f4bb18 (#95) introduced a regression
* These unpluralized translations would return the original key
  • Loading branch information...
1 parent aff504b commit 18f816a7d9653ff7e532cb402b3b9378e66b30e7 @jferris jferris committed Aug 7, 2013
Showing with 25 additions and 5 deletions.
  1. +17 −5 lib/gettext_i18n_rails/backend.rb
  2. +8 −0 spec/gettext_i18n_rails/backend_spec.rb
@@ -17,11 +17,8 @@ def available_locales
def translate(locale, key, options)
if gettext_key = gettext_key(key, options)
- translation = if options[:count]
- FastGettext.n_(gettext_key, options[:count])
- else
- FastGettext._(gettext_key)
- end
+ translation =
+ plural_translate(gettext_key, options) || FastGettext._(gettext_key)
interpolate(translation, options)
else
result = backend.translate(locale, key, options)
@@ -52,6 +49,21 @@ def gettext_key(key, options)
end
end
+ def plural_translate(gettext_key, options)
+ if options[:count]
+ translation = FastGettext.n_(gettext_key, options[:count])
+ discard_pass_through_key gettext_key, translation
+ end
+ end
+
+ def discard_pass_through_key(key, translation)
+ if translation == key
+ nil
+ else
+ translation
+ end
+ end
+
def interpolate(string, values)
if string.respond_to?(:%)
reserved_keys = if defined?(I18n::RESERVED_KEYS) # rails 3+
@@ -50,6 +50,14 @@
subject.translate('xx', 'ab.e', :count => 2).should == 'plural'
end
+ it "interpolates a count without plural translations" do
+ repo = {'ab.e' => 'existing %{count}'}
+ repo.should_receive(:plural).and_return []
+ repo.stub(:pluralisation_rule).and_return lambda { |i| true }
+ FastGettext.stub(:current_repository).and_return repo
+ subject.translate('xx', 'ab.e', :count => 1).should == 'existing 1'
+ end
+
it "can translate with gettext using symbols" do
FastGettext.should_receive(:current_repository).and_return 'xy.z.v'=>'a'
subject.translate('xx',:v ,:scope=>['xy','z']).should == 'a'

0 comments on commit 18f816a

Please sign in to comment.