rails_i18n_extended 0.1.1 → 0.1.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rails_i18n_extended/i18n_overrides.rb +83 -0
- data/lib/rails_i18n_extended/object_extensions.rb +109 -0
- data/lib/rails_i18n_extended/version.rb +1 -1
- data/lib/rails_i18n_extended.rb +2 -194
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f06aeae35fc27132e4b1c79b387dbedde3b950ca476803c7b1195652e89729a
|
4
|
+
data.tar.gz: 32937a649d66ce181748a1adbc1e11c67e9000a27e9fb43e11ef54a91989e6b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd4f03e9b9c2113f960d84aa79a1751967d7105608e4c84f3beeabe4a8c9dbc10dcf4986ecd2f6fb2953486a71a1ae4c1b9288133561407348c383a149f49bdc
|
7
|
+
data.tar.gz: 41117349d808239ade16f34567f17f0c68da874a9ca266f039f3c84016aaf4c808b197fb03b06e1d3021bba11bb29393ad6b987d3d79bb51169e873b9da0dabe
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "i18n"
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def translate_with_default key, options={}, original=nil
|
7
|
+
begin
|
8
|
+
self.translate_without_default(key, **{raise: true}.update(options))
|
9
|
+
rescue => e
|
10
|
+
split = key.to_s.split('.')
|
11
|
+
if split.size <= 2
|
12
|
+
translate_without_default original || key, **options
|
13
|
+
else
|
14
|
+
v = split.pop
|
15
|
+
v2 = split.pop
|
16
|
+
split.pop if v2 == 'defaults'
|
17
|
+
split << 'defaults' << v
|
18
|
+
new_key = split.join('.')
|
19
|
+
translate_with_default new_key, options, original || key
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias_method :translate_without_default, :translate
|
24
|
+
alias_method :translate, :translate_with_default
|
25
|
+
alias_method :t, :translate
|
26
|
+
|
27
|
+
def translate_with_fallback(key = nil, **options)
|
28
|
+
locale = options[:locale] || I18n.locale
|
29
|
+
intended_to_raise = options[:raise]
|
30
|
+
translate_without_fallback(key, options.dup.update(raise: true))
|
31
|
+
rescue I18n::MissingTranslationData => e
|
32
|
+
raise if locale == I18n.default_locale && intended_to_raise
|
33
|
+
|
34
|
+
begin
|
35
|
+
return translate_without_fallback(key, options.dup.update(locale: I18n.default_locale, raise: true))
|
36
|
+
rescue I18n::MissingTranslationData
|
37
|
+
raise e if intended_to_raise
|
38
|
+
end
|
39
|
+
|
40
|
+
translate_without_fallback(key, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :translate_without_fallback, :translate
|
44
|
+
alias_method :translate, :translate_with_fallback
|
45
|
+
alias_method :t, :translate
|
46
|
+
|
47
|
+
def model_key(obj)
|
48
|
+
obj.model_name.to_s.underscore.gsub('/', '_')
|
49
|
+
end
|
50
|
+
|
51
|
+
def attribute_key(obj, attr)
|
52
|
+
"activerecord.attributes.#{model_key(obj)}.#{attr}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def custom_attribute_key(obj, attr)
|
56
|
+
"activerecord.custom_display_attributes.#{model_key(obj)}.#{attr}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def flash(key, params={})
|
60
|
+
scope = [:flash] + params[:scope].to_a
|
61
|
+
|
62
|
+
t(key, params.update(scope: scope))
|
63
|
+
end
|
64
|
+
|
65
|
+
def notice(key, params={})
|
66
|
+
scope = [:notice] + params[:scope].to_a
|
67
|
+
|
68
|
+
flash(key, params.update(scope: scope))
|
69
|
+
end
|
70
|
+
|
71
|
+
def alert(key, params={})
|
72
|
+
scope = [:alert] + params[:scope].to_a
|
73
|
+
|
74
|
+
flash(key, params.update(scope: scope))
|
75
|
+
end
|
76
|
+
|
77
|
+
def error(key, params={})
|
78
|
+
scope = [:error] + params[:scope].to_a
|
79
|
+
|
80
|
+
flash(key, params.update(scope: scope))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
class String
|
4
|
+
def t(params={})
|
5
|
+
I18n.t(self, **params)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Time
|
10
|
+
def l(params={})
|
11
|
+
I18n.l(self, **params)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class DateTime
|
16
|
+
def l(params={})
|
17
|
+
I18n.l(self, **params)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Date
|
22
|
+
def l(params={})
|
23
|
+
I18n.l(self, **params)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Date
|
28
|
+
def l(params={})
|
29
|
+
I18n.l(self, **params)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class TrueClass
|
34
|
+
def t(params={})
|
35
|
+
I18n.t(self.to_s, **params)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class FalseClass
|
40
|
+
def t(params={})
|
41
|
+
I18n.t(self.to_s, **params)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module RailsI18nExtended
|
46
|
+
extend ActiveSupport::Concern
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def t(params={})
|
50
|
+
"activerecord.models.#{model_name.to_s.underscore.gsub('/', '_')}".t({count: 1}.update(params))
|
51
|
+
end
|
52
|
+
|
53
|
+
def tp(params={})
|
54
|
+
t(count: 2)
|
55
|
+
end
|
56
|
+
|
57
|
+
def t_scope(scope, params={})
|
58
|
+
I18n.t("activerecord.scopes.#{I18n.model_key(self)}.#{scope}", **params)
|
59
|
+
end
|
60
|
+
|
61
|
+
def t_panel(panel, params={})
|
62
|
+
I18n.t("panels.#{I18n.model_key(self)}.#{panel}", **params)
|
63
|
+
end
|
64
|
+
|
65
|
+
def t_action(panel, params={})
|
66
|
+
I18n.t("actions.#{I18n.model_key(self)}.#{panel}", **params)
|
67
|
+
end
|
68
|
+
|
69
|
+
def t_confirm(panel, params={})
|
70
|
+
I18n.t("confirm.#{I18n.model_key(self)}.#{panel}", **params)
|
71
|
+
end
|
72
|
+
|
73
|
+
def t_message(panel, params={})
|
74
|
+
I18n.t("message.#{I18n.model_key(self)}.#{panel}", **params)
|
75
|
+
end
|
76
|
+
|
77
|
+
def t_attr(attr, params={})
|
78
|
+
I18n.t(I18n.attribute_key(self, attr), **params)
|
79
|
+
end
|
80
|
+
|
81
|
+
def t_custom_attr(attr, params={})
|
82
|
+
I18n.t(I18n.custom_attribute_key(self, attr), **params)
|
83
|
+
end
|
84
|
+
|
85
|
+
def t_enum(attr, value, params={})
|
86
|
+
return unless value.present?
|
87
|
+
|
88
|
+
I18n.t([I18n.attribute_key(self, attr.to_s.pluralize), value].join('.'), **params)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def t_attr(attr, params={})
|
93
|
+
self.class.t_attr(attr, params)
|
94
|
+
end
|
95
|
+
|
96
|
+
def t_custom_attr(attr, params={})
|
97
|
+
self.class.t_custom_attr(attr, params)
|
98
|
+
end
|
99
|
+
|
100
|
+
def t_enum(attr, params={})
|
101
|
+
return unless send(attr).present?
|
102
|
+
|
103
|
+
self.class.t_enum(attr, send(attr))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class ActiveRecord::Base
|
108
|
+
include RailsI18nExtended
|
109
|
+
end
|
data/lib/rails_i18n_extended.rb
CHANGED
@@ -1,194 +1,2 @@
|
|
1
|
-
require "
|
2
|
-
require "rails_i18n_extended/
|
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
|
1
|
+
require "rails_i18n_extended/i18n_overrides"
|
2
|
+
require "rails_i18n_extended/object_extensions"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_i18n_extended
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johan VAN RYSEGHEM
|
@@ -58,6 +58,8 @@ files:
|
|
58
58
|
- bin/console
|
59
59
|
- bin/setup
|
60
60
|
- lib/rails_i18n_extended.rb
|
61
|
+
- lib/rails_i18n_extended/i18n_overrides.rb
|
62
|
+
- lib/rails_i18n_extended/object_extensions.rb
|
61
63
|
- lib/rails_i18n_extended/version.rb
|
62
64
|
- rails_i18n_extended.gemspec
|
63
65
|
homepage: https://github.com/honestica/rails_i18n_extended
|