Skip to content

Commit

Permalink
add Cell::Translation. fixes #319. many thanks to @johnlane for fixin…
Browse files Browse the repository at this point in the history
…g all this.
  • Loading branch information
apotonick committed Sep 23, 2015
1 parent 1d2325f commit 62d9190
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
@@ -1,6 +1,7 @@
## 4.0.3

* `Cell::Partial` now does _append_ the global partial path to its `view_paths` instead of using `unshift` and thereby removing possible custom paths.
* Adding `Cell::Translation` which allows using the `#t` helper. Thanks to @johnlane.

## 4.0.2

Expand Down
19 changes: 19 additions & 0 deletions lib/cell/translation.rb
@@ -0,0 +1,19 @@
# NOTE: this will soon be moved to cells-rails.
module Cell::Translation
def self.included(includer)
super
includer.inheritable_attr :translation_path
end

def initialize(*)
super
@virtual_path = translation_path
end

private
# If you override this to change this path, please report it on the trailblazer/chat gitter channel,
# so we can find out best practices.
def translation_path
self.class.translation_path or controller_path.gsub("/", ".")
end
end
31 changes: 0 additions & 31 deletions test/i18n_test.rb

This file was deleted.

45 changes: 45 additions & 0 deletions test/rails/translation_test.rb
@@ -0,0 +1,45 @@
require "test_helper"
require "cells/translation"

class TranslationTest < MiniTest::Spec
class I18nCell < Cell::ViewModel
include ActionView::Helpers::TranslationHelper
include Translation

def greet_relative
t(".greeting") # gets appended to translation_test.i18n.
end

def greet_absolute
t("translation_test.i18n.greeting")
end
end

I18n.backend = I18n::Backend::KeyValue.new({})
I18n.backend.store_translations(:en,
{ "translation_test.i18n.greeting" => "Translated!",
"cell.friendly.greeting" => "Hello you!" },
escape: false)

# Translate text specified by an absolute path
it { I18nCell.new.greet_absolute.must_equal "Translated!" }

# Translate text specified by an relative path
it { I18nCell.new.greet_relative.must_equal "Translated!" }


describe "::translation_path" do
class ExplicitI18NCell < Cell::ViewModel
include ActionView::Helpers::TranslationHelper
include Translation

self.translation_path = "cell.friendly"

def show
t(".greeting")
end
end

it { ExplicitI18NCell.new.().must_equal "Hello you!" }
end
end

0 comments on commit 62d9190

Please sign in to comment.