Permalink
Browse files

Fix translation lookup in partials (#116)

* Fix translation lookup in partials

* Address namespacing issue outlined in #117

* Refactor Translation#translate to reduce complexity

* Update changelog
  • Loading branch information...
stevschmid authored and Spone committed Aug 24, 2018
1 parent 7a23a76 commit 0685c5c919401bbb394f3d71c46115b966406874
View
@@ -11,6 +11,7 @@
**Bug fixes:**
- Removed redundant `class` attribute in HAML templates
- Fix `content_for` / `yield` which was no longer working across components, since `v2.0.0`
- Fix translation key lookup in partials (helper method `t`)
## v2.2.0 (2018-07-03)
@@ -107,6 +107,7 @@ Feature: Component generator
import "components/bar/bar";
import "components/foo_bar/foo_bar";
import "components/namespaced";
import "components/partial";
import "components/required/required";
import "components/some_example/some_example";
import "components/world/world";
@@ -2,5 +2,6 @@ import "components/all/all";
import "components/bar/bar";
import "components/foo_bar/foo_bar";
import "components/namespaced";
import "components/partial";
import "components/required/required";
import "components/world/world";
@@ -0,0 +1 @@
import "components/partial/universe/partial_universe";
@@ -0,0 +1 @@
<%= t(".reply", answer: answer) -%>
@@ -0,0 +1 @@
<div class="partial-universe"><%= render partial: "answer", locals: { answer: 42 } %></div>
@@ -0,0 +1 @@
.partial-universe {}
@@ -0,0 +1,4 @@
en:
partial_universe_component:
answer:
reply: "The answer is %{answer}"
@@ -0,0 +1 @@
import "./partial_universe.css";
@@ -0,0 +1,5 @@
# frozen_string_literal: true
module PartialUniverseComponent
extend ComponentHelper
end
@@ -5,12 +5,16 @@ module Translation
def translate(key, options = {})
virtual_path = @virtual_path
is_component = key.to_s.first == "." and
is_component = key.to_s.first == "." &&
virtual_path =~ /^components/
if is_component
path = virtual_path.match(/^components\/.+\/_(.+)/)[1]
path += "_component"
matches = virtual_path.match(/^components\/(?<folders>.+)\/_(?<view_name>.+)/)
folders, view_name = matches['folders'], matches['view_name']
component_name = folders.gsub('/', '_')
path = path(component_name, view_name)
defaults = [:"#{path}#{key}"]
defaults << options[:default] if options[:default]
options[:default] = defaults.flatten
@@ -20,5 +24,13 @@ def translate(key, options = {})
super(key, options)
end
alias :t :translate
private
def path(component_name, view_name)
path = "#{component_name}_component"
path += ".#{view_name}" if view_name != component_name # partial scope
path
end
end
end
@@ -39,6 +39,13 @@ def test_helper_renders_localized_keys
component('hello').chomp
end
def test_help_renders_localized_keys_in_partials
I18n.locale = :en
assert_equal \
%(<div class="partial-universe">The answer is 42</div>),
component('partial/universe').chomp
end
def test_helper_renders_default_property
assert_equal \
%(<div class="foo">Foobar</div>),

0 comments on commit 0685c5c

Please sign in to comment.