i18n-inflector-3 3.0.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/ChangeLog +1674 -0
- data/LGPL-LICENSE +169 -0
- data/README.md +259 -0
- data/docs/COPYING +57 -0
- data/docs/EXAMPLES +357 -0
- data/docs/HISTORY +280 -0
- data/docs/LEGAL +10 -0
- data/docs/LGPL +166 -0
- data/docs/TODO +8 -0
- data/docs/USAGE +967 -0
- data/docs/rdoc.css +20 -0
- data/lib/i18n-inflector/api.rb +753 -0
- data/lib/i18n-inflector/api_strict.rb +671 -0
- data/lib/i18n-inflector/backend.rb +352 -0
- data/lib/i18n-inflector/config.rb +289 -0
- data/lib/i18n-inflector/errors.rb +226 -0
- data/lib/i18n-inflector/hset.rb +24 -0
- data/lib/i18n-inflector/inflection_data.rb +357 -0
- data/lib/i18n-inflector/inflection_data_strict.rb +300 -0
- data/lib/i18n-inflector/inflector.rb +38 -0
- data/lib/i18n-inflector/interpolate.rb +546 -0
- data/lib/i18n-inflector/lazy_enum.rb +267 -0
- data/lib/i18n-inflector/long_comments.rb +57 -0
- data/lib/i18n-inflector/options.rb +329 -0
- data/lib/i18n-inflector/version.rb +27 -0
- data/lib/i18n-inflector.rb +19 -0
- metadata +87 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
|
5
|
+
# Copyright:: (c) 2011,2012,2013 by Paweł Wilk
|
|
6
|
+
# License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
|
|
7
|
+
#
|
|
8
|
+
# This file contains class that is used to keep
|
|
9
|
+
# inflection data for strict kinds.
|
|
10
|
+
|
|
11
|
+
# @abstract This namespace is shared with I18n subsystem.
|
|
12
|
+
module I18n
|
|
13
|
+
module Inflector
|
|
14
|
+
# This class contains structures for keeping parsed translation data
|
|
15
|
+
# and basic operations for strict kinds and tokens assigned to them.
|
|
16
|
+
# Methods in this class vary from methods from {I18n::Inflector::InflectionData}
|
|
17
|
+
# in a way that +kind+ argument is usually required, not optional, since
|
|
18
|
+
# managing the strict kinds requires a kind of any token to be always known.
|
|
19
|
+
class InflectionData_Strict
|
|
20
|
+
# This constant contains a dummy hash for an empty token. It makes
|
|
21
|
+
# chaining calls to internal data easier.
|
|
22
|
+
DUMMY_TOKEN = {kind: nil, target: nil, description: nil}.freeze
|
|
23
|
+
|
|
24
|
+
# This constant contains a dummy hash of hashes for tokens collection.
|
|
25
|
+
# It makes chaining calls to internal data easier.
|
|
26
|
+
DUMMY_TOKENS = Hash.new(DUMMY_TOKEN).freeze
|
|
27
|
+
|
|
28
|
+
# This constant contains a dummy hash. It makes
|
|
29
|
+
# chaining calls to internal data easier.
|
|
30
|
+
DUMMY_HASH = {}.freeze
|
|
31
|
+
|
|
32
|
+
# Locale this database works for.
|
|
33
|
+
attr_reader :locale
|
|
34
|
+
|
|
35
|
+
# Initializes internal structures.
|
|
36
|
+
#
|
|
37
|
+
# @param [Symbol,nil] locale the locale identifier for
|
|
38
|
+
# the object to be labeled with
|
|
39
|
+
def initialize(locale = nil)
|
|
40
|
+
@tokens = Hash.new(DUMMY_TOKENS)
|
|
41
|
+
@lazy_kinds = LazyArrayEnumerator.for(@tokens)
|
|
42
|
+
@defaults = {}
|
|
43
|
+
@locale = locale
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Adds an alias (overwriting existing alias).
|
|
47
|
+
#
|
|
48
|
+
# @param [Symbol] name the name of an alias
|
|
49
|
+
# @param [Symbol] target the target token for the created alias
|
|
50
|
+
# @param [Symbol] kind the identifier of a kind
|
|
51
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
52
|
+
# (in case of bad names or non-existent targets)
|
|
53
|
+
def add_alias(name, target, kind)
|
|
54
|
+
return false if name.nil? || target.nil? || kind.nil?
|
|
55
|
+
return false if name.to_s.empty? || target.to_s.empty? || kind.to_s.empty?
|
|
56
|
+
|
|
57
|
+
name = name.to_sym
|
|
58
|
+
target = target.to_sym
|
|
59
|
+
kind = kind.to_sym
|
|
60
|
+
k = @tokens[kind]
|
|
61
|
+
return false unless k.key?(target)
|
|
62
|
+
|
|
63
|
+
token = k[name] = {}
|
|
64
|
+
token[:description] = k[target][:description]
|
|
65
|
+
token[:target] = target
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Adds a token (overwriting existing token).
|
|
70
|
+
#
|
|
71
|
+
# @param [Symbol] token the name of a token to add
|
|
72
|
+
# @param [Symbol] kind the identifier of a kind
|
|
73
|
+
# @param [String] description the description of a token
|
|
74
|
+
# @return [Boolean] +true+ if everything went ok, +false+ otherwise
|
|
75
|
+
# (in case of bad names)
|
|
76
|
+
def add_token(token, kind, description)
|
|
77
|
+
return false if token.to_s.empty? || kind.to_s.empty? || description.nil?
|
|
78
|
+
|
|
79
|
+
token = token.to_sym
|
|
80
|
+
kind = kind.to_sym
|
|
81
|
+
kind_tree = @tokens[kind]
|
|
82
|
+
kind_tree = @tokens[kind] = Hash.new(DUMMY_TOKEN) if kind_tree.equal?(DUMMY_TOKENS)
|
|
83
|
+
token = kind_tree[token] = {}
|
|
84
|
+
token[:description] = description.to_s
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Sets the default token for the given strict kind.
|
|
89
|
+
#
|
|
90
|
+
# @param [Symbol] kind the kind to which the default
|
|
91
|
+
# token should be assigned
|
|
92
|
+
# @param [Symbol] target the token to set
|
|
93
|
+
# @return [void]
|
|
94
|
+
def set_default_token(kind, target)
|
|
95
|
+
@defaults[kind.to_sym] = target.to_sym
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Tests if the given token of the given
|
|
99
|
+
# strict kind is a true token.
|
|
100
|
+
#
|
|
101
|
+
# @param [Symbol] token the identifier of a token
|
|
102
|
+
# @param [Symbol] kind the identifier of a kind
|
|
103
|
+
# @return [Boolean] +true+ if the given +token+ is
|
|
104
|
+
# a token and not an alias, and is a kind of
|
|
105
|
+
# the given kind, +false+ otherwise
|
|
106
|
+
def has_true_token?(token, kind)
|
|
107
|
+
@tokens[kind].key?(token) && @tokens[kind][token][:target].nil?
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Tests if the given token (or alias) of the
|
|
111
|
+
# given strict kind is present.
|
|
112
|
+
#
|
|
113
|
+
# @param [Symbol] token the identifier of a token
|
|
114
|
+
# @param [Symbol] kind the identifier of a kind
|
|
115
|
+
# @return [Boolean] +true+ if the given +token+
|
|
116
|
+
# (which may be an alias) exists and if kind of
|
|
117
|
+
# the given kind
|
|
118
|
+
def has_token?(token, kind)
|
|
119
|
+
@tokens[kind].key?(token)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Tests if a strict kind exists.
|
|
123
|
+
#
|
|
124
|
+
# @param [Symbol] kind the identifier of a kind
|
|
125
|
+
# @return [Boolean] +true+ if the given +kind+ exists
|
|
126
|
+
def has_kind?(kind)
|
|
127
|
+
@tokens.key?(kind)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Tests if the given strict kind has a default
|
|
131
|
+
# token assigned.
|
|
132
|
+
#
|
|
133
|
+
# @param [Symbol] kind the identifier of a kind
|
|
134
|
+
# @return [Boolean] +true+ if there is a default
|
|
135
|
+
# token of the given kind
|
|
136
|
+
def has_default_token?(kind)
|
|
137
|
+
@defaults.key?(kind)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Tests if the given alias of the given strict
|
|
141
|
+
# kind is really an alias.
|
|
142
|
+
#
|
|
143
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
144
|
+
# @param [Symbol] kind the identifier of a kind
|
|
145
|
+
# @return [Boolean] +true+ if the given alias is really an alias
|
|
146
|
+
# being a kind of the given kind, +false+ otherwise
|
|
147
|
+
def has_alias?(alias_name, kind)
|
|
148
|
+
!@tokens[kind][alias_name][:target].nil?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Iterates through all the true tokens (not aliases) of the
|
|
152
|
+
# given strict kind.
|
|
153
|
+
#
|
|
154
|
+
# @param [Symbol] kind the identifier of a kind
|
|
155
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
156
|
+
# @yield [token, description] optional block in which each token will be yielded
|
|
157
|
+
# @yieldparam [Symbol] token a token
|
|
158
|
+
# @yieldparam [String] description a description string for a token
|
|
159
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
160
|
+
def each_true_token(kind, &)
|
|
161
|
+
LazyHashEnumerator.for(@tokens[kind])
|
|
162
|
+
.select { |_token, data| data[:target].nil? }
|
|
163
|
+
.map { |_token, data| data[:description] }
|
|
164
|
+
.each(&)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Iterates through all the aliases of the given strict kind.
|
|
168
|
+
#
|
|
169
|
+
# @param [Symbol] kind the identifier of a kind
|
|
170
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
|
|
171
|
+
# @yield [alias, target] optional block in which each alias will be yielded
|
|
172
|
+
# @yieldparam [Symbol] alias an alias
|
|
173
|
+
# @yieldparam [Symbol] target a name of the target token
|
|
174
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
175
|
+
def each_alias(kind, &)
|
|
176
|
+
LazyHashEnumerator.for(@tokens[kind])
|
|
177
|
+
.reject { |_token, data| data[:target].nil? }
|
|
178
|
+
.map { |_token, data| data[:target] }
|
|
179
|
+
.each(&)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Iterates through all the tokens of the given strict kind
|
|
183
|
+
# in a way that it is possible to
|
|
184
|
+
# distinguish true tokens from aliases.
|
|
185
|
+
#
|
|
186
|
+
# @note True tokens have descriptions (String) and aliases
|
|
187
|
+
# have targets (Symbol) assigned.
|
|
188
|
+
# @param [Symbol] kind the identifier of a kind
|
|
189
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
|
|
190
|
+
# @yield [token, value] optional block in which each token will be yielded
|
|
191
|
+
# @yieldparam [Symbol] token a token
|
|
192
|
+
# @yieldparam [Symbol, String] value a description string for a token or a target (if alias)
|
|
193
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
194
|
+
def each_raw_token(kind, &)
|
|
195
|
+
LazyHashEnumerator.for(@tokens[kind])
|
|
196
|
+
.map { |_token, data| data[:target] || data[:description] }
|
|
197
|
+
.each(&)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Iterates through all the tokens (including aliases) of the given
|
|
201
|
+
# strict kind.
|
|
202
|
+
#
|
|
203
|
+
# @note Use {#each_raw_token} if you want to distinguish
|
|
204
|
+
# true tokens from aliases.
|
|
205
|
+
# @param [Symbol] kind the identifier of a kind
|
|
206
|
+
# @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
|
|
207
|
+
# @yield [token, description] optional block in which each token will be yielded
|
|
208
|
+
# @yieldparam [Symbol] token a token
|
|
209
|
+
# @yieldparam [String] description a description string for a token
|
|
210
|
+
# @yieldreturn [LazyHashEnumerator] the lazy enumerator
|
|
211
|
+
def each_token(kind, &)
|
|
212
|
+
LazyHashEnumerator.for(@tokens[kind])
|
|
213
|
+
.map { |_token, data| data[:description] }
|
|
214
|
+
.each(&)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Gets a target token for the given alias of a strict kind.
|
|
218
|
+
#
|
|
219
|
+
# @param [Symbol] alias_name the identifier of an alias
|
|
220
|
+
# @param [Symbol] kind the identifier of a kind
|
|
221
|
+
# @return [Symbol,nil] the token that the given alias points to
|
|
222
|
+
# or +nil+ if it isn't really an alias
|
|
223
|
+
def get_target_for_alias(alias_name, kind)
|
|
224
|
+
@tokens[kind][alias_name][:target]
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Gets a strict kind of the given token or alias.
|
|
228
|
+
#
|
|
229
|
+
# @note This method may be concidered dummy since there is a
|
|
230
|
+
# need to give the inflection kind, but it's here in order
|
|
231
|
+
# to preserve compatibility with the same method from
|
|
232
|
+
# {I18n::Inflector::InflectionData} which guesses the kind.
|
|
233
|
+
# @param [Symbol] token identifier of a token
|
|
234
|
+
# @param [Symbol] kind the identifier of a kind (expectations filter)
|
|
235
|
+
# @return [Symbol,nil] the kind of the given +token+
|
|
236
|
+
# or +nil+ if the token is unknown or is not of the given kind
|
|
237
|
+
def get_kind(token, kind)
|
|
238
|
+
@tokens[kind].key?(token) ? kind : nil
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Gets a true token (of the given strict kind) for the given
|
|
242
|
+
# identifier.
|
|
243
|
+
#
|
|
244
|
+
# @note If the given +token+ is really an alias it will
|
|
245
|
+
# be resolved and the real token pointed by that alias
|
|
246
|
+
# will be returned.
|
|
247
|
+
# @param [Symbol] token the identifier of a token
|
|
248
|
+
# @param [Symbol] kind the identifier of a kind
|
|
249
|
+
# @return [Symbol,nil] the true token for the given +token+
|
|
250
|
+
# or +nil+ if the token is unknown or is not a kind of the
|
|
251
|
+
# given +kind+
|
|
252
|
+
def get_true_token(token, kind)
|
|
253
|
+
o = @tokens[kind]
|
|
254
|
+
return nil unless o.key?(token)
|
|
255
|
+
|
|
256
|
+
o = o[token]
|
|
257
|
+
o[:target].nil? ? token : o[:target]
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Iterates through all known strict kinds.
|
|
261
|
+
#
|
|
262
|
+
# @return [LazyArrayEnumerator] the lazy enumerator
|
|
263
|
+
# @yield [kind] optional block in which each kind will be yielded
|
|
264
|
+
# @yieldparam [Symbol] kind the inflection kind
|
|
265
|
+
# @yieldreturn [LazyArrayEnumerator] the lazy enumerator
|
|
266
|
+
def each_kind(&)
|
|
267
|
+
@lazy_kinds.map { |k, _v| k }.each(&)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Reads the default token of a strict kind.
|
|
271
|
+
#
|
|
272
|
+
# @note It will always return true token (not an alias).
|
|
273
|
+
# @param [Symbol] kind the identifier of a kind
|
|
274
|
+
# @return [Symbol,nil] the default token of the given +kind+
|
|
275
|
+
# or +nil+ if there is no default token set
|
|
276
|
+
def get_default_token(kind)
|
|
277
|
+
@defaults[kind]
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Gets a description of a token or alias belonging to a strict kind.
|
|
281
|
+
#
|
|
282
|
+
# @note If the token is really an alias it will resolve the alias first.
|
|
283
|
+
# @param [Symbol] token the identifier of a token
|
|
284
|
+
# @param [Symbol] kind the identifier of a kind
|
|
285
|
+
# @return [String,nil] the string containing description of the given
|
|
286
|
+
# token (which may be an alias) or +nil+ if the token is unknown
|
|
287
|
+
def get_description(token, kind)
|
|
288
|
+
@tokens[kind][token][:description]
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Test if the inflection data have no elements.
|
|
292
|
+
#
|
|
293
|
+
# @return [Boolean] +true+ if the inflection data
|
|
294
|
+
# have no elements
|
|
295
|
+
def empty?
|
|
296
|
+
@tokens.empty?
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
|
5
|
+
# Copyright:: (c) 2011,2012,2013 by Paweł Wilk
|
|
6
|
+
# License:: This program is licensed under the terms of {file:docs/LGPL GNU Lesser General Public License} or {file:docs/COPYING Ruby License}.
|
|
7
|
+
#
|
|
8
|
+
# This file contains a stub of I18n::Inflector module,
|
|
9
|
+
# which extends I18n by adding the ability
|
|
10
|
+
# to interpolate patterns containing inflection tokens
|
|
11
|
+
# defined in translation data and manipulate on that data.
|
|
12
|
+
|
|
13
|
+
module I18n
|
|
14
|
+
class << self
|
|
15
|
+
# This is proxy method that returns an inflector
|
|
16
|
+
# object used by the current I18n backend.
|
|
17
|
+
#
|
|
18
|
+
# @return [I18n::Inflector::API] inflector the inflector
|
|
19
|
+
# used by the current backend
|
|
20
|
+
def inflector
|
|
21
|
+
I18n.backend.inflector
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @version 2.6
|
|
26
|
+
# @api public
|
|
27
|
+
#
|
|
28
|
+
# This module contains inflection classes and modules for enabling
|
|
29
|
+
# the inflection support in I18n translations.
|
|
30
|
+
# It is used by the module called {I18n::Backend::Inflector}
|
|
31
|
+
# that overwrites the translate method from the Simple backend
|
|
32
|
+
# so it will interpolate additional inflection data present
|
|
33
|
+
# in translations.
|
|
34
|
+
#
|
|
35
|
+
# @see file:docs/USAGE
|
|
36
|
+
module Inflector
|
|
37
|
+
end
|
|
38
|
+
end
|