i18n-callbacks 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/examples/rails-18n-key-debug.md +50 -0
- data/lib/i18n/backend/callbacks.rb +2 -2
- data/lib/i18n/callbacks/version.rb +1 -1
- data/spec/backend/callbacks_spec.rb +19 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49c2ce75710545c387cd79fe44ac93eb819ab084
|
4
|
+
data.tar.gz: 840f8dafc369a0c7c7be9c5e3f94c1f06f2804fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2092f3d9e02d80ca01a126e75dc7725b6de6010f2e08d79a582fd67658fb71ce599e085330fe94ffa353c67cb41d043d64940a4bd488ab9acf8c9c9bac938e08
|
7
|
+
data.tar.gz: f16d457f2e03e3be225461d792188fc08958315b7e2d6590ce56814538b199b0a72dd6be0b102e95d426001ed66315c1b93537748ba66194d66b8175d6ccd0dc
|
data/README.md
CHANGED
@@ -41,7 +41,7 @@ require "i18n/callbacks"
|
|
41
41
|
|
42
42
|
wrapped = I18n::Backend::Simple.new
|
43
43
|
|
44
|
-
# ...load
|
44
|
+
# ...load your translations (normally from YAML files)
|
45
45
|
wrapped.store_translations(:en, {foo: "bar"})
|
46
46
|
|
47
47
|
I18n.backend = I18n::Backend::Callbacks.new(
|
@@ -52,6 +52,8 @@ I18n.backend = I18n::Backend::Callbacks.new(
|
|
52
52
|
I18n.t(:foo) # => "foo: bar"
|
53
53
|
```
|
54
54
|
|
55
|
+
See also the `examples` directory.
|
56
|
+
|
55
57
|
## Development
|
56
58
|
|
57
59
|
Run `rake spec` to run the tests.
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Rails I18n Key Debug
|
2
|
+
|
3
|
+
Often the strings used in a Rails app come from many different places: from
|
4
|
+
your app and from various different gems (i.e. 'activerecord', 'devise',
|
5
|
+
or 'kaminari').
|
6
|
+
|
7
|
+
When this happens, you often want to know what keys are producing the
|
8
|
+
translations you are seeing in your views.
|
9
|
+
|
10
|
+
# Setup
|
11
|
+
|
12
|
+
Add an initializer for I18n (config/initializers/i18n.rb):
|
13
|
+
|
14
|
+
```
|
15
|
+
if Rails.env.development?
|
16
|
+
I18n.backend = I18n::Backend::Callbacks.new(I18n::Backend::Simple.new)
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
Add an around hook in ApplicationController:
|
21
|
+
|
22
|
+
```
|
23
|
+
class ApplicationController < ActionController::Base
|
24
|
+
around_action :debug_i18n
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def debug_i18n
|
29
|
+
if !Rails.env.development?
|
30
|
+
yield
|
31
|
+
return
|
32
|
+
end
|
33
|
+
|
34
|
+
if params[:debug_i18n]
|
35
|
+
I18n.backend.after = ->(locale, key, options={}, result) do
|
36
|
+
"[#{key}] #{result}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
yield
|
41
|
+
|
42
|
+
I18n.backend.after = nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
# Use
|
48
|
+
|
49
|
+
When you want to see which keys are in use, add `?debug_i18n=1` in your
|
50
|
+
browser's address bar.
|
@@ -42,7 +42,25 @@ describe I18n::Backend::Callbacks do
|
|
42
42
|
|
43
43
|
context "without hooks defined" do
|
44
44
|
it "proxies to the backend" do
|
45
|
-
expect(I18n.t(
|
45
|
+
expect(I18n.t(key)).to eq(original)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "setting hooks via attributes" do
|
50
|
+
it "works for the pre hook" do
|
51
|
+
I18n.backend.before = ->(locale, key, options = {}) do
|
52
|
+
[:fr, key, options]
|
53
|
+
end
|
54
|
+
|
55
|
+
I18n.t(key)
|
56
|
+
|
57
|
+
expect(wrapped).to have_received(:translate).with(:fr, key, {})
|
58
|
+
end
|
59
|
+
|
60
|
+
it "works for the post hook" do
|
61
|
+
I18n.backend.after = ->(*args) { "ciao" }
|
62
|
+
|
63
|
+
expect(I18n.t(key)).to eq("ciao")
|
46
64
|
end
|
47
65
|
end
|
48
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n-callbacks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Yates
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- LICENSE.txt
|
82
82
|
- README.md
|
83
83
|
- Rakefile
|
84
|
+
- examples/rails-18n-key-debug.md
|
84
85
|
- i18n-callbacks.gemspec
|
85
86
|
- lib/i18n/backend/callbacks.rb
|
86
87
|
- lib/i18n/callbacks.rb
|