i18n-callbacks 0.1.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: ae3d088f84070a37a78555a10a1204db1dd1a4a0
4
+ data.tar.gz: ce8b40ef7a18a5c6cec0b8e99f1df79537cd1464
5
+ SHA512:
6
+ metadata.gz: 7aeff25f7a8655937b9304d0e218352f16f69b704cf15cc04b489e6bc8e809068971cc8b52a7f7d7ec903693ae9f864c85017a47c54b1d7cef80f0273d8942ac
7
+ data.tar.gz: 611036153f983d64034ded43492523f2472cccee484a3f6043c76f3c01e3efc155e1a806eb068e2674a0f16f45cce7114e4d88e7f92c03419bbd641435e31024
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /pkg/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at joe.g.yates@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i18n-callbacks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joe Yates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # I18n::Callbacks
2
+
3
+ Wrap your `I18n.t` calls in `before` and/or `after` hooks.
4
+
5
+ This gem provides the `I18n::Backend::Callbacks` backend.
6
+
7
+ You pass it an I18n backend to call plus before/after callbacks to pre-/post-
8
+ process the calls.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'i18n-callbacks'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install i18n-callbacks
25
+
26
+ ## Usage
27
+
28
+ Supply either a `:before` or `:after` proc to the constructor, process I18n.t
29
+ calls.
30
+
31
+ * `:before` - takes 3 parameters: locale, key and options,
32
+ * `:after` - takes 4 parameters: locale, key and options, and the result of
33
+ the call to the backend that you are wrapping.
34
+
35
+ Suppose you want all translated strings to be prefixed by the key used to
36
+ access them:
37
+
38
+ ```ruby
39
+ require "i18n"
40
+ require "i18n/callbacks"
41
+
42
+ wrapped = I18n::Backend::Simple.new
43
+
44
+ # ...load you translations (normally from YAML files)
45
+ wrapped.store_translations(:en, {foo: "bar"})
46
+
47
+ I18n.backend = I18n::Backend::Callbacks.new(
48
+ wrapped,
49
+ after: ->(locale, key, options={}, result) { "#{key}: #{result}" }
50
+ )
51
+
52
+ I18n.t(:foo) # => "foo: bar"
53
+ ```
54
+
55
+ ## Development
56
+
57
+ Run `rake spec` to run the tests.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`.
60
+ To release a new version, update the version number in `version.rb`, and then
61
+ run `bundle exec rake release`, which will create a git tag for the version,
62
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at
67
+ https://github.com/cantierecreativo/i18n-callbacks. This project is intended to
68
+ be a safe, welcoming space for collaboration, and contributors are expected to
69
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "i18n/callbacks/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i18n-callbacks"
8
+ spec.version = I18n::Callbacks::VERSION
9
+ spec.authors = ["Joe Yates"]
10
+ spec.email = ["joe.g.yates@gmail.com"]
11
+
12
+ spec.summary = %q{Pre- and post-process you calls to I18n.t()}
13
+ spec.homepage = "https://github.com/cantierecreativo/i18n-callbacks"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.lines.map(&:chomp!)
17
+ spec.test_files = `git ls-files spec`.lines.map(&:chomp!)
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "i18n"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,31 @@
1
+ require "i18n"
2
+
3
+ class I18n::Backend::Callbacks < Delegator
4
+ attr_reader :wrapped
5
+ attr_reader :before
6
+ attr_reader :after
7
+
8
+ def initialize(wrapped, before: nil, after: nil)
9
+ super(wrapped)
10
+ @wrapped = wrapped
11
+ @before = before
12
+ @after = after
13
+ end
14
+
15
+ def translate(locale, key, options = {})
16
+ locale, key, options = before.call(locale, key, options) if before
17
+ result = wrapped.translate(locale, key, options)
18
+ result = after.call(locale, key, options, result) if after
19
+ result
20
+ end
21
+
22
+ # implement the Delegator interface
23
+
24
+ def __setobj__(obj)
25
+ @wrapped = obj
26
+ end
27
+
28
+ def __getobj__
29
+ wrapped
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ module I18n
2
+ module Callbacks
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "i18n/callbacks/version"
2
+ require "i18n/backend/callbacks"
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe I18n::Backend::Callbacks do
4
+ let(:wrapped) do
5
+ instance_double(
6
+ I18n::Backend::Simple,
7
+ available_locales: [:en],
8
+ translate: original
9
+ )
10
+ end
11
+ let(:key) { "hello" }
12
+ let(:original) { "foo" }
13
+ let(:before) { nil }
14
+ let(:after) { nil }
15
+
16
+ before do
17
+ I18n.backend = described_class.new(wrapped, before: before, after: after)
18
+ end
19
+ after { I18n.backend = nil }
20
+
21
+ context "with a pre hook defined" do
22
+ let(:before) do
23
+ ->(locale, key, options = {}) { [:fr, key, options] }
24
+ end
25
+
26
+ it "allows pre-processing of I18n.t calls" do
27
+ I18n.t(key)
28
+
29
+ expect(wrapped).to have_received(:translate).with(:fr, key, {})
30
+ end
31
+ end
32
+
33
+ context "with a post hook defined" do
34
+ let(:after) do
35
+ ->(locale, key, options = {}, result) { "#{key} -> #{result}" }
36
+ end
37
+
38
+ it "allows post-processing of I18n.t calls" do
39
+ expect(I18n.t(key)).to eq("#{key} -> #{original}")
40
+ end
41
+ end
42
+
43
+ context "without hooks defined" do
44
+ it "proxies to the backend" do
45
+ expect(I18n.t("hello")).to eq(original)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ describe I18n::Callbacks do
4
+ it "has a version number" do
5
+ expect(I18n::Callbacks::VERSION).not_to be_nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'i18n/callbacks'
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-callbacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joe Yates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-19 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - joe.g.yates@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - i18n-callbacks.gemspec
85
+ - lib/i18n/backend/callbacks.rb
86
+ - lib/i18n/callbacks.rb
87
+ - lib/i18n/callbacks/version.rb
88
+ - spec/backend/callbacks_spec.rb
89
+ - spec/callbacks_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/cantierecreativo/i18n-callbacks
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Pre- and post-process you calls to I18n.t()
115
+ test_files:
116
+ - spec/backend/callbacks_spec.rb
117
+ - spec/callbacks_spec.rb
118
+ - spec/spec_helper.rb