Permalink
Browse files

Support for locale option added (Fixes #5)

  • Loading branch information...
iGEL committed Dec 14, 2011
1 parent 2a71aba commit de899a2c2a17500e70cda7145b1c47100f458ea7
Showing with 48 additions and 44 deletions.
  1. +22 −22 lib/it/helper.rb
  2. +26 −22 spec/it/helper_spec.rb
View
@@ -3,66 +3,66 @@
module It
# The helper will be available in the views.
module Helper
- # This helper method works just like +t+ (or +translate+ for long), but it allows to insert tags like links
+ # This helper method works just like +t+ (or +translate+ for long), but it allows to insert tags like links
# or spans in the result. The content of these tags can be written in line with the rest of the translation.
# Unless you need this functionality, use the normal +t+ method, which is faster.
- #
+ #
# Like for normal translations, you specify interpolations with <code>%{</code> at the beginning and with
# <code>}</code> at the end. The new element is the <code>:</code>, which separates the name from the argument
# of this interpolation.
- #
+ #
# # translation: "Already signed up? %{login_link:Sign in}!"
- #
+ #
# <%=it("translation", :login_link => It.link(login_path))
- #
+ #
# If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with +_link+,
# you may specify the argument as a String or your helper.
- #
+ #
# # translation: "Already signed up? %{login_link:Sign in}!"
- #
+ #
# <%=it("translation", :login_link => login_path)
- #
+ #
# You may have as many tags inside of one translation as you like, and you even may nest them into each other. Also you
# may specify arguments to links and other tags.
- #
+ #
# # translation: "The top contributor of %{wiki_link:our wiki} is currently %{user_link:%{b:%{name}}}. Thanks a lot, %{name}!"
- #
+ #
# <%= it("translation", :wiki_link => wiki_path, :name => user.name, :b => It.tag(:b, :class => "user"), :user_link => It.link(user_path(user), :target => "_blank"))
- #
- # I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or <code><br /></code>, but I think,
+ #
+ # I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or <code><br /></code>, but I think,
# things seperated over multiple lines should go into different translations. Use it for inline tags like links, <code><span></code>,
# <code><b></code>, <code><i></code>, <code><em></code> or <code><strong></code>.
- #
+ #
# It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify your
# link target as an Hash. Also, it's not possible by default to use this helper from inside your controllers. Eighter
# you google how to use helpers inside your controllers or you push only the arguments for +it+ into the flash and parse
# it once you parse the flash in the view:
- #
+ #
# # Controller:
# flash[:message] = ["articles.update.success", {:article_link => It.link(article_path(article))}]
- #
+ #
# # View (or layout)
# <% if flash[:message].is_a?(Array) %>
# <%= it(flash[:message].first, flash[:message].last) %>
# <% else %>
# <%= flash[:message] %>
# <% end %>
- #
+ #
def it(identifier, options = {})
options.stringify_keys!
# We want the escaped String, not an ActiveSupport::SafeBuffer
- translation = String.new(h(t(identifier)))
-
- # For deep nesting, we repeat the process until we have not interpolations anymore
+ translation = String.new(h(t(identifier, :locale => options.delete("locale"))))
+
+ # For deep nesting, we repeat the process until we have no interpolations anymore
while translation =~ /%\{[^{}}]+\}/
translation.gsub!(/%\{[^{}}]+\}/) do |interpolation|
token, label = interpolation[2..-2].split(":", 2)
-
+
# Convert tokens with String arguments into It::Links, if they are named link, link_* or *_link
if (token == "link" || token.ends_with?("_link") || token.starts_with?("link_")) && (options[token].is_a?(String) || options[token].is_a?(Hash))
options[token] = It::Link.new(options[token])
end
-
+
if !options.has_key?(token)
raise KeyError, "key{#{token}} not found"
elsif label && !options[token].is_a?(It::Tag)
@@ -76,7 +76,7 @@ def it(identifier, options = {})
end
end
end
-
+
raw translation
end
end
View
@@ -7,108 +7,112 @@
before do
I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
I18n.backend.store_translations(:de, :test1 => "Ich enthalte einen %{link:Link zu Rails} in der Mitte.")
-
+
@view = ActionView::Base.new
@controller = ActionController::Base.new
@view.controller = @controller
end
-
+
after do
I18n.locale = :en
end
-
+
it "should insert the link into the string" do
@view.it("test1", :link => It.link("http://www.rubyonrails.org")).should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
end
-
+
it "should insert the link into the German translation" do
I18n.locale = :de
@view.it("test1", :link => It.link("http://www.rubyonrails.org")).should == 'Ich enthalte einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.'
end
-
+
it "should allow link options to be set" do
@view.it("test1", :link => It.link("http://www.rubyonrails.org", :target => "_blank")).should == 'I\'m containing a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.'
end
-
+
it "should support the plain thing" do
@view.it("test1", :link => It.plain("%s[http://www.rubyonrails.org]")).should == 'I\'m containing a link to Rails[http://www.rubyonrails.org] in the middle.'
end
-
+
it "should parse other tags as well" do
@view.it("test1", :link => It.tag(:b, :class => "classy")).should == 'I\'m containing a <b class="classy">link to Rails</b> in the middle.'
end
-
+
it "should mark the result as html safe" do
@view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?.should be_true
end
-
+
it "should escape all html in the translation" do
I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
@view.it("test2", :link => It.link("http://www.rubyonrails.org")).should == '&lt;a href=&quot;hax0r&quot;&gt; &amp; <a href="http://www.rubyonrails.org">link -&gt; Rails</a> in &lt;b&gt;the middle&lt;/b&gt;.'
end
-
+
it "should also work with 2 links" do
I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
@view.it("test3", :link1 => It.link("http://www.rubyonrails.org"), :link2 => It.link("http://www.github.com")).should == 'I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.'
end
-
+
it "should allow normal I18n interpolations" do
I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
@view.it("test4", :link => It.link("http://www.rubyonrails.org"), :position => "middle").should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
end
-
+
it "should allow Intergers as normal interpolation" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
@view.it("test5", :name => 2).should == 'Hello 2.'
end
-
+
it "should escape the HTML in normal interpolations" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
@view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>').should == 'Hello &lt;a href=&quot;http://evil.haxor.com&quot;&gt;victim&lt;/a&gt;.'
end
-
+
it "should not escape html_safe interpolations" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
@view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe).should == 'Hello <a href="http://www.rubyonrails.org">Rails</a>.'
end
-
+
it "should allow interpolations inside of links" do
I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
@view.it("test6", :link => It.link("/article/2"), :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
end
-
+
it "should raise a KeyError, if the key was not given" do
expect { @view.it("test1", :blubb => true) }.to raise_error(KeyError, "key{link} not found")
end
-
+
it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
I18n.backend.store_translations(:en, :test7 => "Sign up %{asdf:here}!")
expect { @view.it("test7", :asdf => "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
end
-
+
it "should allow Strings, if the interpolation name is link" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
@view.it("test8", :link => "/register").should == 'Sign up <a href="/register">here</a>!'
end
-
+
it "should allow Strings, if the interpolation name ends with _link" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
@view.it("test8", :register_link => "/register").should == 'Sign up <a href="/register">here</a>!'
end
-
+
it "should allow Strings, if the interpolation name starts with link_" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
@view.it("test8", :link_to_register => "/register").should == 'Sign up <a href="/register">here</a>!'
end
-
+
it "should work with tags without arguments" do
I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
@view.it("test9", :br => It.tag(:br)).should == 'We can <br /> do line breaks'
end
-
+
it 'should support dot-prefixed keys' do
I18n.backend.store_translations(:en, :widgets => { :show => { :all_widgets => "See %{widgets_link:all widgets}" } })
@view.instance_variable_set(:"@_virtual_path", "widgets/show")
@view.it('.all_widgets', :widgets_link => '/widgets').should == 'See <a href="/widgets">all widgets</a>'
end
+
+ it 'should support the locale option' do
+ @view.it('test1', :locale => "de", :link => It.link("http://www.rubyonrails.org")).should == 'Ich enthalte einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.'
+ end
end

0 comments on commit de899a2

Please sign in to comment.