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.
@@ -0,0 +1,753 @@
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 {I18n::Inflector::API} class,
9
+ # which is public API for I18n Inflector.
10
+
11
+ module I18n
12
+ module Inflector
13
+ # Instance of this class, the inflector, is attached
14
+ # to I18n backend. This class contains common operations
15
+ # that can be performed on inflections. It can operate
16
+ # on both unnamed an named patterns (regular and strict kinds).
17
+ # This class is used by backend methods to interpolate
18
+ # strings and load inflections.
19
+ #
20
+ # It uses the databases containing instances of
21
+ # {I18n::Inflector::InflectionData} and {I18n::Inflector::InflectionData_Strict}
22
+ # that are stored in the Hashes and indexed by locale names.
23
+ #
24
+ # Note that strict kinds used to handle named patterns
25
+ # internally are stored in a different database than
26
+ # regular kinds. Most of the methods of this class are also
27
+ # aware of strict kinds and will call proper methods handling
28
+ # strict inflection data when the +@+ symbol is detected
29
+ # at the beginning of the given identifier of a kind.
30
+ #
31
+ # ==== Usage
32
+ # You can access the instance of this class attached to
33
+ # default I18n backend by calling:
34
+ # I18n.backend.inflector
35
+ # or in a short form:
36
+ # I18n.inflector
37
+ # In case of named patterns (strict kinds):
38
+ # I18n.inflector.strict
39
+ #
40
+ # @see I18n::Inflector::API_Strict The API_Strict class
41
+ # for accessing inflection data for named
42
+ # patterns (strict kinds).
43
+ # @see file:docs/EXAMPLES The examples of real-life usage.
44
+ # @api public
45
+ class API < API_Strict
46
+ include I18n::Inflector::Interpolate
47
+
48
+ # Options controlling the engine.
49
+ #
50
+ # @api public
51
+ # @return [I18n::Inflector::InflectionOptions] the set of options
52
+ # controlling inflection engine
53
+ # @see I18n::Inflector::InflectionOptions#raises
54
+ # @see I18n::Inflector::InflectionOptions#unknown_defaults
55
+ # @see I18n::Inflector::InflectionOptions#excluded_defaults
56
+ # @see I18n::Inflector::InflectionOptions#aliased_patterns
57
+ # @example Usage of +options+:
58
+ # # globally set raises flag
59
+ # I18n.inflector.options.raises = true
60
+ #
61
+ # # globally set raises flag (the same meaning as the example above)
62
+ # I18n.backend.inflector.options.raises = true
63
+ #
64
+ # # set raises flag just for this translation
65
+ # I18n.translate('welcome', inflector_raises: true)
66
+ attr_reader :options
67
+
68
+ # @private
69
+ def config
70
+ I18n::Inflector::Config
71
+ end
72
+
73
+ # @private
74
+ def strict
75
+ @strict ||= I18n::Inflector::API_Strict.new(@idb_strict, @options)
76
+ end
77
+
78
+ # Initilizes the inflector by creating internal databases
79
+ # used for storing inflection data and options.
80
+ #
81
+ # @api public
82
+ def initialize
83
+ super(nil, nil)
84
+ @idb_strict = {}
85
+ end
86
+
87
+ # Creates a database for the specified locale.
88
+ #
89
+ # @api public
90
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
91
+ # @param [Symbol] locale the locale for which the inflections database is to be created
92
+ # @return [I18n::Inflector::InflectionData] the new object for keeping inflection data
93
+ def new_database(locale)
94
+ locale = prep_locale(locale)
95
+ @idb[locale] = I18n::Inflector::InflectionData.new(locale)
96
+ end
97
+
98
+ # Creates internal databases (regular and strict) for the specified locale.
99
+ #
100
+ # @api public
101
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
102
+ # @param [Symbol] locale the locale for which the inflections databases are to be created
103
+ # @return [Array<I18n::Inflector::InflectionData,I18n::Inflector::InflectionData_Strict>] the
104
+ # array of objects for keeping inflection data
105
+ def new_databases(locale)
106
+ normal = new_databases(locale)
107
+ strict = strict.new_database(locale)
108
+ [normal, strict]
109
+ end
110
+
111
+ # Attaches instance of {I18n::Inflector::InflectionData} and
112
+ # optionally {I18n::Inflector::InflectionData_Strict}
113
+ # to the inflector.
114
+ #
115
+ # @api public
116
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
117
+ # @note It doesn't create a copy of inflection data, but registers the given object(s).
118
+ # @return [I18n::Inflector::InflectionData,Array,nil]
119
+ # @overload add_database(db)
120
+ # @param [I18n::Inflector::InflectionData] db inflection data to add
121
+ # @return [I18n::Inflector::InflectionData,nil] the given object or +nil+
122
+ # if something went wrong (e.g. +nil+ was given as an argument)
123
+ # @overload add_database(db, db_strict)
124
+ # @note An array is returned and databases are
125
+ # used only if both databases are successfully attached. References to
126
+ # both databases will be unset if there would be a problem with attaching
127
+ # any of them.
128
+ # @param [I18n::Inflector::InflectionData] db inflection data to add
129
+ # @param [I18n::Inflector::InflectionData_Strict] db_strict strict inflection data to add
130
+ # @return [Array<I18n::Inflector::InflectionData,I18n::Inflector::InflectionData_Strict>,nil] the
131
+ # array of the given objects or +nil+ if something went wrong (e.g. +nil+ was
132
+ # given as the first argument)
133
+ def add_database(db, db_strict = nil)
134
+ r = super(db)
135
+ return r if r.nil? || db_strict.nil?
136
+
137
+ r_strict = strict.add_database(db_strict)
138
+ if r_strict.nil?
139
+ delete_database(db.locale)
140
+ return nil
141
+ end
142
+ [r, r_strict]
143
+ end
144
+ alias_method :add_databases, :add_database
145
+
146
+ # Deletes the internal databases for the specified locale.
147
+ #
148
+ # @api public
149
+ # @note It detaches the databases from {I18n::Inflector::API} instance.
150
+ # Other objects referring to them may still use it.
151
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
152
+ # @param [Symbol] locale the locale for which the inflections database is to be deleted.
153
+ # @return [void]
154
+ def delete_databases(locale)
155
+ delete_database(locale)
156
+ strict.delete_database(locale)
157
+ end
158
+
159
+ # Checks if the given locale was configured to support inflection.
160
+ #
161
+ # @api public
162
+ # @note That method uses information from regular and strict kinds.
163
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
164
+ # @return [Boolean] +true+ if a locale supports inflection
165
+ # @overload inflected_locale?(locale)
166
+ # Checks if the given locale was configured to support inflection.
167
+ # @param [Symbol] locale the locale to test
168
+ # @return [Boolean] +true+ if the given locale supports inflection
169
+ # @overload inflected_locale?
170
+ # Checks if the current locale was configured to support inflection.
171
+ # @return [Boolean] +true+ if the current locale supports inflection
172
+ def inflected_locale?(locale = nil)
173
+ super || strict.inflected_locale?(locale)
174
+ end
175
+ alias_method :locale?, :inflected_locale?
176
+ alias_method :locale_supported?, :inflected_locale?
177
+
178
+ # Gets locales which have configured inflection support.
179
+ #
180
+ # @api public
181
+ # @note This method uses information from both regular and strict kinds.
182
+ # @return [Array<Symbol>] the array containing locales that support inflection
183
+ #
184
+ # @overload inflected_locales
185
+ # Gets locales which have configured inflection support.
186
+ # @return [Array<Symbol>] the array containing locales that support inflection
187
+ # @overload inflected_locales(kind)
188
+ # Gets locales which have configured inflection support for the given +kind+.
189
+ # @param [Symbol] kind the identifier of a kind
190
+ # @return [Array<Symbol>] the array containing locales that support inflection
191
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
192
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#inflected_locales})
193
+ def inflected_locales(kind = nil)
194
+ if kind.to_s[0..0] == Markers::STRICT_KIND
195
+ strict.inflected_locales(kind.to_s[1..])
196
+ else
197
+ kind = kind.to_s.empty? ? nil : kind.to_sym
198
+ r = (@inflected_locales_cache[kind] ||= super.uniq)
199
+ r.nil? ? r : r.dup
200
+ end
201
+ end
202
+
203
+ # Iterates through locales which have configured inflection support.
204
+ #
205
+ # @api public
206
+ # @note This method uses information from both regular and strict kinds.
207
+ # The locale identifiers may be duplicated!
208
+ # @return [LazyArrayEnumerator] the lazy enumerator
209
+ # @yield [locale] optional block in which each kind will be yielded
210
+ # @yieldparam [Symbol] locale the inflected locale identifier
211
+ # @yieldreturn [LazyArrayEnumerator] the lazy enumerator
212
+ # @overload each_inflected_locale
213
+ # Iterates through locales which have configured inflection support.
214
+ # @return [LazyArrayEnumerator] the lazy enumerator
215
+ # @overload each_inflected_locale(kind)
216
+ # Iterates through locales which have configured inflection support for the given +kind+.
217
+ # @param [Symbol] kind the identifier of a kind
218
+ # @return [LazyArrayEnumerator] the lazy enumerator
219
+ def each_inflected_locale(kind = nil, &)
220
+ super + strict.inflected_locales(kind)
221
+ end
222
+ alias_method :each_locale, :each_inflected_locale
223
+ alias_method :each_supported_locale, :each_inflected_locale
224
+
225
+ # Tests if a kind exists.
226
+ #
227
+ # @api public
228
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
229
+ # @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
230
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
231
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#has_kind?})
232
+ # @overload has_kind?(kind)
233
+ # Tests if a regular kind exists for the current locale.
234
+ # @param [Symbol] kind the identifier of a kind
235
+ # @return [Boolean] +true+ if the given +kind+ exists for the current
236
+ # locale, +false+ otherwise
237
+ # @overload has_kind?(kind, locale)
238
+ # Tests if a regular kind exists for the given +locale+.
239
+ # @param [Symbol,String] kind the identifier of a kind
240
+ # @param [Symbol] locale the locale identifier
241
+ # @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
242
+ def has_kind?(kind, locale = nil)
243
+ return strict.has_kind?(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND
244
+
245
+ super
246
+ end
247
+
248
+ # Reads default token for the given +kind+.
249
+ #
250
+ # @api public
251
+ # @return [Symbol,nil] the default token or +nil+
252
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
253
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
254
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#default_token})
255
+ # @overload default_token(kind)
256
+ # This method reads default token for the given +kind+ and the current locale.
257
+ # @param [Symbol,String] kind the kind of tokens
258
+ # @return [Symbol,nil] the default token or +nil+ if
259
+ # there is no default token
260
+ # @overload default_token(kind, locale)
261
+ # This method reads default token for the given +kind+ and the given +locale+.
262
+ # @param [Symbol,String] kind the kind of tokens
263
+ # @param [Symbol] locale the locale to use
264
+ # @return [Symbol,nil] the default token or +nil+ if
265
+ # there is no default token
266
+ def default_token(kind, locale = nil)
267
+ return nil if kind.nil? || kind.to_s.empty?
268
+ return strict.default_token(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND
269
+
270
+ super
271
+ end
272
+
273
+ # Checks if the given +token+ is an alias.
274
+ #
275
+ # @api public
276
+ # @note By default it uses regular kinds database, not strict kinds.
277
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
278
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
279
+ # @raise [ArgumentError] if the count of arguments is invalid
280
+ # @overload has_alias?(token)
281
+ # Uses current locale to check if the given +token+ is an alias.
282
+ # @param [Symbol,String] token name of the checked token
283
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
284
+ # @overload has_alias?(token, locale)
285
+ # Uses the given +locale+ to check if the given +token+ is an alias.
286
+ # @param [Symbol,String] token name of the checked token
287
+ # @param [Symbol] locale the locale to use
288
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
289
+ # @overload has_alias?(token, kind, locale)
290
+ # Uses the given +locale+ and +kind+ to check if the given +token+ is an alias.
291
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
292
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#has_alias?})
293
+ # @param [Symbol,String] token name of the checked token
294
+ # @param [Symbol,String] kind the kind used to narrow the check
295
+ # @param [Symbol] locale the locale to use
296
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
297
+ # @overload has_alias?(token, strict_kind)
298
+ # Uses the current locale and the given +strict_kind+ (which name must begin with
299
+ # the +@+ symbol) to check if the given +token+ is an alias.
300
+ # @note It calls {I18n::Inflector::API_Strict#has_alias?} on strict kinds data.
301
+ # @param [Symbol,String] token name of the checked token
302
+ # @param [Symbol,String] strict_kind the kind of the given alias
303
+ # @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
304
+ def has_alias?(*args)
305
+ token, kind, locale = tkl_args(args)
306
+ return false if token.nil? || token.to_s.empty?
307
+
308
+ unless kind.nil?
309
+ kind = kind.to_s
310
+ reutrn false if kind.empty?
311
+ return strict.has_alias?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
312
+
313
+ kind = kind.to_sym
314
+ end
315
+ data_safe(locale).has_alias?(token.to_sym, kind)
316
+ end
317
+ alias_method :token_has_alias?, :has_alias?
318
+
319
+ # Checks if the given +token+ is a true token (not alias).
320
+ #
321
+ # @api public
322
+ # @note By default it uses regular kinds database, not strict kinds.
323
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
324
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
325
+ # @raise [ArgumentError] if the count of arguments is invalid
326
+ # @overload has_true_token?(token)
327
+ # Uses current locale to check if the given +token+ is a true token.
328
+ # @param [Symbol,String] token name of the checked token
329
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
330
+ # @overload has_true_token?(token, locale)
331
+ # Uses the given +locale+ to check if the given +token+ is a true token.
332
+ # @param [Symbol,String] token name of the checked token
333
+ # @param [Symbol] locale the locale to use
334
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
335
+ # @overload has_true_token?(token, kind, locale)
336
+ # Uses the given +locale+ and +kind+ to check if the given +token+ is a true token.
337
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
338
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#has_true_token?})
339
+ # @param [Symbol,String] token name of the checked token
340
+ # @param [Symbol,String] kind the kind used to narrow the check
341
+ # @param [Symbol] locale the locale to use
342
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
343
+ # @overload has_true_token?(token, strict_kind)
344
+ # Uses the current locale and the given +strict_kind+ (which name must begin with
345
+ # the +@+ symbol) to check if the given +token+ is a true token.
346
+ # @note It calls {I18n::Inflector::API_Strict#has_true_token?} on strict kinds data.
347
+ # @param [Symbol,String] token name of the checked token
348
+ # @param [Symbol,String] strict_kind the kind of the given token
349
+ # @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
350
+ def has_true_token?(*args)
351
+ token, kind, locale = tkl_args(args)
352
+ return false if token.nil? || token.to_s.empty?
353
+
354
+ unless kind.nil?
355
+ kind = kind.to_s
356
+ return false if kind.empty?
357
+ return strict.has_true_token?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
358
+
359
+ kind = kind.to_sym
360
+ end
361
+ data_safe(locale).has_true_token?(token.to_sym, kind)
362
+ end
363
+ alias_method :token_has_true?, :has_true_token?
364
+
365
+ # Checks if the given +token+ exists. It may be an alias or a true token.
366
+ #
367
+ # @api public
368
+ # @note By default it uses regular kinds database, not strict kinds.
369
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
370
+ # @raise [I18n::InvalidLocale] if the given +locale+ is invalid
371
+ # @raise [ArgumentError] if the count of arguments is invalid
372
+ # @overload has_token?(token)
373
+ # Uses current locale to check if the given +token+ is a token.
374
+ # @param [Symbol,String] token name of the checked token
375
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
376
+ # @overload has_token?(token, locale)
377
+ # Uses the given +locale+ to check if the given +token+ exists.
378
+ # @param [Symbol,String] token name of the checked token
379
+ # @param [Symbol] locale the locale to use
380
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
381
+ # @overload has_token?(token, kind, locale)
382
+ # Uses the given +locale+ and +kind+ to check if the given +token+ exists.
383
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
384
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#has_token?})
385
+ # @param [Symbol,String] token name of the checked token
386
+ # @param [Symbol,String] kind the kind used to narrow the check
387
+ # @param [Symbol] locale the locale to use
388
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
389
+ # @overload has_token?(token, strict_kind)
390
+ # Uses the current locale and the given +strict_kind+ (which name must begin with
391
+ # the +@+ symbol) to check if the given +token+ exists.
392
+ # @note It calls {I18n::Inflector::API_Strict#has_token?} on strict kinds data.
393
+ # @param [Symbol,String] token name of the checked token
394
+ # @param [Symbol,String] strict_kind the kind of the given token
395
+ # @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
396
+ def has_token?(*args)
397
+ token, kind, locale = tkl_args(args)
398
+ return false if token.nil? || token.to_s.empty?
399
+
400
+ unless kind.nil?
401
+ kind = kind.to_s
402
+ return false if kind.empty?
403
+ return strict.has_token?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
404
+
405
+ kind = kind.to_sym
406
+ end
407
+ data_safe(locale).has_token?(token.to_sym, kind)
408
+ end
409
+ alias_method :token_exists?, :has_token?
410
+
411
+ # Gets true token for the given +token+. If the token
412
+ # is an alias it will be resolved
413
+ # and a true token (target) will be returned.
414
+ # @note By default it uses regular kinds database, not strict kinds.
415
+ # @api public
416
+ # @return [Symbol,nil] the true token or +nil+
417
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
418
+ # @overload true_token(token)
419
+ # Uses current locale to get a real token for the given +token+.
420
+ # @param [Symbol,String] token name of the checked token
421
+ # @return [Symbol,nil] the true token or +nil+
422
+ # @overload true_token(token, locale)
423
+ # Uses the given +locale+ to get a real token for the given +token+.
424
+ # If the token is an alias it will be resolved
425
+ # and a true token (target) will be returned.
426
+ # @param [Symbol,String] token name of the checked token
427
+ # @param [Symbol] locale the locale to use
428
+ # @return [Symbol,nil] the true token or +nil+
429
+ # @overload true_token(token, kind, locale)
430
+ # Uses the given +locale+ and +kind+ to get a real token for the given +token+.
431
+ # If the token is an alias it will be resolved
432
+ # and a true token (target) will be returned.
433
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
434
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#true_token})
435
+ # @param [Symbol,String] token name of the checked token
436
+ # @param [Symbol,String] kind the kind of the given token
437
+ # @param [Symbol] locale the locale to use
438
+ # @return [Symbol,nil] the true token or +nil+
439
+ # @overload true_token(token, strict_kind)
440
+ # Uses the current locale and the given +strict_kind+ (which name must begin with
441
+ # the +@+ symbol) to get a real token for the given +token+.
442
+ # @note It calls {I18n::Inflector::API_Strict#true_token} on strict kinds data.
443
+ # @param [Symbol,String] token name of the checked token
444
+ # @param [Symbol,String] strict_kind the kind of the given token
445
+ # @return [Symbol,nil] the true token
446
+ def true_token(*args)
447
+ token, kind, locale = tkl_args(args)
448
+ return nil if token.nil? || token.to_s.empty?
449
+
450
+ unless kind.nil?
451
+ kind = kind.to_s
452
+ return nil if kind.empty?
453
+ return strict.true_token(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
454
+
455
+ kind = kind.to_sym
456
+ end
457
+ data_safe(locale).get_true_token(token.to_sym, kind)
458
+ end
459
+ alias_method :resolve_alias, :true_token
460
+
461
+ # Gets a kind for the given +token+ (which may be an alias).
462
+ #
463
+ # @api public
464
+ # @note By default it uses regular kinds database, not strict kinds.
465
+ # @return [Symbol,nil] the kind of the given +token+ or +nil+
466
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
467
+ # @overload kind(token)
468
+ # Uses the current locale to get a kind of the given +token+ (which may be an alias).
469
+ # @param [Symbol,String] token name of the token or alias
470
+ # @return [Symbol,nil] the kind of the given +token+
471
+ # @overload kind(token, locale)
472
+ # Uses the given +locale+ to get a kind of the given +token+ (which may be an alias).
473
+ # @param [Symbol,String] token name of the token or alias
474
+ # @param [Symbol] locale the locale to use
475
+ # @return [Symbol,nil] the kind of the given +token+
476
+ # @overload kind(token, kind, locale)
477
+ # Uses the given +locale+ to get a kind of the given +token+ (which may be an alias).
478
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
479
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#kind})
480
+ # @param [Symbol,String] token name of the token or alias
481
+ # @param [Symbol,String] kind the kind name to narrow the search
482
+ # @param [Symbol] locale the locale to use
483
+ # @return [Symbol,nil] the kind of the given +token+
484
+ # @overload kind(token, strict_kind)
485
+ # Uses the current locale and the given +strict_kind+ (which name must begin with
486
+ # the +@+ symbol) to get a kind of the given +token+ (which may be an alias).
487
+ # @note It calls {I18n::Inflector::API_Strict#kind} on strict kinds data.
488
+ # @param [Symbol,String] token name of the token or alias
489
+ # @param [Symbol,String] kind the kind of the given token
490
+ # @return [Symbol,nil] the kind of the given +token+
491
+ def kind(*args)
492
+ token, kind, locale = tkl_args(args)
493
+ return nil if token.nil? || token.to_s.empty?
494
+
495
+ unless kind.nil?
496
+ kind = kind.to_s
497
+ return nil if kind.empty?
498
+ return strict.kind(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
499
+
500
+ kind = kind.to_sym
501
+ end
502
+ data_safe(locale).get_kind(token.to_sym, kind)
503
+ end
504
+
505
+ # Iterates through available inflection tokens and their descriptions.
506
+ #
507
+ # @api public
508
+ # @note By default it uses regular kinds database, not strict kinds.
509
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
510
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
511
+ # @yield [token, description] optional block in which each token will be yielded
512
+ # @yieldparam [Symbol] token a token
513
+ # @yieldparam [String] description a description string for a token
514
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
515
+ # @note You cannot deduce where aliases are pointing to, since the information
516
+ # about a target is replaced by the description. To get targets use the
517
+ # {#raw_tokens} method. To simply list aliases and their targets use
518
+ # the {#aliases} method.
519
+ # @overload each_token
520
+ # Iterates through available inflection tokens and their descriptions.
521
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
522
+ # @overload each_token(kind)
523
+ # Iterates through available inflection tokens and their descriptions for some +kind+.
524
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
525
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token})
526
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
527
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
528
+ # @overload each_token(kind, locale)
529
+ # Iterates through available inflection tokens and their descriptions for some +kind+ and +locale+.
530
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
531
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token})
532
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
533
+ # @param [Symbol] locale the locale to use
534
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
535
+ def each_token(kind = nil, locale = nil)
536
+ return strict.each_token(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND
537
+
538
+ super
539
+ end
540
+
541
+ # Iterates through available inflection tokens and their values.
542
+ #
543
+ # @api public
544
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
545
+ # @yield [token, value] optional block in which each token will be yielded
546
+ # @yieldparam [Symbol] token a token
547
+ # @yieldparam [Symbol, String] value a description string for a token or a target (if alias)
548
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
549
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
550
+ # @note You may deduce whether the returned values are aliases or true tokens
551
+ # by testing if a value is a type of Symbol or String.
552
+ # @overload each_token_raw
553
+ # Iterates through available inflection tokens and their values for regular kinds.
554
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
555
+ # @overload each_token_raw(kind)
556
+ # Iterates through available inflection tokens and their values for the given +kind+.
557
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
558
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token_raw})
559
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
560
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
561
+ # @overload each_token_raw(kind, locale)
562
+ # Iterates through available inflection tokens and their values for the given +kind+ and +locale+.
563
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
564
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token_raw})
565
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
566
+ # @param [Symbol] locale the locale to use
567
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description|target</tt>)
568
+ def each_token_raw(kind = nil, locale = nil)
569
+ return strict.each_token_raw(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND
570
+
571
+ super
572
+ end
573
+ alias_method :each_raw_token, :each_token_raw
574
+
575
+ # Iterates through true inflection tokens and their values.
576
+ #
577
+ # @api public
578
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
579
+ # @yield [token, description] optional block in which each token will be yielded
580
+ # @yieldparam [Symbol] token a token
581
+ # @yieldparam [String] description a description string for a token
582
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
583
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
584
+ # @note It returns only true tokens, not aliases.
585
+ # @overload each_token_true
586
+ # Iterates through true inflection tokens and their values for regular kinds.
587
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
588
+ # @overload each_token_true(kind)
589
+ # Iterates through true inflection tokens and their values for the given +kind+.
590
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
591
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token_true})
592
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
593
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
594
+ # @overload each_token_true(kind, locale)
595
+ # Iterates through true inflection tokens and their values for the given +kind+ and +value+.
596
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
597
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_token_true})
598
+ # @param [Symbol,String] kind the kind of inflection tokens to be returned
599
+ # @param [Symbol] locale the locale to use
600
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => description</tt>)
601
+ def each_token_true(kind = nil, locale = nil, &)
602
+ return strict.each_token_true(kind.to_s[1..], locale, &) if kind.to_s[0..0] == Markers::STRICT_KIND
603
+
604
+ super
605
+ end
606
+ alias_method :each_true_token, :each_token_true
607
+
608
+ # Iterates through inflection aliases and their pointers.
609
+ #
610
+ # @api public
611
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
612
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
613
+ # @yield [alias, target] optional block in which each alias will be yielded
614
+ # @yieldparam [Symbol] alias an alias
615
+ # @yieldparam [Symbol] target a name of the target token
616
+ # @yieldreturn [LazyHashEnumerator] the lazy enumerator
617
+ # @overload each_alias(kind)
618
+ # Iterates through inflection aliases (and their pointers) of the given +kind+ and the current locale.
619
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
620
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_alias})
621
+ # @param [Symbol,String] kind the kind of aliases to get
622
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
623
+ # @overload each_alias(kind, locale)
624
+ # Iterates through inflection aliases (and their pointers) of the given +kind+ and +locale+.
625
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
626
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#each_alias})
627
+ # @param [Symbol,String] kind the kind of aliases to get
628
+ # @param [Symbol] locale the locale to use
629
+ # @return [LazyHashEnumerator] the lazy enumerator (<tt>token => target</tt>)
630
+ def each_alias(kind = nil, locale = nil, &)
631
+ return strict.each_alias(kind.to_s[1..], locale, &) if kind.to_s[0..0] == Markers::STRICT_KIND
632
+
633
+ super
634
+ end
635
+
636
+ # Gets the description of the given inflection token.
637
+ #
638
+ # @api public
639
+ # @note If the given +token+ is really an alias it
640
+ # returns the description of the true token that
641
+ # it points to. By default it uses regular kinds database,
642
+ # not strict kinds.
643
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
644
+ # @return [String,nil] the descriptive string or +nil+
645
+ # @overload token_description(token)
646
+ # Uses current locale to get description of the given inflection +token+.
647
+ # @param [Symbol] token the identifier of a token
648
+ # @return [String,nil] the descriptive string or +nil+ if something
649
+ # went wrong (e.g. token was not found)
650
+ # @overload token_description(token, locale)
651
+ # Uses the given +locale+ to get description of the given inflection +token+.
652
+ # @param [Symbol,String] token the identifier of a token
653
+ # @param [Symbol] locale the locale to use
654
+ # @return [String,nil] the descriptive string or +nil+ if something
655
+ # went wrong (e.g. token was not found)
656
+ # @overload token_description(token, kind, locale)
657
+ # Uses the given +locale+ and +kind+ to get description of the given inflection +token+.
658
+ # @note If +kind+ begins with the +@+ symbol then the variant of this method
659
+ # operating on strict kinds will be called ({I18n::Inflector::API_Strict#token_description})
660
+ # @param [Symbol,String] token the identifier of a token
661
+ # @param [Symbol,String] kind the kind to narrow the results
662
+ # @param [Symbol] locale the locale to use
663
+ # @return [String,nil] the descriptive string or +nil+ if something
664
+ # went wrong (e.g. token was not found or +kind+ mismatched)
665
+ # @overload token_description(token, strict_kind)
666
+ # Uses the default locale and the given +kind+ (which name must begin with
667
+ # the +@+ symbol) to get description of the given inflection +token+.
668
+ # @note It calls {I18n::Inflector::API_Strict#token_description} on strict kinds data.
669
+ # @param [Symbol,String] token the identifier of a token
670
+ # @param [Symbol,String] strict_kind the kind of a token
671
+ # @param [Symbol] locale the locale to use
672
+ # @return [String,nil] the descriptive string or +nil+ if something
673
+ # went wrong (e.g. token was not found or +kind+ mismatched)
674
+ def token_description(*args)
675
+ token, kind, locale = tkl_args(args)
676
+ return nil if token.nil? || token.to_s.empty?
677
+
678
+ unless kind.nil?
679
+ kind = kind.to_s
680
+ return nil if kind.empty?
681
+ return strict.token_description(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND
682
+
683
+ kind = kind.to_sym
684
+ end
685
+ data_safe(locale).get_description(token.to_sym, kind)
686
+ end
687
+
688
+ protected
689
+
690
+ # @private
691
+ def data(locale = nil)
692
+ @idb[prep_locale(locale)]
693
+ end
694
+
695
+ # @private
696
+ def data_safe(locale = nil)
697
+ @idb[prep_locale(locale)] || I18n::Inflector::InflectionData.new(locale)
698
+ end
699
+
700
+ # This method is the internal helper that prepares arguments
701
+ # containing +token+, +kind+ and +locale+.
702
+ #
703
+ # @note This method leaves +kind+ as is when it's +nil+ or empty. It sets
704
+ # +token+ to +nil+ when it's empty.
705
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
706
+ # @raise [ArgumentError] if the count of arguments is invalid
707
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
708
+ # cleaned and validated +token+, +kind+ and +locale+
709
+ # @overload tkl_args(token, kind, locale)
710
+ # Prepares arguments containing +token+, +kind+ and +locale+.
711
+ # @param [String,Hash] token the token
712
+ # @param [String,Hash] kind the inflection kind
713
+ # @param [String,Hash] locale the locale identifier
714
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
715
+ # cleaned and validated +token+, +kind+ and +locale+
716
+ # @overload tkl_args(token, locale)
717
+ # Prepares arguments containing +token+ and +locale+.
718
+ # @param [String,Hash] token the token
719
+ # @param [String,Hash] locale the locale identifier
720
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
721
+ # cleaned and validated +token+, +kind+ and +locale+
722
+ # @overload tkl_args(token)
723
+ # Prepares arguments containing +token+.
724
+ # @param [String,Hash] token the token
725
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
726
+ # cleaned and validated +token+ and the current locale
727
+ # @overload tkl_args(token, strict_kind)
728
+ # Prepares arguments containing +token+ and +strict_kind+.
729
+ # @param [String,Hash] token the token
730
+ # @param [String,Hash] strict_kind the strict kind identifier beginning with +@+ symbol
731
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
732
+ # cleaned and validated +token+, +strict_kind+ and the current locale
733
+ def tkl_args(args)
734
+ token, kind, locale = case args.count
735
+ when 1 then [args[0], nil, nil]
736
+ when 2 then if args[1].to_s[0..0] == Markers::STRICT_KIND
737
+ [args[0], args[1],
738
+ nil]
739
+ else
740
+ [args[0], nil, args[1]]
741
+ end
742
+ when 3 then args
743
+ else raise I18n::ArgumentError, "wrong number of arguments: #{args.count} for (1..3)"
744
+ end
745
+ [token, kind, locale]
746
+ end
747
+ end
748
+
749
+ # @abstract This is for backward compatibility with the old naming scheme.
750
+ class Core < API
751
+ end
752
+ end
753
+ end