i18n_link 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +52 -0
- data/lib/i18n_link.rb +5 -0
- data/lib/i18n_link/helper.rb +27 -0
- data/spec/i18n_link_helper_spec.rb +55 -0
- data/spec/spec_helper.rb +6 -0
- metadata +81 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Johannes Barre
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
h1. A helper for links in your translations
|
2
|
+
|
3
|
+
h2. Why?
|
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.
|
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
|
+
|
8
|
+
<pre><code>en:
|
9
|
+
copy: "Did you read the %{guide_link}?"
|
10
|
+
copy_guide_link: "Rails I18n guide"</code></pre>
|
11
|
+
|
12
|
+
<pre><code><%=raw t("copy", guide_link: link_to(t("copy_guide_link"), "http://guides.rubyonrails.org/i18n.html")) %></code></pre>
|
13
|
+
|
14
|
+
Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? I18n_link lets you do that:
|
15
|
+
|
16
|
+
<pre><code>en:
|
17
|
+
copy: "Did you read the %{guide_link:Rails I18n guide}?"</code></pre>
|
18
|
+
|
19
|
+
<pre><code><%=t_link "copy", guide_link: "http://guides.rubyonrails.org/i18n.html" %></code></pre>
|
20
|
+
|
21
|
+
You may have noticed in the example above, that @t_link@ doesn't require @raw@ anymore. Of course, all HTML in the translation
|
22
|
+
gets properly escaped, so you don't have to worry about XSS.
|
23
|
+
|
24
|
+
h2. Installation
|
25
|
+
|
26
|
+
Just add the following line to your Gemfile:
|
27
|
+
|
28
|
+
<pre><code>gem 'i18n_link'</code></pre>
|
29
|
+
|
30
|
+
h2. Usage
|
31
|
+
|
32
|
+
You may have as many links inside your translations as you like, and normal interpolations are possible as well:
|
33
|
+
|
34
|
+
<pre><code>en:
|
35
|
+
copy: "Did you read the %{guide:Rails I18n guide}? It has more than %{advises} useful advises. You may fork the repo at {repo:github}."</code></pre>
|
36
|
+
|
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
|
+
|
39
|
+
You may also specify options for link_to. Just give a second option named like the link + @_options@
|
40
|
+
|
41
|
+
<pre><code><%=t_link "copy", guide_link: "http://guides.rubyonrails.org/i18n.html", guide_link_options: {target: '_blank', class: "important"} %></code></pre>
|
42
|
+
|
43
|
+
h2. isit18.com?
|
44
|
+
|
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
|
46
|
+
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
|
+
|
48
|
+
h2. Abandoned & outdated?
|
49
|
+
|
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.
|
51
|
+
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.
|
data/lib/i18n_link.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module I18nLink
|
4
|
+
module Helper
|
5
|
+
include ActionView::Helpers::UrlHelper
|
6
|
+
include ERB::Util
|
7
|
+
|
8
|
+
def t_link(translation, options = {})
|
9
|
+
options.symbolize_keys!
|
10
|
+
string = h(t(translation)).gsub(/%\{[^}]+\}/) do |tl|
|
11
|
+
if tl.include?(":")
|
12
|
+
token, label = tl[2..-2].split(":", 2)
|
13
|
+
addr = options.delete(token.to_sym)
|
14
|
+
if addr.nil?
|
15
|
+
tl
|
16
|
+
else
|
17
|
+
link_options = options.delete("#{token}_options".to_sym)
|
18
|
+
link_to(label, addr, link_options)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
tl
|
22
|
+
end
|
23
|
+
end
|
24
|
+
raw(string % options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'i18n_link'
|
5
|
+
|
6
|
+
describe I18nLink::Helper, "#t_link" do
|
7
|
+
before do
|
8
|
+
I18n.backend.store_translations(:en, :test1 => "I'm containing a %{link:link to Rails} in the middle.")
|
9
|
+
I18n.backend.store_translations(:de, :test1 => "Ich enthalte einen %{link:Link zu Rails} in der Mitte.")
|
10
|
+
|
11
|
+
@view = ActionView::Base.new
|
12
|
+
@controller = ActionController::Base.new
|
13
|
+
@view.controller = @controller
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
I18n.locale = :en
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should insert the link into the string" do
|
21
|
+
@view.t_link("test1", :link => "http://www.rubyonrails.org").should == 'I\'m containing a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should insert the link into the German translation" do
|
25
|
+
I18n.locale = :de
|
26
|
+
@view.t_link("test1", :link => "http://www.rubyonrails.org").should == 'Ich enthalte einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should allow link options to be set" do
|
30
|
+
@view.t_link("test1", :link => "http://www.rubyonrails.org", :link_options => {:target => "_blank"}).should == 'I\'m containing a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise an error, if the link_options arn't an Hash" do
|
34
|
+
expect {@view.t_link("test1", :link => "http://www.rubyonrails.org", :link_options => 'target="_blank"')}.to raise_exception
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should mark the result as html safe" do
|
38
|
+
@view.t_link("test1", :link => "http://www.rubyonrails.org").html_safe?.should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should escape all html in the translation" do
|
42
|
+
I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
|
43
|
+
@view.t_link("test2", :link => "http://www.rubyonrails.org").should == '<a href="hax0r"> & <a href="http://www.rubyonrails.org">link -> Rails</a> in <b>the middle</b>.'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should also work with 2 links" do
|
47
|
+
I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
|
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
|
+
end
|
50
|
+
|
51
|
+
it "should allow normal I18n replacements" do
|
52
|
+
I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
|
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
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n_link
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Johannes Barre
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-12 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.0.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.6.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description:
|
38
|
+
email: igel@igels.net
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.textile
|
45
|
+
files:
|
46
|
+
- MIT-LICENSE
|
47
|
+
- README.textile
|
48
|
+
- lib/i18n_link.rb
|
49
|
+
- lib/i18n_link/helper.rb
|
50
|
+
- spec/i18n_link_helper_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: http://github.com/igel/i18n_link
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: A helper for links in your translations
|
79
|
+
test_files:
|
80
|
+
- spec/i18n_link_helper_spec.rb
|
81
|
+
- spec/spec_helper.rb
|