i18n-debug 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 398a60a26ca4f57920cec51d480b9704c9dba002
4
+ data.tar.gz: 3ea870dd5ea63a8b0f1fb8eb59bcb29c2d4541a7
5
+ SHA512:
6
+ metadata.gz: 055095420673af2a805e63727c2c07f01e3ed856c8704debebc26269adadbc30b26ab870b3c63eb1c45a00784b1797999e0716843af1012ce8798b6f7e994789
7
+ data.tar.gz: e396e01eb9d4353657b2ba4241638318ed03e78bf20f298e40dc291025e8f10d652ce004ee14f2ddbac1834d2f233c43c16d55f321911778d893ecd5053ef6c3
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Philipe Fatio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Ruby I18n Debug
2
+
3
+ **Ever wondered which translations are being looked up by Rails, a gem, or
4
+ simply your app? Wonder no more!**
5
+
6
+ Rails' implicit translations, for instance, are a nice feature. But sometimes
7
+ getting the key to, let's say, the `BillingAddress`' `street` attribute in
8
+ a nested form inside an `Order` can be quite tricky to get right on the first
9
+ try. The key for this example would be
10
+ `activerecord.attributes.order/billing_address.street`. Good luck figuring that
11
+ out!
12
+
13
+ With this gem you can easily see which translations are being looked up. The key
14
+ above would have created the following debug log entry:
15
+
16
+ ```
17
+ [i18n-debug] activerecord.attributes.order/billing_address.name => nil
18
+ ```
19
+
20
+ After setting the translation for that key you just discovered, the log entry
21
+ changes to this:
22
+
23
+ ```
24
+ [i18n-debug] activerecord.attributes.order/billing_address.name => "Order billing name"
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Simply add the gem to your `Gemfile`. You probably only want this in development.
30
+ Thus, place it inside the `development` group.
31
+
32
+ ```ruby
33
+ gem 'i18n-debug', group: :development
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ This gem works straight out of the box. If Rails is available, it will log using
39
+ `Rails.logger.debug`. Otherwise it will log using `Logger.new($stdout).debug`.
40
+
41
+ If you wish to use a custom logger, simply set it as follows (make sure it
42
+ responds to `debug`):
43
+
44
+ ```ruby
45
+ I18n::Debug.logger = my_custom_logger
46
+ ```
47
+
48
+ Every lookup invokes the lambda `I18n::Debug.on_lookup` with the key and the
49
+ translation value as arguments. The default lambda simply logs it to the logger
50
+ mentioned above. If you want to change the logging format or do something
51
+ totally different, simply set your own handler to do so:
52
+
53
+ ```ruby
54
+ # Collect stats on I18n key usage.
55
+ i18n_stats = Hash.new { |stats, key| stats[key] = 0 }
56
+
57
+ I18n::Debug.on_lookup = lambda do |key, value|
58
+ i18n_stats[key] += 1 if value
59
+ end
60
+ ```
61
+
62
+ ## Additional Information
63
+
64
+ This gem was inspired by a similar gem called
65
+ [rails-i18n-debug](https://github.com/256dpi/rails-i18n-debug).
66
+
67
+ ### Dependencies
68
+
69
+ - [i18n](https://github.com/svenfuchs/i18n)
70
+
71
+ ### Author
72
+
73
+ Philipe Fatio ([fphilipe](https://github.com/fphilipe))
74
+
75
+ ### License
76
+
77
+ MIT License. Copyright 2014 Philipe Fatio
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,16 @@
1
+ require_relative 'lib/i18n/debug/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'i18n-debug'
5
+ spec.version = I18n::Debug::VERSION
6
+ spec.author = 'Philipe Fatio'
7
+ spec.email = 'me@phili.pe'
8
+ spec.homepage = 'https://github.com/fphilipe/i18n-debug'
9
+ spec.license = 'MIT'
10
+ spec.files = `git ls-files -z`.split("\x0")
11
+ spec.test_files = `git ls-files -z -- test`.split("\x0")
12
+ spec.summary = %q{Ever wondered which translations are being looked up by Rails, a gem, or simply your app? Wonder no more!}
13
+ spec.description = spec.summary
14
+
15
+ spec.add_dependency 'i18n', '~> 0.0'
16
+ end
@@ -0,0 +1,5 @@
1
+ module I18n
2
+ module Debug
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
data/lib/i18n/debug.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'i18n'
2
+ require 'i18n/debug/version'
3
+
4
+ module I18n
5
+ module Debug
6
+ @on_lookup = lambda do |key, result|
7
+ logger.debug("[i18n-debug] #{key} => #{result.inspect}")
8
+ end
9
+
10
+ class << self
11
+ attr_accessor :logger, :on_lookup
12
+
13
+ def logger
14
+ @logger ||=
15
+ if defined?(::Rails)
16
+ ::Rails.logger
17
+ else
18
+ require 'logger'
19
+ ::Logger.new($stdout)
20
+ end
21
+ end
22
+ end
23
+
24
+ module Hook
25
+ def self.included(klass)
26
+ klass.class_eval do
27
+ alias_method :lookup_without_debug, :lookup
28
+ alias_method :lookup, :lookup_with_debug
29
+ end
30
+ end
31
+
32
+ def lookup_with_debug(*args)
33
+ lookup_without_debug(*args).tap do |result|
34
+ options = args.last.is_a?(Hash) ? args.pop : {}
35
+ separator = options[:separator] || I18n.default_separator
36
+ key = I18n.normalize_keys(*args, separator).join(separator)
37
+ Debug.on_lookup[key, result]
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Backend::Simple.include(Debug::Hook)
44
+ end
@@ -0,0 +1,42 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'i18n/debug'
3
+ require 'minitest/autorun'
4
+
5
+ class TestI18nDebug < Minitest::Test
6
+ alias_method :silence_io, :capture_io
7
+
8
+ def setup
9
+ I18n.backend.store_translations(:en, foo: { bar: 'baz' })
10
+ # Avoid I18n deprecation warning:
11
+ I18n.enforce_available_locales = true
12
+ # Reset logger to its initial state:
13
+ I18n::Debug.logger = nil
14
+ end
15
+
16
+ def test_does_not_alter_default_i18n_behavior
17
+ silence_io do
18
+ assert_equal I18n.t('foo.bar'), 'baz'
19
+ end
20
+ end
21
+
22
+ def test_logger_invoked
23
+ assert_output(/en\.foo\.bar => "baz"/) do
24
+ I18n.t('foo.bar')
25
+ end
26
+ end
27
+
28
+ def test_custom_lookup_hook_called
29
+ default_hook = I18n::Debug.on_lookup
30
+ hook_key, hook_value = nil
31
+ I18n::Debug.on_lookup = lambda do |key, value|
32
+ hook_key, hook_value = key, value
33
+ end
34
+
35
+ I18n.t('foo.bar')
36
+
37
+ assert_equal hook_key, 'en.foo.bar'
38
+ assert_equal hook_value, 'baz'
39
+ ensure
40
+ I18n::Debug.on_lookup = default_hook
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-debug
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Philipe Fatio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ description: Ever wondered which translations are being looked up by Rails, a gem,
28
+ or simply your app? Wonder no more!
29
+ email: me@phili.pe
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - i18n-debug.gemspec
40
+ - lib/i18n/debug.rb
41
+ - lib/i18n/debug/version.rb
42
+ - test/test_i18n_debug.rb
43
+ homepage: https://github.com/fphilipe/i18n-debug
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Ever wondered which translations are being looked up by Rails, a gem, or
67
+ simply your app? Wonder no more!
68
+ test_files:
69
+ - test/test_i18n_debug.rb