Skip to content

Commit

Permalink
Merge pull request #126 from danielveleba/master
Browse files Browse the repository at this point in the history
Fix typo in #pluralisation_rule for db repo
  • Loading branch information
grosser committed May 26, 2020
2 parents 9a701e3 + 8a0fbc3 commit 412490f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/fast_gettext/translation_repository/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def available_locales
end

def pluralisation_rule
@model.pluralsation_rule if @model.respond_to? :pluralsation_rule
@model.pluralisation_rule if @model.respond_to? :pluralisation_rule
end

def [](key)
Expand Down
24 changes: 22 additions & 2 deletions spec/fast_gettext/translation_repository/db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
@rep = FastGettext::TranslationRepository::Db.new('x', :model=>TranslationKey)
end

def create_translation(key, text)
def create_translation(key, text, locale = "de")
translation_key = TranslationKey.create!(:key => key)
TranslationText.create!(:translation_key_id => translation_key.id, :text => text, :locale => "de")
TranslationText.create!(:translation_key_id => translation_key.id, :text => text, :locale => locale)
end

it "reads locales from the db" do
Expand Down Expand Up @@ -71,6 +71,26 @@ def create_translation(key, text)
@rep.plural('Axis','Axis').should == ['Achse','Achsen']
end

it 'can pluralize with rule on model' do
class TranslationKey < ActiveRecord::Base
def self.pluralisation_rule
case FastGettext.locale
when 'en'
->(n) { (n == 1) ? 0 : 1 }
when 'cz'
->(n) { (n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2; }
else
nil
end
end
end

FastGettext.locale = 'cz'
create_translation 'Chicken||||Chicken', 'Kuře||||Kuřata||||Kuřat', "cz"
translations = @rep.plural('Chicken','Chicken')
translations[@rep.pluralisation_rule.call(5)].should == 'Kuřat'
end

it "can reload" do
@rep.reload.should == true
end
Expand Down

0 comments on commit 412490f

Please sign in to comment.