i18n_utils 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -4
- data/i18n_utils.gemspec +1 -0
- data/lib/i18n_utils.rb +40 -2
- data/lib/i18n_utils/version.rb +1 -1
- data/spec/helpers_spec.rb +30 -4
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6b5a9200ee6ddbcbe323f65bf5831ad09606073
|
4
|
+
data.tar.gz: a1787e8442dc6f5a65c9008ba7eaf276d08d5fdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d984d9cac645e36a92baf65e5c0317d3ab5c30c5c6edb18e5109a790814d5dcc505abae0cff2cee3df2800060947635099e8c34cd6c783cbf7b9520442d1f6d2
|
7
|
+
data.tar.gz: 9effedf6df34082d6763e33640452709c94a63b7ee4e2506528322d5591dc95f76cd7defe2da5f6d9e7a2f5a949b0778263db856ebd99130abfc936d86dcbd60
|
data/README.md
CHANGED
@@ -5,19 +5,44 @@ I18n utilities for Ruby on Rails.
|
|
5
5
|
|
6
6
|
## Usage
|
7
7
|
|
8
|
+
Just `include I18nUtils::All` in e.g. your `ApplicationHelper` to get all the things.
|
9
|
+
|
10
|
+
Or see under each section to only get that part.
|
11
|
+
|
12
|
+
|
8
13
|
### `t_model`, `t_attribute`
|
9
14
|
|
10
|
-
|
15
|
+
Get this part with `include I18nUtils::Model` in e.g. your `ApplicationHelper`.
|
11
16
|
|
12
|
-
|
17
|
+
I think the Rails way to translate attributes and models is ugly and on the wrong object.
|
13
18
|
|
14
|
-
|
19
|
+
With this, your views can do `t_model(User)` instead of `User.model_name.human`.
|
15
20
|
|
16
21
|
Also `t_attribute(User, :email)` instead of `User.human_attribute_name(:email)`.
|
17
22
|
|
18
23
|
Want to use them outside views? Feel free to include the module anywhere you like, or call them via the `I18nUtils` object, e.g. `I18nUtils.t_model(User)`.
|
19
24
|
|
20
|
-
You could even do `I18n.extend
|
25
|
+
You could even do `I18n.extend(I18nUtils::Model)` to get e.g. `I18n.t_model(User)`.
|
26
|
+
|
27
|
+
|
28
|
+
### `t_scope`
|
29
|
+
|
30
|
+
Get this part with `include I18nUtils::Scope` in e.g. your `ApplicationHelper`.
|
31
|
+
|
32
|
+
Links or other markup in the middle of a translation is tricky. Either you put the HTML straight in the translation and risk the translator messing it up, or it becomes a mess:
|
33
|
+
|
34
|
+
``` slim
|
35
|
+
= t(:"welcome.sign_in_now.text",
|
36
|
+
sign_in: link_to(t(:"welcome.sign_in_now.sign_in"), sign_in_url))
|
37
|
+
```
|
38
|
+
|
39
|
+
The `t_scope` helper lets you use blocks for interpolated values, in your regular template:
|
40
|
+
|
41
|
+
``` slim
|
42
|
+
= t_scope(:"welcome.sign_in_now.text") do |scope|
|
43
|
+
scope.sign_in do
|
44
|
+
= link_to(scope.t(:sign_in), sign_in_url))
|
45
|
+
```
|
21
46
|
|
22
47
|
|
23
48
|
## Installation
|
data/i18n_utils.gemspec
CHANGED
data/lib/i18n_utils.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require "i18n_utils/version"
|
2
2
|
|
3
|
+
# For html_safe/SafeBuffer.
|
4
|
+
require "active_support/all"
|
5
|
+
|
3
6
|
module I18nUtils
|
4
|
-
module
|
7
|
+
module Model
|
5
8
|
def t_attribute(klass, attribute)
|
6
9
|
klass.human_attribute_name(attribute)
|
7
10
|
end
|
@@ -11,5 +14,40 @@ module I18nUtils
|
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
14
|
-
|
17
|
+
module Scope
|
18
|
+
def t_scope(key)
|
19
|
+
base_key = key.to_s.rpartition(".").first
|
20
|
+
scope = ScopeContext.new(base_key, self)
|
21
|
+
yield(scope)
|
22
|
+
t(key, scope.to_hash).html_safe
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module All
|
27
|
+
include Model
|
28
|
+
include Scope
|
29
|
+
end
|
30
|
+
|
31
|
+
extend Model
|
32
|
+
|
33
|
+
class ScopeContext
|
34
|
+
def initialize(base_key, helper)
|
35
|
+
@base_key = base_key
|
36
|
+
@helper = helper
|
37
|
+
@hash = {}
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_hash
|
41
|
+
@hash
|
42
|
+
end
|
43
|
+
|
44
|
+
def t(key, opts = {})
|
45
|
+
@helper.t("#{@base_key}.#{key}", opts)
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(name, *args, &block)
|
49
|
+
html = @helper.capture(&block)
|
50
|
+
@hash[name] = html
|
51
|
+
end
|
52
|
+
end
|
15
53
|
end
|
data/lib/i18n_utils/version.rb
CHANGED
data/spec/helpers_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require "i18n_utils"
|
2
2
|
|
3
|
-
describe I18nUtils
|
4
|
-
|
3
|
+
describe I18nUtils do
|
4
|
+
include I18nUtils::All
|
5
5
|
|
6
6
|
describe "t_attribute" do
|
7
7
|
it "uses human_attribute_name" do
|
8
8
|
klass = double
|
9
9
|
allow(klass).to receive(:human_attribute_name).with(:foo).and_return("foux du fafa")
|
10
|
-
|
10
|
+
t_attribute(klass, :foo).should == "foux du fafa"
|
11
11
|
I18nUtils.t_attribute(klass, :foo).should == "foux du fafa"
|
12
12
|
end
|
13
13
|
end
|
@@ -16,8 +16,34 @@ describe I18nUtils::Helpers do
|
|
16
16
|
it "uses model_name.human" do
|
17
17
|
klass = double
|
18
18
|
klass.stub_chain(:model_name, :human).and_return("Clase")
|
19
|
-
|
19
|
+
t_model(klass).should == "Clase"
|
20
20
|
I18nUtils.t_model(klass).should == "Clase"
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
# Not as integrated as I might like to really trust these tests, but simple.
|
25
|
+
# Suggestions for improvement are very welcome.
|
26
|
+
describe "t_scope" do
|
27
|
+
def link_to(text, url)
|
28
|
+
%{<a href="#{url}">#{text}</a>}
|
29
|
+
end
|
30
|
+
|
31
|
+
it "captures content and abbreviates scopes" do
|
32
|
+
expect(self).to receive(:capture) { |&block| block.call }
|
33
|
+
|
34
|
+
expect(self).to receive(:t).
|
35
|
+
with("sign_in_now.text", sign_in: %{<a href="url">Sign in</a>}).
|
36
|
+
and_return("output")
|
37
|
+
|
38
|
+
expect(self).to receive(:t).
|
39
|
+
with("sign_in_now.sign_in", {}).
|
40
|
+
and_return("Sign in")
|
41
|
+
|
42
|
+
t_scope("sign_in_now.text") do |scope|
|
43
|
+
scope.sign_in do
|
44
|
+
link_to(scope.t(:sign_in), "url")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
23
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: active_support
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|