Skip to content

Commit

Permalink
Fix translation lookup in partials (#116)
Browse files Browse the repository at this point in the history
* 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 Feb 26, 2019
1 parent 5f2fa2f commit f858722
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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)

Expand Down
1 change: 1 addition & 0 deletions features/component_generator.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions fixtures/my_app/frontend/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
1 change: 1 addition & 0 deletions fixtures/my_app/frontend/components/partial/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "components/partial/universe/partial_universe";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= t(".reply", answer: answer) -%>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="partial-universe"><%= render partial: "answer", locals: { answer: 42 } %></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.partial-universe {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
partial_universe_component:
answer:
reply: "The answer is %{answer}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./partial_universe.css";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module PartialUniverseComponent
extend ComponentHelper
end
18 changes: 15 additions & 3 deletions lib/komponent/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
7 changes: 7 additions & 0 deletions test/komponent/komponent_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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>),
Expand Down

0 comments on commit f858722

Please sign in to comment.