i18n_link 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -2,7 +2,7 @@ h1. A helper for links in your translations
2
2
 
3
3
  h2. Why?
4
4
 
5
- I18n is backed right into Rails, and it's great. But if you want to place links inside your translated copies, things get a little messy.
5
+ I18n is baked right into Rails, and it's great. But if you want to place links inside your translated copies, things get a little messy.
6
6
  You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
7
7
 
8
8
  <pre><code>en:
@@ -23,7 +23,7 @@ gets properly escaped, so you don't have to worry about XSS.
23
23
 
24
24
  h2. Installation
25
25
 
26
- Just add the following line to your Gemfile:
26
+ Just add the following line to your Gemfile & run @bundle install@:
27
27
 
28
28
  <pre><code>gem 'i18n_link'</code></pre>
29
29
 
@@ -36,17 +36,31 @@ You may have as many links inside your translations as you like, and normal inte
36
36
 
37
37
  <pre><code><%=t_link "copy", guide: "http://guides.rubyonrails.org/i18n.html", advices: 100, repo: "https://github.com/lifo/docrails/tree/master/railties/guides" %></code></pre>
38
38
 
39
- You may also specify options for link_to. Just give a second option named like the link + @_options@
39
+ You may also specify options for @link_to@. Just give a second option named like the link + @_options@
40
40
 
41
41
  <pre><code><%=t_link "copy", guide_link: "http://guides.rubyonrails.org/i18n.html", guide_link_options: {target: '_blank', class: "important"} %></code></pre>
42
42
 
43
+ You may pass any kind of object accepted by @link_to@ as the link target, so your loved named routes like
44
+ @article_path(:id => article.id)@ or hashes like @{controller: "articles", action: "index"}@ will all work fine, too.
45
+
46
+ Even nested interpolations are possible:
47
+
48
+ <pre><code>en:
49
+ copy: "Did you read this %{link:nice %{guide}}?"</code></pre>
50
+
51
+ <pre><code><%=t_link "copy", link: "http://guides.rubyonrails.org/i18n.html", guide: 'article' %></code></pre>
52
+
43
53
  h2. isit18.com?
44
54
 
45
- Sorry, I'm using Ruby 1.9.2 and didn't took the time to port i18n_link back to Ruby 1.8. You either might update to Ruby 1.9 as well
55
+ Sorry, I'm using Ruby 1.9.2 at the time of writing and didn't took the time to port i18n_link back to Ruby 1.8. You either might update to Ruby 1.9 as well
46
56
  or make the port yourself. It's probably a mater of minutes to do so, since the lib is small. Send me a pull request, if you have a port!
47
57
 
48
58
  h2. Abandoned & outdated?
49
59
 
50
- Everybody hates old and outdated gems, which don't work on with the latest Rails or Ruby version or haven't been updated for ages.
60
+ Everybody hates old and outdated gems, which don't work on with the latest Rails or Ruby version or haven't been updated for ages.
51
61
  Sadly, rubygems is full of such gems. If you improved i18n_link, send me a pull request. If I have not time to support the lib
52
- anymore, I will happily hand the project over to a new maintainer.
62
+ anymore, I will happily hand the project over to a new maintainer.
63
+
64
+ h2. Broken English?
65
+
66
+ I'm not a native speaker, so you'll probably find some spelling errors or clumsy sentences above. If you do, please send me a message.
@@ -7,7 +7,20 @@ module I18nLink
7
7
 
8
8
  def t_link(translation, options = {})
9
9
  options.symbolize_keys!
10
- string = h(t(translation)).gsub(/%\{[^}]+\}/) do |tl|
10
+ string = String.new(h(t(translation))) # We want an escaped String, not an ActiveSupport::SafeBuffer
11
+
12
+ # Resolve the inner interpolations of nested interpolations
13
+ string.gsub!(/(%\{[^}]+:[^}]*)(%\{[^}]+\})([^}]*\})/) do |tl|
14
+ token = $2[2..-2].to_sym
15
+ if options.has_key?(token)
16
+ "#{$1}#{options[token]}#{$3}"
17
+ else
18
+ tl
19
+ end
20
+ end
21
+
22
+ # Parse links
23
+ string.gsub!(/%\{[^}]+\}/) do |tl|
11
24
  if tl.include?(":")
12
25
  token, label = tl[2..-2].split(":", 2)
13
26
  addr = options.delete(token.to_sym)
@@ -15,13 +28,16 @@ module I18nLink
15
28
  tl
16
29
  else
17
30
  link_options = options.delete("#{token}_options".to_sym)
18
- link_to(label, addr, link_options)
31
+ link_to(raw(label), addr, link_options)
19
32
  end
20
33
  else
21
34
  tl
22
35
  end
23
36
  end
37
+
38
+ # Escape HTML of the replacements and interpolate
39
+ options.each { |key, value| options[key] = (value.is_a?(String) && !value.html_safe?) ? h(value) : value }
24
40
  raw(string % options)
25
41
  end
26
42
  end
27
- end
43
+ end
@@ -48,8 +48,23 @@ describe I18nLink::Helper, "#t_link" do
48
48
  @view.t_link("test3", :link1 => "http://www.rubyonrails.org", :link2 => "http://www.github.com").should == 'I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.'
49
49
  end
50
50
 
51
- it "should allow normal I18n replacements" do
51
+ it "should allow normal I18n interpolations" do
52
52
  I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
53
53
  @view.t_link("test4", :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.'
54
54
  end
55
- end
55
+
56
+ it "should escape the HTML in normal interpolations" do
57
+ I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
58
+ @view.t_link("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;.'
59
+ end
60
+
61
+ it "should not escape html_safe interpolations" do
62
+ I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
63
+ @view.t_link("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe).should == 'Hello <a href="http://www.rubyonrails.org">Rails</a>.'
64
+ end
65
+
66
+ it "should allow interpolations inside of links" do
67
+ I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
68
+ @view.t_link("test6", :link => "/article/2", :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
69
+ end
70
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: i18n_link
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Johannes Barre