it 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -52,7 +52,7 @@ Even nested interpolations are possible:
52
52
 
53
53
  <pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', :b => It.tag(:b) %></code></pre>
54
54
 
55
- If you would like to use the same translations in your html and plain text mails, you will like the +It.plain+ method:
55
+ If you would like to use the same translations in your html and plain text mails, you will like the @It.plain@ method:
56
56
  <pre><code>en:
57
57
  mail_copy: "Do you like %{link:Rails}?"</code></pre>
58
58
 
@@ -63,7 +63,7 @@ Plain mail:
63
63
  <%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>
64
64
  </code></pre>
65
65
 
66
- The +%s+ will be replaced with the label, in the example with Rails. You could provide any other string containing +%s+. The default is just +%s+, so it will return only the label itself.
66
+ The @%s@ will be replaced with the label, in the example with Rails. You could provide any other string containing @%s@. The default is just @%s@, so it will return only the label itself.
67
67
 
68
68
  h2. isit18.com?
69
69
 
data/lib/it.rb CHANGED
@@ -9,8 +9,14 @@ end
9
9
 
10
10
  # Namespace of the gem.
11
11
  module It
12
+ # It outside of your views. See documentation at Helper#it
13
+ def self.it(identifier, options = {})
14
+ options.stringify_keys!
15
+ process(I18n.t(identifier, :locale => options["locale"]), options)
16
+ end
17
+
12
18
  # Creates a new link to be used in +it+.
13
- #
19
+ #
14
20
  # * +href+: The url for the link. You may specify it as a String or as a named route like +article_path+. It's not possible to specify
15
21
  # a Hash like <code>{:controller => "articles", :action => "index"}</code> directly. Use the +url_for+ helper, if you would like to specify your
16
22
  # links like that.
@@ -18,20 +24,55 @@ module It
18
24
  def self.link(href, options = {})
19
25
  It::Link.new(href, options)
20
26
  end
21
-
27
+
22
28
  # Creates a new plain replacement to be used in +it+.
23
- #
24
- # * +template+: A string to be used as the template. An example would be <code>"%s[http://www.rubyonrails.org]"</code>. Defaults to
29
+ #
30
+ # * +template+: A string to be used as the template. An example would be <code>"%s[http://www.rubyonrails.org]"</code>. Defaults to
25
31
  # <code>"%s"</code>. <em>(optional)</em>
26
32
  def self.plain(template = "%s")
27
33
  It::Plain.new(template)
28
34
  end
29
-
35
+
30
36
  # Creates a new tag to be used in +it+.
31
- #
37
+ #
32
38
  # * +tag_name+: The name of the tag as a Symbol or String.
33
39
  # * +options+: The options will become attributes on the tag. <em>(optional)</em>
34
40
  def self.tag(tag_name, options = {})
35
41
  It::Tag.new(tag_name, options)
36
42
  end
37
- end
43
+
44
+ private
45
+ def self.process(string, options)
46
+ # Handle pluralization
47
+ string = I18n.backend.send(:pluralize, options["locale"] || I18n.locale, string, options["count"]) if string.is_a?(Hash) && options["count"]
48
+
49
+ # We want the escaped String, not an ActiveSupport::SafeBuffer
50
+ translation = String.new(ERB::Util.h(string))
51
+
52
+ # For deep nesting, we repeat the process until we have no interpolations anymore
53
+ while translation =~ /%\{[^{}}]+\}/
54
+ translation.gsub!(/%\{[^{}}]+\}/) do |interpolation|
55
+ token, label = interpolation[2..-2].split(":", 2)
56
+
57
+ # Convert tokens with String arguments into It::Links, if they are named link, link_* or *_link
58
+ if (token == "link" || token.ends_with?("_link") || token.starts_with?("link_")) && (options[token].is_a?(String) || options[token].is_a?(Hash))
59
+ options[token] = It::Link.new(options[token])
60
+ end
61
+
62
+ if !options.has_key?(token)
63
+ raise KeyError, "key{#{token}} not found"
64
+ elsif label && !options[token].is_a?(It::Tag)
65
+ raise ArgumentError, "key{#{token}} has an argument, so it cannot resolved with a #{options[token].class}"
66
+ elsif label # Normal tags
67
+ options[token].process(label.html_safe)
68
+ elsif options[token].is_a?(It::Tag) # Empty tag
69
+ options[token].process
70
+ else # Normal interpolations, as I18n.t would do it.
71
+ ERB::Util.h(options[token])
72
+ end
73
+ end
74
+ end
75
+
76
+ translation.html_safe
77
+ end
78
+ end
data/lib/it/helper.rb CHANGED
@@ -3,81 +3,44 @@
3
3
  module It
