the translate liquid filter handles pluralization (fix issue locomotivecms/steam#50)

did committed Dec 28, 2015
commit 4689dd415064c6634541f41e1dc93779a358741a
Showing 3 changed files with 74 additions and 8 deletions
locomotive/steam/services/translator_service.rb b/lib/locomotive/steam/services/translator_service.rb +11 -0
@@ @@ -17,6 +17,8 @@ module Locomotive
scope = options.delete('scope')
if scope.blank?
+ input = "#{input}_#{pluralize_prefix(options['count'])}" if options['count']
+
values = repository.by_key(input).try(:values) || {}
# FIXME: important to check if the returned value is nil (instead of nil + false)
@@ @@ -39,6 +41,15 @@ module Locomotive
::Liquid::Template.parse(string).render(options)
end
+ def pluralize_prefix(count)
+ case count.to_i
+ when 0 then 'zero'
+ when 1 then 'one'
+ when 2 then 'two'
+ else 'other'
+ end
+ end
+
end
end
spec/integration/liquid/filters/translate_spec.rb +15 -8
@@ @@ -10,16 +10,13 @@ describe Locomotive::Steam::Liquid::Filters::Translate do
let(:assigns) { {} }
let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) }
- before {
- services.locale = :en
- allow(translator).to receive(:translate).and_return(translation)
- }
+ before { services.locale = :en }
subject { render_template(source, context) }
context 'missing translation' do
- let(:translation) { nil }
+ before { allow(translator).to receive(:translate).and_return(nil) }
it { is_expected.to eq 'welcome_message' }
@@ @@ -27,15 +24,15 @@ describe Locomotive::Steam::Liquid::Filters::Translate do
context 'existing translation' do
- let(:translation) { 'Hello world' }
+ before { allow(translator).to receive(:translate).and_return('Hello world') }
it { is_expected.to eq 'Hello world' }
end
- context 'passing a locale and a scope' do
+ describe 'passing a locale and a scope' do
- let(:translation) { 'Bonjour monde' }
+ before { allow(translator).to receive(:translate).and_return('Bonjour monde') }
describe 'legacy syntax' do
@@ @@ -53,4 +50,14 @@ describe Locomotive::Steam::Liquid::Filters::Translate do
end
+ describe 'pluralization' do
+
+ let(:translation) { instance_double('Translation', values: { 'en' => '{{ name }} has {{ count }} articles' }) }
+ before { expect(translator.repository).to receive(:by_key).with('post_count_two').and_return(translation) }
+
+ let(:source) { "{{ 'post_count' | translate: count: 2, name: 'John' }}" }
+ it { expect(subject).to eq('John has 2 articles') }
+
+ end
+
end
spec/unit/services/translator_service_spec.rb +48 -0
@@ @@ -61,6 +61,54 @@ describe Locomotive::Steam::TranslatorService do
end
+ describe 'pluralization' do
+
+ context 'zero' do
+
+ let(:interpolation) { { 'count' => '0' } }
+ let(:translation) { instance_double('Translation', values: { 'en' => 'No posts' }) }
+
+ before { expect(repository).to receive(:by_key).with('example_test_zero').and_return(translation) }
+
+ it { is_expected.to eq 'No posts' }
+
+ end
+
+ context 'one' do
+
+ let(:interpolation) { { 'count' => '1' } }
+ let(:translation) { instance_double('Translation', values: { 'en' => '1 post' }) }
+
+ before { expect(repository).to receive(:by_key).with('example_test_one').and_return(translation) }
+
+ it { is_expected.to eq '1 post' }
+
+ end
+
+ context 'two' do
+
+ let(:interpolation) { { 'count' => 2 } }
+ let(:translation) { instance_double('Translation', values: { 'en' => '2 posts' }) }
+
+ before { expect(repository).to receive(:by_key).with('example_test_two').and_return(translation) }
+
+ it { is_expected.to eq '2 posts' }
+
+ end
+
+ context 'other' do
+
+ let(:interpolation) { { 'count' => 42 } }
+ let(:translation) { instance_double('Translation', values: { 'en' => '{{ count }} posts' }) }
+
+ before { expect(repository).to receive(:by_key).with('example_test_other').and_return(translation) }
+
+ it { is_expected.to eq '42 posts' }
+
+ end
+
+ end
+
end
describe 'missing translation' do