i18n-dot_lookup 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: b4ab84a8459f24c2db3c073e280c84e78a7285d8
4
+ data.tar.gz: e858b17e09147d9b9c99d29ca944a0787c7a4fa8
5
+ SHA512:
6
+ metadata.gz: dac66431e5d88673b1b5f68ee14f37b29ffa4390ac2890ce7a443569c027a651a251932ec1b77afe9d3882f866c8c2b56debb05479b648118ccb02dfd11ea437
7
+ data.tar.gz: f1bdb3a3678133f6b1e1d3bf0b6edd2b283c33eefd5d92ef7bccae192cbb7d933c5c07ccef64273de90ca992866e956799253420ba3653131a9522422714bdf7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - 2.2.2
6
+ before_install: gem install bundler
7
+ notifications:
8
+ email: false
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i18n-dot_lookup.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nando Vieira
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,51 @@
1
+ # i18n-dot_lookup
2
+
3
+ ![build status](https://travis-ci.org/fnando/i18n-dot_lookup.svg)
4
+
5
+ Ever wanted to get properties from an object, like `%{user.name}`? Now you can!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'i18n-dot_lookup'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install i18n-dot_lookup
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'i18n-dot_lookup'
27
+ require 'ostruct'
28
+
29
+ # You don't need to do this, but can use it to test on your console.
30
+ I18n.backend.store_translations :en, {hello: 'hello %{user.name}'}
31
+
32
+ # The user object will probably come from your database
33
+ I18n.t :hello, user: OpenStruct.new(name: 'john')
34
+ #=> hello john
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/i18n-dot_lookup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ require './lib/i18n-dot_lookup/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'i18n-dot_lookup'
5
+ spec.version = I18nDotLookup::VERSION
6
+ spec.authors = ['Nando Vieira']
7
+ spec.email = ['fnando.vieira@gmail.com']
8
+
9
+ spec.summary = "Allow interpolation to be performed on a object's attribute, e.g. %{user.name}"
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/fnando/i18n-dot_lookup'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_dependency 'i18n'
19
+ spec.add_development_dependency 'bundler', '~> 1.10'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'minitest'
22
+ spec.add_development_dependency 'minitest-utils'
23
+ spec.add_development_dependency 'pry-meta'
24
+ end
@@ -0,0 +1,2 @@
1
+ require 'i18n/backend/dot_lookup'
2
+ I18n.backend = I18n::Backend::DotLookup.new
@@ -0,0 +1,3 @@
1
+ module I18nDotLookup
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'i18n'
2
+
3
+ module I18n
4
+ module Backend
5
+ class DotLookup < Simple
6
+ KEY_EXTRACTOR = /%\{(\w+(?:\.\w+)+)\}/
7
+ UNKNOWN_PROPERTY = Object.new
8
+ DOT = '.'.freeze
9
+
10
+ _verbose, $VERBOSE = $VERBOSE, nil
11
+ ::I18n::INTERPOLATION_PATTERN = Regexp.union(KEY_EXTRACTOR, ::I18n::INTERPOLATION_PATTERN)
12
+ $VERBOSE = _verbose
13
+
14
+ def translate(locale, key, options = {})
15
+ raise InvalidLocale.new(locale) unless locale
16
+ entry = key && lookup(locale, key, options[:scope], options)
17
+
18
+ if options.empty?
19
+ entry = resolve(locale, key, entry, options)
20
+ else
21
+ count, default = options.values_at(:count, :default)
22
+ values = options.except(*RESERVED_KEYS)
23
+ entry = entry.nil? && default ?
24
+ default(locale, key, default, options) : resolve(locale, key, entry, options)
25
+ end
26
+
27
+ throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.nil?
28
+ entry = entry.dup if entry.is_a?(String)
29
+
30
+ populate_options(entry, values) if entry.kind_of?(String)
31
+ entry = pluralize(locale, entry, count) if count
32
+ entry = interpolate(locale, entry, values) if values
33
+ entry
34
+ end
35
+
36
+ def populate_options(subject, options)
37
+ extract_keys(subject).each_with_object(options) do |key, buffer|
38
+ names = key.split(DOT)
39
+ object = buffer[names.shift.to_sym]
40
+ value = catch(:unknown_property) { get_value(object, names) }
41
+ buffer[key.to_sym] = value if value != UNKNOWN_PROPERTY
42
+ end
43
+ end
44
+
45
+ def extract_keys(subject)
46
+ subject.scan(KEY_EXTRACTOR).flatten
47
+ end
48
+
49
+ def get_value(object, keys)
50
+ value = keys.inject(object) do |target, key|
51
+ throw :unknown_property, UNKNOWN_PROPERTY unless target && target.respond_to?(key)
52
+ target.public_send(key)
53
+ end
54
+
55
+ value
56
+ end
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-dot_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-11 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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
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: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-utils
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-meta
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Allow interpolation to be performed on a object's attribute, e.g. %{user.name}
98
+ email:
99
+ - fnando.vieira@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - i18n-dot_lookup.gemspec
112
+ - lib/i18n-dot_lookup.rb
113
+ - lib/i18n-dot_lookup/version.rb
114
+ - lib/i18n/backend/dot_lookup.rb
115
+ homepage: https://github.com/fnando/i18n-dot_lookup
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.6
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Allow interpolation to be performed on a object's attribute, e.g. %{user.name}
139
+ test_files: []