4
4
  # The helper will be available in the views.
5
5
  module Helper
6
- # This helper method works just like +t+ (or +translate+ for long), but it allows to insert tags like links
6
+ # This helper method works just like +t+ (or +translate+ for long), but it allows to insert tags like links
7
7
  # or spans in the result. The content of these tags can be written in line with the rest of the translation.
8
8
  # Unless you need this functionality, use the normal +t+ method, which is faster.
9
- #
9
+ #
10
10
  # Like for normal translations, you specify interpolations with <code>%{</code> at the beginning and with
11
11
  # <code>}</code> at the end. The new element is the <code>:</code>, which separates the name from the argument
12
12
  # of this interpolation.
13
- #
13
+ #
14
14
  # # translation: "Already signed up? %{login_link:Sign in}!"
15
- #
15
+ #
16
16
  # <%=it("translation", :login_link => It.link(login_path))
17
- #
17
+ #
18
18
  # If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with +_link+,
19
19
  # you may specify the argument as a String or your helper.
20
- #
20
+ #
21
21
  # # translation: "Already signed up? %{login_link:Sign in}!"
22
- #
22
+ #
23
23
  # <%=it("translation", :login_link => login_path)
24
- #
24
+ #
25
25
  # You may have as many tags inside of one translation as you like, and you even may nest them into each other. Also you
26
26
  # may specify arguments to links and other tags.
27
- #
27
+ #
28
28
  # # translation: "The top contributor of %{wiki_link:our wiki} is currently %{user_link:%{b:%{name}}}. Thanks a lot, %{name}!"
29
- #
29
+ #
30
30
  # <%= it("translation", :wiki_link => wiki_path, :name => user.name, :b => It.tag(:b, :class => "user"), :user_link => It.link(user_path(user), :target => "_blank"))
31
- #
32
- # 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,
31
+ #
32
+ # 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,
33
33
  # things seperated over multiple lines should go into different translations. Use it for inline tags like links, <code><span></code>,
34
34
  # <code><b></code>, <code><i></code>, <code><em></code> or <code><strong></code>.
35
- #
35
+ #
36
36
  # It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify your
37
- # link target as an Hash. Also, it's not possible by default to use this helper from inside your controllers. Eighter
38
- # you google how to use helpers inside your controllers or you push only the arguments for +it+ into the flash and parse
39
- # it once you parse the flash in the view:
40
- #
41
- # # Controller:
42
- # flash[:message] = ["articles.update.success", {:article_link => It.link(article_path(article))}]
43
- #
44
- # # View (or layout)
45
- # <% if flash[:message].is_a?(Array) %>
46
- # <%= it(flash[:message].first, flash[:message].last) %>
47
- # <% else %>
48
- # <%= flash[:message] %>
49
- # <% end %>
50
- #
37
+ # link target as an Hash.
38
+ #
39
+ # If you need to use it outside of your views, use +It.it+.
40
+ #
51
41
  def it(identifier, options = {})
52
42
  options.stringify_keys!
53
- # We want the escaped String, not an ActiveSupport::SafeBuffer
54
- translation = String.new(h(t(identifier)))
55
-
56
- # For deep nesting, we repeat the process until we have not interpolations anymore
57
- while translation =~ /%\{[^{}}]+\}/
58
- translation.gsub!(/%\{[^{}}]+\}/) do |interpolation|
59
- token, label = interpolation[2..-2].split(":", 2)
60
-
61
- # Convert tokens with String arguments into It::Links, if they are named link, link_* or *_link
62
- if (token == "link" || token.ends_with?("_link") || token.starts_with?("link_")) && (options[token].is_a?(String) || options[token].is_a?(Hash))
63
- options[token] = It::Link.new(options[token])
64
- end
65
-
66
- if !options.has_key?(token)
67
- raise KeyError, "key{#{token}} not found"
68
- elsif label && !options[token].is_a?(It::Tag)
69
- raise ArgumentError, "key{#{token}} has an argument, so it cannot resolved with a #{options[token].class}"
70
- elsif label # Normal tags
71
- options[token].process(raw label)
72
- elsif options[token].is_a?(It::Tag) # Empty tag
73
- options[token].process
74
- else # Normal interpolations, as I18n.t would do it.
75
- h(options[token])
76
- end
77
- end
78
- end
79
-
80
- raw translation
43
+ It.process(t(identifier, :locale => options["locale"]), options)
81
44
  end
