rails_i18n_extended 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rails_i18n_extended/version.rb +3 -0
- data/lib/rails_i18n_extended.rb +194 -0
- data/rails_i18n_extended.gemspec +33 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2a792c6b59b81e49afa55da44408822b8aed8cdcd2e70670ee737b782bd6384e
|
4
|
+
data.tar.gz: cf10a1e92e4adebefaec5f89e2deb9b5355dac1bd2d3fb4b687cec7804b916fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b9b867b963058cb11e710f5372ea75f53baa19bb05715379b9c842d6eb2cc5fd63d189dad7f8103fad969c3aa7b93e85aecc8a7dde487325c9aadab27ee0419
|
7
|
+
data.tar.gz: 3f1da885ede191017ae6f25e0300510761df7216b0b0d35c9c6ae59c1b4f6fee1c59aa31e2e4b945909e033b877626d2f26533d35640b4c5a785537187fa74d5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_i18n_extended (0.1.0)
|
5
|
+
activerecord
|
6
|
+
sqlite3
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.0.3)
|
12
|
+
activesupport (= 6.0.3)
|
13
|
+
activerecord (6.0.3)
|
14
|
+
activemodel (= 6.0.3)
|
15
|
+
activesupport (= 6.0.3)
|
16
|
+
activesupport (6.0.3)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
22
|
+
concurrent-ruby (1.1.6)
|
23
|
+
diff-lcs (1.3)
|
24
|
+
i18n (1.8.2)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.14.1)
|
27
|
+
rake (12.3.3)
|
28
|
+
rspec (3.9.0)
|
29
|
+
rspec-core (~> 3.9.0)
|
30
|
+
rspec-expectations (~> 3.9.0)
|
31
|
+
rspec-mocks (~> 3.9.0)
|
32
|
+
rspec-core (3.9.2)
|
33
|
+
rspec-support (~> 3.9.3)
|
34
|
+
rspec-expectations (3.9.2)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.9.0)
|
37
|
+
rspec-mocks (3.9.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-support (3.9.3)
|
41
|
+
sqlite3 (1.4.2)
|
42
|
+
thread_safe (0.3.6)
|
43
|
+
tzinfo (1.2.7)
|
44
|
+
thread_safe (~> 0.1)
|
45
|
+
zeitwerk (2.3.0)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
ruby
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
rails_i18n_extended!
|
52
|
+
rake (~> 12.0)
|
53
|
+
rspec (~> 3.0)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Johan VAN RYSEGHEM
|
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,50 @@
|
|
1
|
+
# RailsI18nExtended
|
2
|
+
|
3
|
+
The gem adds a bunch of helpers to ease the use of I18n in your rails project.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rails_i18n_extended'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rails_i18n_extended
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The following helpers are available:
|
24
|
+
- `Model.t` will get the model name
|
25
|
+
- `Model.ts` will get the model name (pluralized)
|
26
|
+
- `Model.t_action(action_name)` will get localized action
|
27
|
+
- `Model.t_panel(panel_name)` will get localized panel name
|
28
|
+
- `Model.t_attr(attr)` will get name of the attribute
|
29
|
+
- `model.t_attr(attr)` (on an instance) will get name of the attribute
|
30
|
+
- `model.t_enum(attr)` (on an instance) will get value of an enum attribue, localized
|
31
|
+
|
32
|
+
You can also call the method `t` on booleans, or on a string to get its translation as a key.
|
33
|
+
You can call `l` on all time-related classes to get their localized representation.
|
34
|
+
|
35
|
+
We also added a "default fallback" behaviour: when looking for the key `a.b.c.key`, before failing, I18n will look for `a.b.default.key`, `a.default.key` and `default.key`
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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/honestica/rails_i18n_extended.
|
46
|
+
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_i18n_extended"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
require "i18n"
|
2
|
+
require "rails_i18n_extended/version"
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module I18n
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def translate_with_default key, options={}, original=nil
|
10
|
+
begin
|
11
|
+
self.translate_without_default(key, **{raise: true}.update(options))
|
12
|
+
rescue => e
|
13
|
+
split = key.to_s.split('.')
|
14
|
+
if split.size <= 2
|
15
|
+
translate_without_default original || key, **options
|
16
|
+
else
|
17
|
+
v = split.pop
|
18
|
+
v2 = split.pop
|
19
|
+
split.pop if v2 == 'defaults'
|
20
|
+
split << 'defaults' << v
|
21
|
+
new_key = split.join('.')
|
22
|
+
translate_with_default new_key, options, original || key
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
alias_method :translate_without_default, :translate
|
27
|
+
alias_method :translate, :translate_with_default
|
28
|
+
alias_method :t, :translate
|
29
|
+
|
30
|
+
def translate_with_fallback(key = nil, **options)
|
31
|
+
locale = options[:locale] || I18n.locale
|
32
|
+
intended_to_raise = options[:raise]
|
33
|
+
translate_without_fallback(key, options.dup.update(raise: true))
|
34
|
+
rescue I18n::MissingTranslationData => e
|
35
|
+
raise if locale == I18n.default_locale && intended_to_raise
|
36
|
+
|
37
|
+
begin
|
38
|
+
return translate_without_fallback(key, options.dup.update(locale: I18n.default_locale, raise: true))
|
39
|
+
rescue I18n::MissingTranslationData
|
40
|
+
raise e if intended_to_raise
|
41
|
+
end
|
42
|
+
|
43
|
+
translate_without_fallback(key, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
alias_method :translate_without_fallback, :translate
|
47
|
+
alias_method :translate, :translate_with_fallback
|
48
|
+
alias_method :t, :translate
|
49
|
+
|
50
|
+
def model_key(obj)
|
51
|
+
obj.model_name.to_s.underscore.gsub('/', '_')
|
52
|
+
end
|
53
|
+
|
54
|
+
def attribute_key(obj, attr)
|
55
|
+
"activerecord.attributes.#{model_key(obj)}.#{attr}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def custom_attribute_key(obj, attr)
|
59
|
+
"activerecord.custom_display_attributes.#{model_key(obj)}.#{attr}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def flash(key, params={})
|
63
|
+
scope = [:flash] + params[:scope].to_a
|
64
|
+
|
65
|
+
t(key, params.update(scope: scope))
|
66
|
+
end
|
67
|
+
|
68
|
+
def notice(key, params={})
|
69
|
+
scope = [:notice] + params[:scope].to_a
|
70
|
+
|
71
|
+
flash(key, params.update(scope: scope))
|
72
|
+
end
|
73
|
+
|
74
|
+
def alert(key, params={})
|
75
|
+
scope = [:alert] + params[:scope].to_a
|
76
|
+
|
77
|
+
flash(key, params.update(scope: scope))
|
78
|
+
end
|
79
|
+
|
80
|
+
def error(key, params={})
|
81
|
+
scope = [:error] + params[:scope].to_a
|
82
|
+
|
83
|
+
flash(key, params.update(scope: scope))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class String
|
89
|
+
def t(params={})
|
90
|
+
I18n.t(self, **params)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Time
|
95
|
+
def l(params={})
|
96
|
+
I18n.l(self, **params)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class DateTime
|
101
|
+
def l(params={})
|
102
|
+
I18n.l(self, **params)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Date
|
107
|
+
def l(params={})
|
108
|
+
I18n.l(self, **params)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Date
|
113
|
+
def l(params={})
|
114
|
+
I18n.l(self, **params)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class TrueClass
|
119
|
+
def t(params={})
|
120
|
+
I18n.t(self.to_s, **params)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class FalseClass
|
125
|
+
def t(params={})
|
126
|
+
I18n.t(self.to_s, **params)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
module RailsI18nExtended
|
131
|
+
extend ActiveSupport::Concern
|
132
|
+
|
133
|
+
module ClassMethods
|
134
|
+
def t(params={})
|
135
|
+
"activerecord.models.#{model_name.to_s.underscore.gsub('/', '_')}".t({count: 1}.update(params))
|
136
|
+
end
|
137
|
+
|
138
|
+
def tp(params={})
|
139
|
+
t(count: 2)
|
140
|
+
end
|
141
|
+
|
142
|
+
def t_scope(scope, params={})
|
143
|
+
I18n.t("activerecord.scopes.#{I18n.model_key(self)}.#{scope}", **params)
|
144
|
+
end
|
145
|
+
|
146
|
+
def t_panel(panel, params={})
|
147
|
+
I18n.t("panels.#{I18n.model_key(self)}.#{panel}", **params)
|
148
|
+
end
|
149
|
+
|
150
|
+
def t_action(panel, params={})
|
151
|
+
I18n.t("actions.#{I18n.model_key(self)}.#{panel}", **params)
|
152
|
+
end
|
153
|
+
|
154
|
+
def t_confirm(panel, params={})
|
155
|
+
I18n.t("confirm.#{I18n.model_key(self)}.#{panel}", **params)
|
156
|
+
end
|
157
|
+
|
158
|
+
def t_message(panel, params={})
|
159
|
+
I18n.t("message.#{I18n.model_key(self)}.#{panel}", **params)
|
160
|
+
end
|
161
|
+
|
162
|
+
def t_attr(attr, params={})
|
163
|
+
I18n.t(I18n.attribute_key(self, attr), **params)
|
164
|
+
end
|
165
|
+
|
166
|
+
def t_custom_attr(attr, params={})
|
167
|
+
I18n.t(I18n.custom_attribute_key(self, attr), **params)
|
168
|
+
end
|
169
|
+
|
170
|
+
def t_enum(attr, value, params={})
|
171
|
+
return unless value.present?
|
172
|
+
|
173
|
+
I18n.t([I18n.attribute_key(self, attr.to_s.pluralize), value].join('.'), **params)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def t_attr(attr, params={})
|
178
|
+
self.class.t_attr(attr, params)
|
179
|
+
end
|
180
|
+
|
181
|
+
def t_custom_attr(attr, params={})
|
182
|
+
self.class.t_custom_attr(attr, params)
|
183
|
+
end
|
184
|
+
|
185
|
+
def t_enum(attr, params={})
|
186
|
+
return unless send(attr).present?
|
187
|
+
|
188
|
+
self.class.t_enum(attr, send(attr))
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
class ActiveRecord::Base
|
193
|
+
include RailsI18nExtended
|
194
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'lib/rails_i18n_extended/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rails_i18n_extended"
|
5
|
+
spec.version = RailsI18nExtended::VERSION
|
6
|
+
spec.authors = ["Johan VAN RYSEGHEM"]
|
7
|
+
spec.email = ["johan@about-blank.fr"]
|
8
|
+
|
9
|
+
spec.summary = 'Add a few helpers for I18n in RoR'
|
10
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
11
|
+
spec.homepage = "https://github.com/honestica/rails_i18n_extended"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/honestica/rails_i18n_extended"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/honestica/rails_i18n_extended"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
# spec.add_dependency "rails"
|
31
|
+
spec.add_dependency "activerecord"
|
32
|
+
spec.add_dependency "sqlite3"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_i18n_extended
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johan VAN RYSEGHEM
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
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: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- johan@about-blank.fr
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/rails_i18n_extended.rb
|
59
|
+
- lib/rails_i18n_extended/version.rb
|
60
|
+
- rails_i18n_extended.gemspec
|
61
|
+
homepage: https://github.com/honestica/rails_i18n_extended
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
allowed_push_host: https://rubygems.org
|
66
|
+
homepage_uri: https://github.com/honestica/rails_i18n_extended
|
67
|
+
source_code_uri: https://github.com/honestica/rails_i18n_extended
|
68
|
+
changelog_uri: https://github.com/honestica/rails_i18n_extended
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.3.0
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.1.2
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Add a few helpers for I18n in RoR
|
88
|
+
test_files: []
|