diff --git a/lib/nice_partials.rb b/lib/nice_partials.rb index bc6203a..146e58a 100644 --- a/lib/nice_partials.rb +++ b/lib/nice_partials.rb @@ -3,11 +3,6 @@ require_relative "nice_partials/version" module NicePartials - def self.locale_prefix_from(lookup_context, block) - partial_location = block.source_location.first.dup - lookup_context.view_paths.each { partial_location.delete_prefix!(_1.path)&.delete_prefix!("/") } - partial_location.split('.').first.gsub('/_', '/').gsub('/', '.') - end end ActiveSupport.on_load :action_view do diff --git a/lib/nice_partials/monkey_patch.rb b/lib/nice_partials/monkey_patch.rb index ba57398..9588128 100644 --- a/lib/nice_partials/monkey_patch.rb +++ b/lib/nice_partials/monkey_patch.rb @@ -1,28 +1,41 @@ +# frozen_string_literal: true + # Monkey patch required to make `t` work as expected. Is this evil? # TODO Do we need to monkey patch other types of renderers as well? module NicePartials::RenderingWithLocalePrefix - ActionView::Base.prepend self + module BaseIntegration + ::ActionView::Base.prepend self - def capture(*, &block) - with_nice_partials_t_prefix(lookup_context, block) { super } - end + def t(key, options = {}) + if (template = @_nice_partials_translate_template) && key&.start_with?(".") + key = "#{virtual_path_translate_key_prefix(template.virtual_path)}#{key}" + end + + super(key, **options) + end - def t(key, options = {}) - if (prefix = @_nice_partials_t_prefix) && key&.start_with?(".") - key = "#{prefix}#{key}" + def with_nice_partials_t_prefix(block) + old_nice_partials_translate_template = @_nice_partials_translate_template + @_nice_partials_translate_template = block ? @current_template : nil + yield + ensure + @_nice_partials_translate_template = old_nice_partials_translate_template end - super(key, **options) + private + + def virtual_path_translate_key_prefix(virtual_path) + @_scope_key_by_partial_cache ||= {} # Reuses Rails' existing `t` cache. + @_scope_key_by_partial_cache[virtual_path] ||= virtual_path.gsub(%r{/_?}, ".") + end end - private + module PartialRendererIntegration + ActionView::PartialRenderer.prepend self - def with_nice_partials_t_prefix(lookup_context, block) - _nice_partials_t_prefix = @_nice_partials_t_prefix - @_nice_partials_t_prefix = block ? NicePartials.locale_prefix_from(lookup_context, block) : nil - yield - ensure - @_nice_partials_t_prefix = _nice_partials_t_prefix + def render(partial, view, block) + view.with_nice_partials_t_prefix(block) { super } + end end end diff --git a/test/fixtures/translations/_nice_partials_translated.html.erb b/test/fixtures/translations/_nice_partials_translated.html.erb index 2cba69a..a6299a0 100644 --- a/test/fixtures/translations/_nice_partials_translated.html.erb +++ b/test/fixtures/translations/_nice_partials_translated.html.erb @@ -1,3 +1,3 @@ -<%= render("basic") do |partial| %> - <%= partial.message.t ".message" %> +<%= render "basic" do %> + <% _1.message.t ".message" %> <% end %> diff --git a/test/fixtures/translations/_nice_partials_translated_nested.html.erb b/test/fixtures/translations/_nice_partials_translated_nested.html.erb new file mode 100644 index 0000000..ba2ed28 --- /dev/null +++ b/test/fixtures/translations/_nice_partials_translated_nested.html.erb @@ -0,0 +1,6 @@ +<%= render "basic" do %> + <% _1.message render("translations/translated") %> + <% _1.message do %> + <%= render "translations/translated" %> + <% end %> +<% end %> diff --git a/test/renderer/translation_test.rb b/test/renderer/translation_test.rb index 4522a82..b86e433 100644 --- a/test/renderer/translation_test.rb +++ b/test/renderer/translation_test.rb @@ -34,6 +34,12 @@ class Renderer::TranslationTest < NicePartials::Test assert_text "nice_partials" end + test "translations nested" do + render "translations/nice_partials_translated_nested" + + assert_text "message\n message\n" + end + test "translations key lookup handles special characters" do render "translations/special_nice_partials_translated"