it 0.2.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +27 -0
- data/Gemfile +2 -5
- data/README.md +123 -0
- data/Rakefile +6 -1
- data/it.gemspec +28 -0
- data/lib/it.rb +13 -43
- data/lib/it/helper.rb +19 -17
- data/lib/it/interpolation.rb +65 -0
- data/lib/it/link.rb +8 -5
- data/lib/it/parser.rb +50 -0
- data/lib/it/plain.rb +7 -5
- data/lib/it/tag.rb +10 -9
- data/lib/it/version.rb +3 -0
- metadata +138 -86
- data/README.textile +0 -82
- data/spec/it/helper_spec.rb +0 -136
- data/spec/it/link_spec.rb +0 -42
- data/spec/it/plain_spec.rb +0 -30
- data/spec/it/tag_spec.rb +0 -68
- data/spec/it_spec.rb +0 -65
- data/spec/spec_helper.rb +0 -6
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b79069211ccb670779e106ffa6802ab17feac3d72ca87b4088ce4eab9b928b7
|
4
|
+
data.tar.gz: 61b097f8a810653820c8376c523b56f5a7435c93a966505511688f139f3d7837
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a508fd6bb549cdee9b7dd8a9e1fedf9ef0ede81b739fc7cec3e29fc610119e42528e21696bde282847725633b1f029896194b28a4b7bbb0ba6ae5a364cb84a17
|
7
|
+
data.tar.gz: 912d6c0e1db45ecf52cd5c80be7780f6385400f0e84e3ab88076ba639ce8e25569b9a373fa32f23901e50ffa1f73837302425927300301abff04f66e39570557
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# 2.0.0 (2020-08-07)
|
2
|
+
|
3
|
+
## Breaking changes
|
4
|
+
|
5
|
+
* Support dropped for Ruby 2.3, 2.4 and for Rails 4.2, 5.0, and 5.1
|
6
|
+
|
7
|
+
## Fixes
|
8
|
+
|
9
|
+
* Fix 'TypeError - hash key raise is not a Symbol' when used with Rails 6.0.3 [#28](https://github.com/iGEL/it/pull/28)
|
10
|
+
*Jason Barnabe* and *wingice*
|
11
|
+
* Fix Ruby 2.7 deprecations
|
12
|
+
* `It.link` now accepts `ActiveSupport::SafeBuffer`s
|
13
|
+
Reported by *James Balazs*
|
14
|
+
|
15
|
+
# 1.0.0 (2017-06-03)
|
16
|
+
|
17
|
+
* Allow whitespace after the colon without considering it part of the value [#20](https://github.com/iGEL/it/pull/20)
|
18
|
+
*Russell Norris*
|
19
|
+
* Run specs with warnings enabled
|
20
|
+
* 1.0.0 Release to conform with semver 2.0
|
21
|
+
|
22
|
+
# 0.8.0 (2015-06-12)
|
23
|
+
|
24
|
+
* Make `It.plain` work with empty content [#14](https://github.com/iGEL/it/pull/14)
|
25
|
+
*Emil Sågfors*
|
26
|
+
* Pass though `:scope` & `:default` [#13](https://github.com/iGEL/it/pull/13)
|
27
|
+
*Emil Sågfors*
|
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/6f705ee22d3eb7422d8f/maintainability)](https://codeclimate.com/github/iGEL/it/maintainability)
|
2
|
+
[![Build Status](http://img.shields.io/travis/iGEL/it/main.svg?style=flat)](https://travis-ci.org/iGEL/it)
|
3
|
+
[![Coverage](http://img.shields.io/coveralls/iGEL/it/main.svg?style=flat)](https://coveralls.io/r/iGEL/it)
|
4
|
+
[![Rubygems](http://img.shields.io/gem/v/it.svg?style=flat)](http://rubygems.org/gems/it)
|
5
|
+
[![Github Issues](http://img.shields.io/github/issues/iGEL/it.svg?style=flat)](https://github.com/iGEL/it/issues)
|
6
|
+
|
7
|
+
Tested against Ruby 2.7, 2.6, and 2.5 and Rails 6.0 and 5.2
|
8
|
+
|
9
|
+
What is **it**?
|
10
|
+
=============
|
11
|
+
|
12
|
+
I18n is baked right into Rails, and it's great. But if you want to place markup or links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
|
13
|
+
|
14
|
+
```yaml
|
15
|
+
en:
|
16
|
+
copy: "If you are already registered, %{login_link}!"
|
17
|
+
copy_login_link: "please sign in"
|
18
|
+
```
|
19
|
+
|
20
|
+
```erb
|
21
|
+
<%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %>
|
22
|
+
```
|
23
|
+
|
24
|
+
Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? **it** lets you do that:
|
25
|
+
|
26
|
+
```yaml
|
27
|
+
en:
|
28
|
+
copy: "If you are already registered, %{login_link:please sign in}!"
|
29
|
+
```
|
30
|
+
|
31
|
+
```erb
|
32
|
+
<%=it "copy", login_link: login_path %>
|
33
|
+
```
|
34
|
+
|
35
|
+
You may have noticed in the example above, that **it** doesn't require `raw` anymore. Of course, all HTML in the translation gets properly escaped, so you don't have to worry about XSS.
|
36
|
+
|
37
|
+
Installation
|
38
|
+
------------
|
39
|
+
|
40
|
+
Just add the following line to your Gemfile & run `bundle install`:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'it'
|
44
|
+
```
|
45
|
+
|
46
|
+
Usage
|
47
|
+
-----
|
48
|
+
|
49
|
+
You may have as many links inside your translations as you like, and normal interpolations are possible as well:
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
en:
|
53
|
+
copy: "Read the %{guide:Rails I18n guide} for more than %{advises} advises. Fork it at %{repo:github}."
|
54
|
+
```
|
55
|
+
|
56
|
+
```erb
|
57
|
+
<%=it "copy",
|
58
|
+
guide: It.link("http://guides.rubyonrails.org/i18n.html"),
|
59
|
+
advices: 100,
|
60
|
+
repo: It.link("https://github.com/rails/rails") %>
|
61
|
+
```
|
62
|
+
|
63
|
+
As you see above, unless the interpolation name is `link` or starts with `link_` or ends with `_link`, you need to call `It.link` to create a link. The advantage of `It.link`: You may specify options like you would with `link_to`:
|
64
|
+
|
65
|
+
```erb
|
66
|
+
<%=it "copy",
|
67
|
+
link: It.link("http://rubyonrails.org", target: '_blank', class: "important") %>
|
68
|
+
```
|
69
|
+
|
70
|
+
You may pass any kind of object accepted by `link_to` as the link target, so your loved named routes like `article_path(id: article.id)` will all work fine.
|
71
|
+
|
72
|
+
Want to introduce some markup into your sentences? **it** will help you:
|
73
|
+
|
74
|
+
```yaml
|
75
|
+
en:
|
76
|
+
advantages: There are %{b:many advantages} for registered users!
|
77
|
+
```
|
78
|
+
|
79
|
+
```erb
|
80
|
+
<%=it "advantages", b: It.tag(:b, class: "red") %>
|
81
|
+
```
|
82
|
+
|
83
|
+
Even nested interpolations are possible:
|
84
|
+
|
85
|
+
```yaml
|
86
|
+
en:
|
87
|
+
copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"
|
88
|
+
```
|
89
|
+
|
90
|
+
```erb
|
91
|
+
<%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', b: It.tag(:b) %>
|
92
|
+
```
|
93
|
+
|
94
|
+
To use **it** outside of the view layer, just use `It.it`:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
flash[:notice] = It.it('flash.invitation_accepted_already', link: root_path)
|
98
|
+
```
|
99
|
+
|
100
|
+
If you would like to use the same translations in your html and plain text mails, you will like the `It.plain` method:
|
101
|
+
```yaml
|
102
|
+
en:
|
103
|
+
mail_copy: "Do you like %{link:Rails}?"
|
104
|
+
```
|
105
|
+
|
106
|
+
```erb
|
107
|
+
https://github.com/rails/rails
|
108
|
+
<%= it "mail_copy", link: It.link("http://www.rubyonrails.org/") %>
|
109
|
+
|
110
|
+
Plain mail:
|
111
|
+
<%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>
|
112
|
+
```
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
Contribute
|
117
|
+
----------
|
118
|
+
|
119
|
+
* Fork it
|
120
|
+
* Create a feature branch (`git checkout -b my-new-feature`)
|
121
|
+
* Create one or more failing specs. If you change code (feature or bug), I won't accept a pull request unless you changed specs.
|
122
|
+
* Fix the specs by implementing the feature/bug fix
|
123
|
+
* Push and open a pull request
|
data/Rakefile
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
1
2
|
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
require 'reek/rake/task'
|
2
5
|
|
3
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
RuboCop::RakeTask.new
|
8
|
+
Reek::Rake::Task.new
|
4
9
|
|
5
|
-
task :
|
10
|
+
task default: %i[spec rubocop reek]
|
data/it.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'it/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'it'
|
7
|
+
spec.version = It::VERSION
|
8
|
+
spec.authors = ['Johannes Barre']
|
9
|
+
spec.email = ['igel@igels.net']
|
10
|
+
|
11
|
+
spec.summary = 'A helper for links and other html tags in your translations'
|
12
|
+
spec.homepage = 'https://github.com/igel/it'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = %w[MIT-LICENSE README.md Rakefile Gemfile CHANGELOG.md it.gemspec] + Dir['lib/**/*.rb']
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.required_ruby_version = '>= 2.5.0'
|
19
|
+
spec.add_dependency 'actionpack', '>= 3.0.0'
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler'
|
22
|
+
spec.add_development_dependency 'coveralls'
|
23
|
+
spec.add_development_dependency 'nokogiri'
|
24
|
+
spec.add_development_dependency 'rake', '~> 12'
|
25
|
+
spec.add_development_dependency 'reek'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
27
|
+
spec.add_development_dependency 'rubocop'
|
28
|
+
end
|
data/lib/it.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
require 'it/parser'
|
2
|
+
require 'it/interpolation'
|
1
3
|
require 'it/tag'
|
2
4
|
require 'it/link'
|
3
5
|
require 'it/plain'
|
4
6
|
require 'it/helper'
|
7
|
+
require 'it/version'
|
5
8
|
|
6
9
|
ActiveSupport.on_load(:action_view) do
|
7
10
|
include It::Helper
|
@@ -11,15 +14,17 @@ end
|
|
11
14
|
module It
|
12
15
|
# It outside of your views. See documentation at Helper#it
|
13
16
|
def self.it(identifier, options = {})
|
14
|
-
|
15
|
-
|
17
|
+
Parser.new(
|
18
|
+
I18n.t(identifier, **Parser.backend_options(options).symbolize_keys),
|
19
|
+
options.stringify_keys
|
20
|
+
).process
|
16
21
|
end
|
17
22
|
|
18
23
|
# Creates a new link to be used in +it+.
|
19
24
|
#
|
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
|
21
|
-
# a Hash like <code>{:
|
22
|
-
# links like that.
|
25
|
+
# * +href+: The url for the link. You may specify it as a String or as a named route like +article_path+. It's not
|
26
|
+
# possible to specify a Hash like <code>{controller: "articles", action: "index"}</code> directly. Use the
|
27
|
+
# +url_for+ helper, if you would like to specify your links like that.
|
23
28
|
# * +options+: The options as an Hash. Use them like you would with +link_to+. <em>(optional)</em>
|
24
29
|
def self.link(href, options = {})
|
25
30
|
It::Link.new(href, options)
|
@@ -27,9 +32,9 @@ module It
|
|
27
32
|
|
28
33
|
# Creates a new plain replacement to be used in +it+.
|
29
34
|
#
|
30
|
-
# * +template+: A string to be used as the template. An example would be
|
31
|
-
# <code>"%s"</code>. <em>(optional)</em>
|
32
|
-
def self.plain(template =
|
35
|
+
# * +template+: A string to be used as the template. An example would be
|
36
|
+
# <code>"%s[http://www.rubyonrails.org]"</code>. Defaults to <code>"%s"</code>. <em>(optional)</em>
|
37
|
+
def self.plain(template = '%s')
|
33
38
|
It::Plain.new(template)
|
34
39
|
end
|
35
40
|
|
@@ -40,39 +45,4 @@ module It
|
|
40
45
|
def self.tag(tag_name, options = {})
|
41
46
|
It::Tag.new(tag_name, options)
|
42
47
|
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
48
|
end
|
data/lib/it/helper.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
1
|
module It
|
4
2
|
# The helper will be available in the views.
|
5
3
|
module Helper
|
@@ -13,34 +11,38 @@ module It
|
|
13
11
|
#
|
14
12
|
# # translation: "Already signed up? %{login_link:Sign in}!"
|
15
13
|
#
|
16
|
-
# <%=it("translation", :
|
14
|
+
# <%=it("translation", login_link: It.link(login_path))
|
17
15
|
#
|
18
|
-
# If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with
|
19
|
-
# you may specify the argument as a String or your helper.
|
16
|
+
# If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with
|
17
|
+
# +_link+, you may specify the argument as a String or your helper.
|
20
18
|
#
|
21
19
|
# # translation: "Already signed up? %{login_link:Sign in}!"
|
22
20
|
#
|
23
|
-
# <%=it("translation", :
|
21
|
+
# <%=it("translation", login_link: login_path)
|
24
22
|
#
|
25
|
-
# You may have as many tags inside of one translation as you like, and you even may nest them into each other.
|
26
|
-
# may specify arguments to links and other tags.
|
23
|
+
# You may have as many tags inside of one translation as you like, and you even may nest them into each other.
|
24
|
+
# Also you may specify arguments to links and other tags.
|
27
25
|
#
|
28
|
-
# # translation: "The top
|
26
|
+
# # translation: "The top %{wiki_link:our wiki} contributor is %{user_link:%{b:%{name}}}. Thanks %{name}!"
|
29
27
|
#
|
30
|
-
# <%= it("translation", :
|
28
|
+
# <%= it("translation", wiki_link: wiki_path, name: user.name, b: It.tag(:b, class: "user"),
|
29
|
+
# user_link: It.link(user_path(user), target: "_blank"))
|
31
30
|
#
|
32
|
-
# I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or
|
33
|
-
# things seperated over multiple lines should go into different translations. Use
|
34
|
-
# <code><b></code>, <code><i></code>, <code><em></code> or
|
31
|
+
# I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or
|
32
|
+
# <code><br /></code>, but I think, things seperated over multiple lines should go into different translations. Use
|
33
|
+
# it for inline tags like links, <code><span></code>, <code><b></code>, <code><i></code>, <code><em></code> or
|
34
|
+
# <code><strong></code>.
|
35
35
|
#
|
36
|
-
# It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify
|
37
|
-
# link target as an Hash.
|
36
|
+
# It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify
|
37
|
+
# your link target as an Hash.
|
38
38
|
#
|
39
39
|
# If you need to use it outside of your views, use +It.it+.
|
40
40
|
#
|
41
41
|
def it(identifier, options = {})
|
42
|
-
|
43
|
-
|
42
|
+
It::Parser.new(
|
43
|
+
t(identifier, **It::Parser.backend_options(options).symbolize_keys),
|
44
|
+
options.stringify_keys
|
45
|
+
).process
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module It
|
2
|
+
# Contains one interpolation and will delegate the work to the It::Tag (or subclass) or
|
3
|
+
# ERB::Util.h
|
4
|
+
class Interpolation
|
5
|
+
class << self
|
6
|
+
def call(string, values)
|
7
|
+
key, label = extract_key_and_label_from(string)
|
8
|
+
value = values[key]
|
9
|
+
|
10
|
+
raise KeyError, "key{#{key}} not found" unless values.key?(key)
|
11
|
+
|
12
|
+
new(key, value, label).process
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# This is a :reek:UtilityFunction, but it's not an instance method
|
18
|
+
def extract_key_and_label_from(string)
|
19
|
+
# eg: %{key:label} or %{key} or %{key: label}
|
20
|
+
string[2..-2].split(/:\s*/, 2)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(key, value, label)
|
25
|
+
@key = key
|
26
|
+
@value = value
|
27
|
+
@label = label
|
28
|
+
end
|
29
|
+
|
30
|
+
def process
|
31
|
+
convert_link
|
32
|
+
|
33
|
+
validate_value_for_arguments
|
34
|
+
|
35
|
+
if value.is_a?(It::Tag)
|
36
|
+
process_it_tag
|
37
|
+
else # Normal interpolations, as I18n.t would do it.
|
38
|
+
ERB::Util.h(value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
attr_reader :key, :value, :label
|
45
|
+
|
46
|
+
# Convert keys with String arguments into It::Links, if they are named link, link_* or *_link
|
47
|
+
def convert_link
|
48
|
+
@value = It::Link.new(value) if key =~ /(\Alink\z|_link\z|\Alink_)/ && value.is_a?(String)
|
49
|
+
end
|
50
|
+
|
51
|
+
def process_it_tag
|
52
|
+
if label
|
53
|
+
value.process(label.html_safe)
|
54
|
+
else
|
55
|
+
value.process
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate_value_for_arguments
|
60
|
+
if label && !value.is_a?(It::Tag)
|
61
|
+
raise ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{value.class}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/it/link.rb
CHANGED
@@ -2,17 +2,20 @@ module It
|
|
2
2
|
# A class for links
|
3
3
|
class Link < Tag
|
4
4
|
include ActionView::Helpers::UrlHelper
|
5
|
-
|
5
|
+
|
6
|
+
attr_reader :href
|
7
|
+
|
6
8
|
# See It.link for details. You can do everything there and save 6 characters.
|
7
9
|
def initialize(href, options = {})
|
8
|
-
raise TypeError,
|
10
|
+
raise TypeError, 'Invalid href given' unless [Hash, String, ActiveSupport::SafeBuffer].include?(href.class)
|
11
|
+
|
9
12
|
super(:a, options)
|
10
13
|
@href = href
|
11
14
|
end
|
12
|
-
|
15
|
+
|
13
16
|
# Will be called from inside the helper to return the tag with the given content.
|
14
17
|
def process(content)
|
15
|
-
link_to(content,
|
18
|
+
link_to(content, href, options)
|
16
19
|
end
|
17
20
|
end
|
18
|
-
end
|
21
|
+
end
|
data/lib/it/parser.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module It
|
2
|
+
# Parses the string and replaces all interpolations accordingly.
|
3
|
+
class Parser
|
4
|
+
attr_reader :string, :options
|
5
|
+
|
6
|
+
INTERPOLATION_REGEXP = /%\{[^{}]+\}/.freeze
|
7
|
+
|
8
|
+
def self.backend_options(options)
|
9
|
+
options.with_indifferent_access.slice(:default, :locale, :scope)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(string, options)
|
13
|
+
@string = string
|
14
|
+
@options = options
|
15
|
+
end
|
16
|
+
|
17
|
+
def process
|
18
|
+
handle_pluralization
|
19
|
+
escape_string
|
20
|
+
|
21
|
+
# For deep nesting, we repeat the process until we have no interpolations anymore
|
22
|
+
while contains_interpolation?
|
23
|
+
string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
|
24
|
+
Interpolation.call(interpolation, options)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
string.html_safe
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def contains_interpolation?
|
33
|
+
string =~ INTERPOLATION_REGEXP
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_pluralization
|
37
|
+
return if !string.is_a?(Hash) || !options.key?('count')
|
38
|
+
|
39
|
+
@string = I18n.backend.send(:pluralize, locale, string, options['count'])
|
40
|
+
end
|
41
|
+
|
42
|
+
def locale
|
43
|
+
options['locale'] || I18n.locale
|
44
|
+
end
|
45
|
+
|
46
|
+
def escape_string
|
47
|
+
@string = String.new(ERB::Util.h(string))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/it/plain.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module It
|
2
|
+
# Handles replacements in non HTML templates (e.g. mails)
|
2
3
|
class Plain < Tag
|
3
|
-
def initialize(template =
|
4
|
+
def initialize(template = '%s')
|
4
5
|
raise TypeError, "expected a String, got #{template.class}" unless template.is_a?(String)
|
6
|
+
|
5
7
|
@template = template
|
6
8
|
end
|
7
|
-
|
8
|
-
def process(content)
|
9
|
-
|
9
|
+
|
10
|
+
def process(content = '')
|
11
|
+
format(@template, content)
|
10
12
|
end
|
11
13
|
end
|
12
|
-
end
|
14
|
+
end
|
data/lib/it/tag.rb
CHANGED
@@ -2,24 +2,25 @@ module It
|
|
2
2
|
# A generic class for html tags.
|
3
3
|
class Tag
|
4
4
|
include ActionView::Helpers::TagHelper
|
5
|
-
|
5
|
+
|
6
6
|
attr_reader :tag_name, :options
|
7
|
-
|
7
|
+
|
8
8
|
# See It.tag for details. You can do everything there and save 6 characters.
|
9
9
|
def initialize(tag_name, options = {})
|
10
|
-
raise TypeError,
|
11
|
-
raise TypeError,
|
10
|
+
raise TypeError, 'tag_name must be a String or Symbol' unless [String, Symbol].include?(tag_name.class)
|
11
|
+
raise TypeError, 'options must be a Hash' unless options.is_a?(Hash)
|
12
|
+
|
12
13
|
@tag_name = tag_name.to_sym
|
13
14
|
@options = options.symbolize_keys
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
# Will be called from inside the helper to return the tag with the given content.
|
17
18
|
def process(content = nil) # :nodoc:
|
18
|
-
if content
|
19
|
-
tag(@tag_name, @options)
|
20
|
-
else
|
19
|
+
if content
|
21
20
|
content_tag(@tag_name, content, @options)
|
21
|
+
else
|
22
|
+
tag(@tag_name, @options)
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
data/lib/it/version.rb
ADDED
metadata
CHANGED
@@ -1,117 +1,169 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: it
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 3
|
10
|
-
version: 0.2.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Johannes Barre
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2020-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: actionpack
|
22
|
-
|
23
|
-
|
24
|
-
none: false
|
25
|
-
requirements:
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
26
17
|
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 7
|
29
|
-
segments:
|
30
|
-
- 3
|
31
|
-
- 0
|
32
|
-
- 0
|
18
|
+
- !ruby/object:Gem::Version
|
33
19
|
version: 3.0.0
|
34
20
|
type: :runtime
|
35
|
-
|
36
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nokogiri
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '12'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: reek
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
37
98
|
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.2'
|
104
|
+
type: :development
|
38
105
|
prerelease: false
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
50
118
|
type: :development
|
51
|
-
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
52
125
|
description:
|
53
|
-
email:
|
126
|
+
email:
|
127
|
+
- igel@igels.net
|
54
128
|
executables: []
|
55
|
-
|
56
129
|
extensions: []
|
57
|
-
|
58
|
-
|
59
|
-
-
|
60
|
-
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- CHANGELOG.md
|
133
|
+
- Gemfile
|
61
134
|
- MIT-LICENSE
|
62
|
-
- README.
|
135
|
+
- README.md
|
63
136
|
- Rakefile
|
64
|
-
-
|
137
|
+
- it.gemspec
|
65
138
|
- lib/it.rb
|
66
139
|
- lib/it/helper.rb
|
67
|
-
- lib/it/
|
140
|
+
- lib/it/interpolation.rb
|
68
141
|
- lib/it/link.rb
|
142
|
+
- lib/it/parser.rb
|
69
143
|
- lib/it/plain.rb
|
70
|
-
-
|
71
|
-
-
|
72
|
-
- spec/it/tag_spec.rb
|
73
|
-
- spec/it/plain_spec.rb
|
74
|
-
- spec/it_spec.rb
|
75
|
-
- spec/spec_helper.rb
|
144
|
+
- lib/it/tag.rb
|
145
|
+
- lib/it/version.rb
|
76
146
|
homepage: https://github.com/igel/it
|
77
|
-
licenses:
|
78
|
-
|
147
|
+
licenses:
|
148
|
+
- MIT
|
149
|
+
metadata: {}
|
79
150
|
post_install_message:
|
80
151
|
rdoc_options: []
|
81
|
-
|
82
|
-
require_paths:
|
152
|
+
require_paths:
|
83
153
|
- lib
|
84
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
-
|
86
|
-
requirements:
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
87
156
|
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
version: "0"
|
93
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
|
-
requirements:
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 2.5.0
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
96
161
|
- - ">="
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
segments:
|
100
|
-
- 1
|
101
|
-
- 3
|
102
|
-
- 6
|
103
|
-
version: 1.3.6
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
104
164
|
requirements: []
|
105
|
-
|
106
|
-
rubyforge_project:
|
107
|
-
rubygems_version: 1.8.10
|
165
|
+
rubygems_version: 3.1.2
|
108
166
|
signing_key:
|
109
|
-
specification_version:
|
167
|
+
specification_version: 4
|
110
168
|
summary: A helper for links and other html tags in your translations
|
111
|
-
test_files:
|
112
|
-
- spec/it/helper_spec.rb
|
113
|
-
- spec/it/link_spec.rb
|
114
|
-
- spec/it/tag_spec.rb
|
115
|
-
- spec/it/plain_spec.rb
|
116
|
-
- spec/it_spec.rb
|
117
|
-
- spec/spec_helper.rb
|
169
|
+
test_files: []
|
data/README.textile
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
h1. What is *it*?
|
2
|
-
|
3
|
-
I18n is baked right into Rails, and it's great. But if you want to place markup or links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
|
4
|
-
|
5
|
-
<pre><code>en:
|
6
|
-
copy: "If you are already registered, %{login_link}!"
|
7
|
-
copy_login_link: "please sign in"</code></pre>
|
8
|
-
|
9
|
-
<pre><code><%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %></code></pre>
|
10
|
-
|
11
|
-
Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? *it* lets you do that:
|
12
|
-
|
13
|
-
<pre><code>en:
|
14
|
-
copy: "If you are already registered, %{login_link:please sign in}!"</code></pre>
|
15
|
-
|
16
|
-
<pre><code><%=it "copy", login_link: login_path %></code></pre>
|
17
|
-
|
18
|
-
You may have noticed in the example above, that *it* doesn't require @raw@ anymore. Of course, all HTML in the translation gets properly escaped, so you don't have to worry about XSS.
|
19
|
-
|
20
|
-
h2. Installation
|
21
|
-
|
22
|
-
Just add the following line to your Gemfile & run @bundle install@:
|
23
|
-
|
24
|
-
<pre><code>gem 'it'</code></pre>
|
25
|
-
|
26
|
-
h2. Usage
|
27
|
-
|
28
|
-
You may have as many links inside your translations as you like, and normal interpolations are possible as well:
|
29
|
-
|
30
|
-
<pre><code>en:
|
31
|
-
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>
|
32
|
-
|
33
|
-
<pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html"), advices: 100, repo: It.link("https://github.com/lifo/docrails/tree/master/railties/guides") %></code></pre>
|
34
|
-
|
35
|
-
As you see above, unless the interpolation name is @link@ or starts with @_link@ or ends with @_link@, you need to call @It.link@ to create a link. The advantage of @It.link@: You may specify options like you would with @link_to@:
|
36
|
-
|
37
|
-
<pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html", target: '_blank', class: "important") %></code></pre>
|
38
|
-
|
39
|
-
You may pass any kind of object accepted by @link_to@ as the link target, so your loved named routes like @article_path(:id => article.id)@ will all work fine.
|
40
|
-
|
41
|
-
Want to introduce some markup into your sentences? *it* will help you:
|
42
|
-
|
43
|
-
<pre><code>en:
|
44
|
-
advantages: There are %{b:many advantages} for registered users!</code></pre>
|
45
|
-
|
46
|
-
<pre><code><%=it "advantages", b: It.tag(:b, class: "red") %></code></pre>
|
47
|
-
|
48
|
-
Even nested interpolations are possible:
|
49
|
-
|
50
|
-
<pre><code>en:
|
51
|
-
copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"</code></pre>
|
52
|
-
|
53
|
-
<pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', :b => It.tag(:b) %></code></pre>
|
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:
|
56
|
-
<pre><code>en:
|
57
|
-
mail_copy: "Do you like %{link:Rails}?"</code></pre>
|
58
|
-
|
59
|
-
<pre><code>HTML mail:
|
60
|
-
<%= it "mail_copy", link: It.link("http://www.rubyonrails.org/") %>
|
61
|
-
|
62
|
-
Plain mail:
|
63
|
-
<%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>
|
64
|
-
</code></pre>
|
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.
|
67
|
-
|
68
|
-
h2. isit18.com?
|
69
|
-
|
70
|
-
Well, the specs pass on 1.8.7, but I never tried it in a real app. Report any issues on "github":https://github.com/iGEL/it/issues
|
71
|
-
|
72
|
-
h2. Abandoned & outdated?
|
73
|
-
|
74
|
-
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. Sadly, rubygems is full of such gems. If you improved *it* send me a pull request. If I have not time to support the lib anymore, I will happily hand the project over to a new maintainer.
|
75
|
-
|
76
|
-
h2. Broken English?
|
77
|
-
|
78
|
-
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.
|
79
|
-
|
80
|
-
h2. Like it?
|
81
|
-
|
82
|
-
Like it? Flattr it! !http://api.flattr.com/button/flattr-badge-large.png!:http://flattr.com/thing/351483/it-HTML-tags-for-your-translations
|
data/spec/it/helper_spec.rb
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'it'
|
5
|
-
|
6
|
-
describe It::Helper, "#it" 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.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
|
-
end
|
23
|
-
|
24
|
-
it "should insert the link into the German translation" do
|
25
|
-
I18n.locale = :de
|
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
|
-
end
|
28
|
-
|
29
|
-
it "should allow link options to be set" do
|
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
|
-
end
|
32
|
-
|
33
|
-
it "should support the plain thing" do
|
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
|
-
end
|
36
|
-
|
37
|
-
it "should parse other tags as well" do
|
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
|
-
end
|
40
|
-
|
41
|
-
it "should mark the result as html safe" do
|
42
|
-
@view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?.should be_true
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should escape all html in the translation" do
|
46
|
-
I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
|
47
|
-
@view.it("test2", :link => It.link("http://www.rubyonrails.org")).should == '<a href="hax0r"> & <a href="http://www.rubyonrails.org">link -> Rails</a> in <b>the middle</b>.'
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should also work with 2 links" do
|
51
|
-
I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
|
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
|
-
end
|
54
|
-
|
55
|
-
it "should allow normal I18n interpolations" do
|
56
|
-
I18n.backend.store_translations(:en, :test4 => "I'm containing a %{link:link to Rails} in the %{position}.")
|
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
|
-
end
|
59
|
-
|
60
|
-
it "should allow Intergers as normal interpolation" do
|
61
|
-
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
62
|
-
@view.it("test5", :name => 2).should == 'Hello 2.'
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should escape the HTML in normal interpolations" do
|
66
|
-
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
67
|
-
@view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>').should == 'Hello <a href="http://evil.haxor.com">victim</a>.'
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should not escape html_safe interpolations" do
|
71
|
-
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
|
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
|
-
end
|
74
|
-
|
75
|
-
it "should allow interpolations inside of links" do
|
76
|
-
I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
|
77
|
-
@view.it("test6", :link => It.link("/article/2"), :article => "article").should == 'Did you read our <a href="/article/2">nice article</a>?'
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should raise a KeyError, if the key was not given" do
|
81
|
-
expect { @view.it("test1", :blubb => true) }.to raise_error(KeyError, "key{link} not found")
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
|
85
|
-
I18n.backend.store_translations(:en, :test7 => "Sign up %{asdf:here}!")
|
86
|
-
expect { @view.it("test7", :asdf => "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should allow Strings, if the interpolation name is link" do
|
90
|
-
I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
|
91
|
-
@view.it("test8", :link => "/register").should == 'Sign up <a href="/register">here</a>!'
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should allow Strings, if the interpolation name ends with _link" do
|
95
|
-
I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
|
96
|
-
@view.it("test8", :register_link => "/register").should == 'Sign up <a href="/register">here</a>!'
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should allow Strings, if the interpolation name starts with link_" do
|
100
|
-
I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
|
101
|
-
@view.it("test8", :link_to_register => "/register").should == 'Sign up <a href="/register">here</a>!'
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should work with tags without arguments" do
|
105
|
-
I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
|
106
|
-
@view.it("test9", :br => It.tag(:br)).should == 'We can <br /> do line breaks'
|
107
|
-
end
|
108
|
-
|
109
|
-
it 'should support dot-prefixed keys' do
|
110
|
-
I18n.backend.store_translations(:en, :widgets => { :show => { :all_widgets => "See %{widgets_link:all widgets}" } })
|
111
|
-
@view.instance_variable_set(:"@_virtual_path", "widgets/show")
|
112
|
-
@view.it('.all_widgets', :widgets_link => '/widgets').should == 'See <a href="/widgets">all widgets</a>'
|
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
|
136
|
-
end
|
data/spec/it/link_spec.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'it'
|
5
|
-
|
6
|
-
describe It::Link, '.new' do
|
7
|
-
it "should accept a String as frist param" do
|
8
|
-
expect { It::Link.new("http://www.rubyonrails.org/") }.not_to raise_error
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should accept a Hash as first param" do
|
12
|
-
expect { It::Link.new({:controller => "articles", :action => "index"}) }.not_to raise_error
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should raise a TypeError if the first param is an Integer" do
|
16
|
-
expect { It::Link.new(1) }.to raise_error(TypeError)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should accept options as a Hash" do
|
20
|
-
expect { It::Link.new("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should raise a TypeError, if the options are a String" do
|
24
|
-
expect { It::Link.new("http://www.rubyonrails.org/", "classy") }.to raise_error(TypeError)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should raise ArgumentError, if called with three params" do
|
28
|
-
expect { It::Link.new("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe It::Link, '#tag_name' do
|
33
|
-
it "should always return a" do
|
34
|
-
It::Link.new("http://www.rubyonrails.org/").tag_name.should == :a
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe It::Link, '#process' do
|
39
|
-
it "should return a link with the options set and the content as label" do
|
40
|
-
It::Link.new("http://www.rubyonrails.org", :target => "_blank").process("Rails").should == '<a href="http://www.rubyonrails.org" target="_blank">Rails</a>'
|
41
|
-
end
|
42
|
-
end
|
data/spec/it/plain_spec.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'it'
|
3
|
-
|
4
|
-
describe It::Plain, '.new' do
|
5
|
-
it "should work with no params" do
|
6
|
-
expect { It::Plain.new }.not_to raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should work with a String" do
|
10
|
-
expect { It::Plain.new("%s[http://www.rubyonrails.org]") }.not_to raise_error
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should raise ArgumentError with 2 params" do
|
14
|
-
expect { It::Plain.new("asdf", "asdf")}.to raise_error(ArgumentError, "wrong number of arguments (2 for 1)")
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should raise a TypeError, if the first param is not a String" do
|
18
|
-
expect { It::Plain.new(1)}.to raise_error(TypeError)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe It::Plain, '#process' do
|
23
|
-
it "should return 'Ruby on Rails', if no param was given" do
|
24
|
-
It::Plain.new.process("Ruby on Rails").should == 'Ruby on Rails'
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should return 'Ruby on Rails[http://www.rubyonrails.org/]'" do
|
28
|
-
It::Plain.new("%s[http://www.rubyonrails.org/]").process("Ruby on Rails").should == 'Ruby on Rails[http://www.rubyonrails.org/]'
|
29
|
-
end
|
30
|
-
end
|
data/spec/it/tag_spec.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'it'
|
5
|
-
|
6
|
-
describe It::Tag, '.new' do
|
7
|
-
it "should work with a parameter (Symbol)" do
|
8
|
-
expect { It::Tag.new(:b) }.not_to raise_error
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should work with a paramter (String)" do
|
12
|
-
expect { It::Tag.new("b") }.not_to raise_error
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should raise TypeError if called with an Integer" do
|
16
|
-
expect { It::Tag.new(1) }.to raise_error(TypeError)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should accept an options Hash" do
|
20
|
-
expect { It::Tag.new(:b, :class => "very_bold") }.not_to raise_error
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should raise a TypeError if called with a String" do
|
24
|
-
expect { It::Tag.new(:b, "very_bold") }.to raise_error(TypeError)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should raise an ArgumentError if called with three params" do
|
28
|
-
expect { It::Tag.new(:b, {}, :blubb) }.to raise_error(ArgumentError)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe It::Tag, '#tag_name' do
|
33
|
-
it "should return the tag as a Symbol if given as a Symbol" do
|
34
|
-
It::Tag.new(:i).tag_name.should == :i
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should return the tag_name as a Symbol if given as a String" do
|
38
|
-
It::Tag.new("i").tag_name.should == :i
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe It::Tag, '#options' do
|
43
|
-
it "should return the options with symbolized keys" do
|
44
|
-
It::Tag.new(:i, "id" => "cool", :class => "classy").options.should == {:id => "cool", :class => "classy"}
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe It::Tag, '#process' do
|
49
|
-
it "should return a tag with the options as attributes and the param as content" do
|
50
|
-
It::Tag.new(:i, "id" => "cool", "class" => "classy").process("some text").should == '<i class="classy" id="cool">some text</i>'
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should be marked as html safe" do
|
54
|
-
It::Tag.new(:i).process("some text").html_safe.should be_true
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should escape HTML" do
|
58
|
-
It::Tag.new(:i).process("some text & <b>html</b>").should == '<i>some text & <b>html</b></i>'
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should not escape strings marked as HTML safe" do
|
62
|
-
It::Tag.new(:i).process("some text & <b>html</b>".html_safe).should == '<i>some text & <b>html</b></i>'
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should return an empty tag, if no content is provided" do
|
66
|
-
It::Tag.new(:br, "id" => "cool").process.should == '<br id="cool" />'
|
67
|
-
end
|
68
|
-
end
|
data/spec/it_spec.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'it'
|
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
|
-
|
13
|
-
describe It, '.link' do
|
14
|
-
it "should return an It::Link object" do
|
15
|
-
It.link("https://www.github.com").class.should == It::Link
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should accept one param" do
|
19
|
-
expect { It.link("http://www.rubyonrails.org/") }.not_to raise_error
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should accept two params" do
|
23
|
-
expect { It.link("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should raise ArgumentError, if called with three params" do
|
27
|
-
expect { It.link("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe It, '.tag' do
|
32
|
-
it "should return an It::Tag object" do
|
33
|
-
It.tag(:b).class.should == It::Tag
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should work with a param" do
|
37
|
-
expect { It.tag(:b) }.not_to raise_error
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should accept two params" do
|
41
|
-
expect { It.tag(:b, :class => "very_bold") }.not_to raise_error
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should raise an ArgumentError if called with three params" do
|
45
|
-
expect { It.tag(:b, {}, :blubb) }.to raise_error(ArgumentError)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe It, '.plain' do
|
50
|
-
it "should return an It::Plain object" do
|
51
|
-
It.plain.class.should == It::Plain
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should work without params" do
|
55
|
-
expect { It.plain }.not_to raise_error
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should accept one param" do
|
59
|
-
expect { It.plain("%s[http://www.rubyonrails.org/]") }.not_to raise_error
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should raise ArgumentError, if called with two params" do
|
63
|
-
expect { It.plain("%s[http://www.rubyonrails.org/]", :blubb) }.to raise_error(ArgumentError)
|
64
|
-
end
|
65
|
-
end
|