change the syntax of the translate liquid filter (but keep the old syntax too)

did committed Dec 27, 2015
commit 662667fdf92d6d8b96e59c57a14ef74f98890106
Showing 4 changed files with 69 additions and 7 deletions
locomotive/steam/liquid/filters/translate.rb b/lib/locomotive/steam/liquid/filters/translate.rb +8 -2
@@ @@ -5,8 +5,14 @@ module Locomotive
module Translate
- def translate(input, locale = nil, scope = nil)
- @context.registers[:services].translator.translate(input, locale, scope) || input
+ def translate(input, options = nil, legacy_scope = nil)
+ options ||= {}
+
+ unless options.respond_to?(:values) # String
+ options = { 'locale' => options, 'scope' => legacy_scope }
+ end
+
+ @context.registers[:services].translator.translate(input, options) || input
end
end
locomotive/steam/services/translator_service.rb b/lib/locomotive/steam/services/translator_service.rb +4 -4
@@ @@ -8,13 +8,13 @@ module Locomotive
# Return the translation described by a key.
#
# @param [ String ] key The key of the translation.
- # @param [ String ] locale The locale we want the translation in
- # @param [ String ] scope If specified, instead of looking in the translations, it will use I18n instead.
+ # @param [ Hash ] options This includes the following options: count, locale (The locale we want the translation in), scope (If specified, instead of looking in the translations, it will use I18n instead)
#
# @return [ String ] the translated text or nil if not found
#
- def translate(input, locale, scope = nil)
- locale ||= self.current_locale
+ def translate(input, options = {})
+ locale = options['locale'] || self.current_locale
+ scope = options['scope']
if scope.blank?
values = repository.by_key(input).try(:values) || {}
spec/integration/liquid/filters/translate_spec.rb +56 -0
@@ @@ -0,0 +1,56 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Liquid::Filters::Translate do
+
+ let(:source) { "{{ 'welcome_message' | translate }}" }
+
+ let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) }
+ let(:services) { Locomotive::Steam::Services.build_instance }
+ let(:translator) { services.translator }
+ let(:assigns) { {} }
+ let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) }
+
+ before {
+ services.locale = :en
+ allow(translator).to receive(:translate).and_return(translation)
+ }
+
+ subject { render_template(source, context) }
+
+ context 'missing translation' do
+
+ let(:translation) { nil }
+
+ it { is_expected.to eq 'welcome_message' }
+
+ end
+
+ context 'existing translation' do
+
+ let(:translation) { 'Hello world' }
+
+ it { is_expected.to eq 'Hello world' }
+
+ end
+
+ context 'passing a locale and a scope' do
+
+ let(:translation) { 'Bonjour monde' }
+
+ describe 'legacy syntax' do
+
+ let(:source) { "{{ 'welcome_message' | translate: 'fr', 'locomotive.default' }}" }
+ it { expect(translator).to receive(:translate).with('welcome_message', 'locale' => 'fr', 'scope' => 'locomotive.default'); subject }
+
+ end
+
+ describe 'new syntax' do
+
+ let(:source) { "{{ 'welcome_message' | translate: locale: 'fr', scope: 'locomotive.default' }}" }
+ it { expect(translator).to receive(:translate).with('welcome_message', 'locale' => 'fr', 'scope' => 'locomotive.default'); subject }
+
+ end
+
+ end
+
+ end
spec/unit/services/translator_service_spec.rb +1 -1
@@ @@ -16,7 +16,7 @@ describe Locomotive::Steam::TranslatorService do
allow(repository).to receive(:by_key).with('example_test').and_return(translation)
end
- subject { service.translate(input, locale, scope) }
+ subject { service.translate(input, 'locale' => locale, 'scope' => scope) }
describe 'existing translation' do