From c226254b07051e9748515538453b28a315eb4a87 Mon Sep 17 00:00:00 2001 From: Jay Pignata Date: Sun, 16 Aug 2009 16:42:09 -0400 Subject: [PATCH] Adding file_name and base_path to the template details hash and using them to determine i18n keys for scoped translations --- .../lib/action_view/helpers/translation_helper.rb | 2 +- actionpack/lib/action_view/template/resolver.rb | 15 ++++++++------- .../test/template/translation_helper_test.rb | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb index 4aed10f..665a7c0 100644 --- a/actionpack/lib/action_view/helpers/translation_helper.rb +++ b/actionpack/lib/action_view/helpers/translation_helper.rb @@ -29,7 +29,7 @@ module ActionView private def scope_key_by_partial(key) if key.to_s.first == "." - template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s + "#{template.details[:base_path].gsub(%r{/_?}, ".")}.#{template.details[:file_name]}" + key.to_s else key end diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index fe65716..2f45db9 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -101,7 +101,7 @@ module ActionView source = File.read(path) identifier = Pathname.new(path).expand_path.to_s - Template.new(source, identifier, *path_to_details(path)) + Template.new(source, identifier, *path_to_details(identifier)) end.compact end end @@ -133,15 +133,16 @@ module ActionView # :api: plugin def path_to_details(path) # [:erb, :format => :html, :locale => :en, :partial => true/false] - if m = path.match(%r'/(_)?[\w-]+(\.[\w-]+)*\.(\w+)$') - partial = m[1] == '_' - details = (m[2]||"").split('.').reject { |e| e.empty? } - handler = Template.handler_class_for_extension(m[3]) + if m = path.match(%r'#{@path}/(.*/)?(_)?([\w-]+)(\.[\w-]+)*\.(\w+)$') + base_path = m[1] + partial = m[2] == '_' + file_name = m[3] + details = (m[4]||"").split('.').reject { |e| e.empty? } + handler = Template.handler_class_for_extension(m[5]) format = Mime[details.last] && details.pop.to_sym locale = details.last && details.pop.to_sym - - return handler, :format => format, :locale => locale, :partial => partial + return handler, :format => format, :locale => locale, :partial => partial, :base_path => base_path, :file_name => file_name end end end diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index a20f3c3..46da8ac 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -17,15 +17,15 @@ class TranslationHelperTest < Test::Unit::TestCase expected = 'en, foo' assert_equal expected, translate(:foo) end - + def test_delegates_localize_to_i18n @time = Time.utc(2008, 7, 8, 12, 18, 38) I18n.expects(:localize).with(@time) localize @time end - def test_scoping_by_partial - expects(:template).returns(stub(:path_without_format_and_extension => "people/index")) + def test_scoping_by_partial + expects(:template).returns(stub(:details => {:file_name => 'index', :base_path => 'people'})).times(2) I18n.expects(:translate).with("people.index.foo", :locale => 'en', :raise => true) translate ".foo", :locale => 'en' end -- 1.5.6.5