82
45
  end
83
46
  end
@@ -7,108 +7,130 @@ describe It::Helper, "#it" do
7
7
  before do
8
8
  I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
9
9
  I18n.backend.store_translations(:de, :test1 => "Ich enthalte einen %{link:Link zu Rails} in der Mitte.")
10
-
10
+
11
11
  @view = ActionView::Base.new
12
12
  @controller = ActionController::Base.new
13
13
  @view.controller = @controller
14
14
  end
15
-
15
+
16
16
  after do
17
17
  I18n.locale = :en
18
18
  end
19
-
19
+
20
20
  it "should insert the link into the string" do
21
21
  @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.'
22
22
  end
23
-
23
+
24
24
  it "should insert the link into the German translation" do
25
25
  I18n.locale = :de
26
26
  @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.'
27
27
  end
28
-
28
+
29
29
  it "should allow link options to be set" do
30
30
  @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.'
31
31
  end
32
-
32
+
33
33
  it "should support the plain thing" do
34
34
  @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.'
35
35
  end
36
-
36
+
37
37
  it "should parse other tags as well" do
38
38
  @view.it("test1", :link => It.tag(:b, :class => "classy")).should == 'I\'m containing a <b class="classy">link to Rails</b> in the middle.'
39
39
  end
40
-
40
+
41
41
  it "should mark the result as html safe" do
42
42
  @view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?.should be_true
43
43
  end
44
-
44
+
45
45
  it "should escape all html in the translation" do
46
46
  I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
47
47
  @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;.'
48
48
  end
49
-
49
+
50
50
  it "should also work with 2 links" do
51
51
  I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
52
52
  @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>.'
53
53
  end
54
-
54
+
55
55
  it "should allow normal I18n interpolations" do
56
56
  I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
57
57
  @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.'
58
58
  end
59
-
59
+
60
60
  it "should allow Intergers as normal interpolation" do
61
61
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
62
62
  @view.it("test5", :name => 2).should == 'Hello 2.'
63
63
  end
64
-
64
+
65
65
  it "should escape the HTML in normal interpolations" do
66
66
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
67
67
  @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;.'
68
68
  end
69
-
69
+
70
70
  it "should not escape html_safe interpolations" do
71
71
  I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
72
72
  @view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe).should == 'Hello <a href="http://www.rubyonrails.org">Rails</a>.'
73
73
  end
74
-
74
+
75
75
  it "should allow interpolations inside of links" do
76
76
  I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
