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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae3d088f84070a37a78555a10a1204db1dd1a4a0
4
- data.tar.gz: ce8b40ef7a18a5c6cec0b8e99f1df79537cd1464
3
+ metadata.gz: 49c2ce75710545c387cd79fe44ac93eb819ab084
4
+ data.tar.gz: 840f8dafc369a0c7c7be9c5e3f94c1f06f2804fc
5
5
  SHA512:
6
- metadata.gz: 7aeff25f7a8655937b9304d0e218352f16f69b704cf15cc04b489e6bc8e809068971cc8b52a7f7d7ec903693ae9f864c85017a47c54b1d7cef80f0273d8942ac
7
- data.tar.gz: 611036153f983d64034ded43492523f2472cccee484a3f6043c76f3c01e3efc155e1a806eb068e2674a0f16f45cce7114e4d88e7f92c03419bbd641435e31024
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 you translations (normally from YAML files)
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.
@@ -2,8 +2,8 @@ require "i18n"
2
2
 
3
3
  class I18n::Backend::Callbacks < Delegator
4
4
  attr_reader :wrapped
5
- attr_reader :before
6
- attr_reader :after
5
+ attr_accessor :before
6
+ attr_accessor :after
7
7
 
8
8
  def initialize(wrapped, before: nil, after: nil)
9
9
  super(wrapped)
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Callbacks
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -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("hello")).to eq(original)
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.1.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-19 00:00:00.000000000 Z
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