Skip to content

Commit

Permalink
Merge 059aeec into 7a23a76
Browse files Browse the repository at this point in the history
  • Loading branch information
stevschmid committed Aug 24, 2018
2 parents 7a23a76 + 059aeec commit 1314d3f
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions features/component_generator.feature
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
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
@@ -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
18 changes: 15 additions & 3 deletions lib/komponent/translation.rb
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
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 1314d3f

Please sign in to comment.