77
77
  @view.it("test6", :link => It.link("/article/2"), :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
78
78
  end
79
-
79
+
80
80
  it "should raise a KeyError, if the key was not given" do
81
81
  expect { @view.it("test1", :blubb => true) }.to raise_error(KeyError, "key{link} not found")
82
82
  end
83
-
83
+
84
84
  it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
85
85
  I18n.backend.store_translations(:en, :test7 => "Sign up %{asdf:here}!")
86
86
  expect { @view.it("test7", :asdf => "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
87
87
  end
88
-
88
+
89
89
  it "should allow Strings, if the interpolation name is link" do
90
90
  I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
91
91
  @view.it("test8", :link => "/register").should == 'Sign up <a href="/register">here</a>!'
92
92
  end
93
-
93
+
94
94
  it "should allow Strings, if the interpolation name ends with _link" do
95
95
  I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
96
96
  @view.it("test8", :register_link => "/register").should == 'Sign up <a href="/register">here</a>!'
97
97
  end
98
-
98
+
99
99
  it "should allow Strings, if the interpolation name starts with link_" do
100
100
  I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
101
101
  @view.it("test8", :link_to_register => "/register").should == 'Sign up <a href="/register">here</a>!'
102
102
  end
103
-
103
+
104
104
  it "should work with tags without arguments" do
105
105
  I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
106
106
  @view.it("test9", :br => It.tag(:br)).should == 'We can <br /> do line breaks'
107
107
  end
108
-
108
+
109
109
  it 'should support dot-prefixed keys' do
110
110
  I18n.backend.store_translations(:en, :widgets => { :show => { :all_widgets => "See %{widgets_link:all widgets}" } })
111
111
  @view.instance_variable_set(:"@_virtual_path", "widgets/show")
112
112
  @view.it('.all_widgets', :widgets_link => '/widgets').should == 'See <a href="/widgets">all widgets</a>'
113
113
  end
114
+
115
+ it 'should support the locale option' do
116
+ @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.'
117
+ end
118
+
119
+ context "With a pluralized translation" do
120
+ before do
121
+ I18n.backend.store_translations(:en, :test10 => {:zero => "You have zero messages.", :one => "You have %{link:one message}.", :other => "You have %{link:%{count} messages}."})
122
+ end
123
+
124
+ it 'should work with count = 0' do
125
+ @view.it("test10", :count => 0, :link => "/messages").should == 'You have zero messages.'
126
+ end
127
+
128
+ it 'should work with count = 1' do
129
+ @view.it("test10", :count => 1, :link => "/messages").should == 'You have <a href="/messages">one message</a>.'
130
+ end
131
+
132
+ it 'should work with count > 1' do
133
+ @view.it("test10", :count => 2, :link => "/messages").should == 'You have <a href="/messages">2 messages</a>.'
134
+ end
135
+ end
114
136
  end
data/spec/it_spec.rb CHANGED
@@ -3,19 +3,26 @@
3
3
  require 'spec_helper'
4
4
  require 'it'
5
5
 
6
+ describe It, '.it' do
7
+ it "should translate inside the controller as well" do
8
+ I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
9
+ It.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.'
10
+ end
11
+ end
12
+
6
13
  describe It, '.link' do
7
14
  it "should return an It::Link object" do
8
15
  It.link("https://www.github.com").class.should == It::Link
9
16
  end
10
-
17
+
11
18
  it "should accept one param" do
12
19
  expect { It.link("http://www.rubyonrails.org/") }.not_to raise_error
13
20
  end
14
-
21
+
15
22
  it "should accept two params" do
16
23
  expect { It.link("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
17
24
  end
18
-
25
+
19
26
  it "should raise ArgumentError, if called with three params" do
20
27
  expect { It.link("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
21
28
  end
@@ -25,15 +32,15 @@ describe It, '.tag' do
25
32
  it "should return an It::Tag object" do
26
33
  It.tag(:b).class.should == It::Tag
27
34
  end
28
-
35
+
29
36
  it "should work with a param" do
30
37
  expect { It.tag(:b) }.not_to raise_error
31
38
  end
32
-
39
+
33
40
  it "should accept two params" do
34
41
  expect { It.tag(:b, :class => "very_bold") }.not_to raise_error
35
42
  end
36
-
43
+
37
44
  it "should raise an ArgumentError if called with three params" do
38
45
  expect { It.tag(:b, {}, :blubb) }.to raise_error(ArgumentError)
39
46
  end
@@ -43,16 +50,16 @@ describe It, '.plain' do
43
50
  it "should return an It::Plain object" do
44
51
  It.plain.class.should == It::Plain
45
52
  end
46
-
53
+
47
54
  it "should work without params" do
48
55
  expect { It.plain }.not_to raise_error
49
56
  end
50
-
57
+
51
58
  it "should accept one param" do
52
59
  expect { It.plain("%s[http://www.rubyonrails.org/]") }.not_to raise_error
53
60
  end
54
-
61
+
55
62
  it "should raise ArgumentError, if called with two params" do
56
63
  expect { It.plain("%s[http://www.rubyonrails.org/]", :blubb) }.to raise_error(ArgumentError)
57
64
  end
58
- end
65
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: it
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Johannes Barre
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-05 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-12-15 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: actionpack
@@ -74,7 +73,6 @@ files:
74
73
  - spec/it/plain_spec.rb
75
74
  - spec/it_spec.rb
76
75
  - spec/spec_helper.rb
77
- has_rdoc: true
78
76
  homepage: https://github.com/igel/it
79
77
  licenses: []
80
78
 
@@ -106,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
104
  requirements: []
107
105
 
108
106
  rubyforge_project:
109
- rubygems_version: 1.3.7
107
+ rubygems_version: 1.8.10
110
108
  signing_key:
111
109
  specification_version: 3
112
110
  summary: A helper for links and other html tags in your translations