i18n-inflector 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +1 -0
- data/ChangeLog +412 -0
- data/Gemfile +1 -0
- data/Manifest.txt +6 -1
- data/README.rdoc +16 -11
- data/Rakefile +15 -2
- data/ci/i18n-inflector.gemspec +1 -1
- data/ci/i18nv4-Gemfile +15 -0
- data/docs/EXAMPLES +222 -0
- data/docs/HISTORY +31 -0
- data/docs/LEGAL +0 -1
- data/docs/RELATIONS +16 -13
- data/docs/TODO +25 -3
- data/lib/i18n-inflector/api.rb +964 -0
- data/lib/i18n-inflector/api_strict.rb +519 -0
- data/lib/i18n-inflector/backend.rb +77 -40
- data/lib/i18n-inflector/errors.rb +56 -14
- data/lib/i18n-inflector/inflection_data.rb +133 -105
- data/lib/i18n-inflector/inflection_data_strict.rb +290 -0
- data/lib/i18n-inflector/inflector.rb +21 -660
- data/lib/i18n-inflector/lazy_enum.rb +120 -0
- data/lib/i18n-inflector/long_comments.rb +203 -14
- data/lib/i18n-inflector/version.rb +1 -1
- data/lib/i18n-inflector.rb +5 -3
- data/test/inflector_test.rb +334 -34
- data/test/test_helper.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +10 -6
- metadata.gz.sig +0 -0
- data/lib/i18n-inflector/util.rb +0 -67
@@ -0,0 +1,519 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Author:: Paweł Wilk (mailto:pw@gnu.org)
|
4
|
+
# Copyright:: (c) 2011 by Paweł Wilk
|
5
|
+
# License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
|
6
|
+
#
|
7
|
+
# This file contains I18n::Inflector::API_Strict class,
|
8
|
+
# which handles public API for managing inflection data
|
9
|
+
# for named patterns (strict kinds).
|
10
|
+
|
11
|
+
module I18n
|
12
|
+
|
13
|
+
module Inflector
|
14
|
+
|
15
|
+
# This class contains common operations
|
16
|
+
# that can be performed on inflection data describing
|
17
|
+
# strict kinds and tokens assigned to them (used in named
|
18
|
+
# patterns). It is used by the regular {I18n::Inflector::API API}
|
19
|
+
# and present there as {I18n::Inflector::API#strict strict}
|
20
|
+
# instance attribute.
|
21
|
+
#
|
22
|
+
# It operates on the database containing instances
|
23
|
+
# of {I18n::Inflector::InflectionData_Strict} indexed by locale
|
24
|
+
# names and has methods to access the inflection data in an easy way.
|
25
|
+
# It can operate on a database and options passed to initializer;
|
26
|
+
# if they aren't passet it will create them.
|
27
|
+
#
|
28
|
+
# ==== Usage
|
29
|
+
# You can access the instance of this class attached to
|
30
|
+
# default I18n backend by calling:
|
31
|
+
# I18n.backend.inflector.strict
|
32
|
+
# or in a short form:
|
33
|
+
# I18n.inflector.strict
|
34
|
+
#
|
35
|
+
# In most cases using the regular {I18n::Inflector::API} instance
|
36
|
+
# may be sufficient to operate on inflection data,
|
37
|
+
# because the regular API (instantiated as <tt>I18n.inflector</tt>)
|
38
|
+
# is aware of strict kinds and can pass calls from +API_Strict+
|
39
|
+
# object if the +kind+ argument given in a method call
|
40
|
+
# contains the +@+ symbol.
|
41
|
+
#
|
42
|
+
# For an instance connected to default I18n backend
|
43
|
+
# the object containing inflection options is shared
|
44
|
+
# with the regular API.
|
45
|
+
#
|
46
|
+
# @api public
|
47
|
+
class API_Strict
|
48
|
+
|
49
|
+
# Initilizes inflector by connecting to internal databases
|
50
|
+
# used for storing inflection data and options.
|
51
|
+
#
|
52
|
+
# @api public
|
53
|
+
# @note If any given option is +nil+ then a proper object will be created.
|
54
|
+
# If it's given, then it will be referenced, not copied.
|
55
|
+
# @param [Hash,nil] idb the strict inflections databases indexed by locale
|
56
|
+
# @param [I18n::Inflector::InflectionOptions,nil] options the inflection options
|
57
|
+
def initialize(idb=nil, options=nil)
|
58
|
+
@idb = idb.nil? ? {} : idb
|
59
|
+
@options = options.nil? ? I18n::Inflector::InflectionOptions.new : options
|
60
|
+
end
|
61
|
+
|
62
|
+
# Creates an empty strict inflections database for the specified locale.
|
63
|
+
#
|
64
|
+
# @api public
|
65
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
66
|
+
# @param [Symbol] locale the locale for which the inflections database
|
67
|
+
# should be created
|
68
|
+
# @return [I18n::Inflector::InflectionData_Strict] the new object for keeping
|
69
|
+
# inflection data
|
70
|
+
def new_database(locale)
|
71
|
+
locale = prep_locale(locale)
|
72
|
+
@idb[locale] = I18n::Inflector::InflectionData_Strict.new(locale)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Attaches {I18n::Inflector::InflectionData_Strict} instance to the
|
76
|
+
# current object.
|
77
|
+
#
|
78
|
+
# @api public
|
79
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
80
|
+
# @note It doesn't create copy of inflection database, it registers the given object.
|
81
|
+
# @param [I18n::Inflector::InflectionData_Strict] idb inflection data to add
|
82
|
+
# @return [I18n::Inflector::InflectionData_Strict] the given +idb+ or +nil+ if something
|
83
|
+
# went wrong (e.g. +nil+ was given as an argument)
|
84
|
+
def add_database(db)
|
85
|
+
return nil if db.nil?
|
86
|
+
locale = prep_locale(db.locale)
|
87
|
+
delete_database(locale)
|
88
|
+
@idb[locale] = db
|
89
|
+
end
|
90
|
+
|
91
|
+
# Deletes a strict inflections database for the specified locale.
|
92
|
+
#
|
93
|
+
# @api public
|
94
|
+
# @note It detaches the database from {I18n::Inflector::API_Strict} instance.
|
95
|
+
# Other objects referring to it directly may still use it.
|
96
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
97
|
+
# @param [Symbol] locale the locale for which the inflections database is to be deleted.
|
98
|
+
# @return [void]
|
99
|
+
def delete_database(locale)
|
100
|
+
locale = prep_locale(locale)
|
101
|
+
return nil if @idb[locale].nil?
|
102
|
+
@idb[locale] = nil
|
103
|
+
end
|
104
|
+
|
105
|
+
# Checks if the given locale was configured to support strict inflection.
|
106
|
+
#
|
107
|
+
# @api public
|
108
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
109
|
+
# @return [Boolean] +true+ if a locale supports inflection
|
110
|
+
# @overload inflected_locale?
|
111
|
+
# Checks if the current locale was configured to support inflection.
|
112
|
+
# @return [Boolean] +true+ if the current locale supports inflection
|
113
|
+
# @overload inflected_locale?(locale)
|
114
|
+
# Checks if the given locale was configured to support inflection.
|
115
|
+
# @param [Symbol] locale the locale to test
|
116
|
+
# @return [Boolean] +true+ if the given locale supports inflection
|
117
|
+
def inflected_locale?(locale=nil)
|
118
|
+
not @idb[prep_locale(locale)].nil? rescue false
|
119
|
+
end
|
120
|
+
alias_method :locale?, :inflected_locale?
|
121
|
+
alias_method :locale_supported?, :inflected_locale?
|
122
|
+
|
123
|
+
# Gets locales which have configured strict inflection support.
|
124
|
+
#
|
125
|
+
# @api public
|
126
|
+
# @return [Array<Symbol>] the array containing locales that support inflection
|
127
|
+
# @overload inflected_locales
|
128
|
+
# Gets locales which have configured inflection support.
|
129
|
+
# @return [Array<Symbol>] the array containing locales that support inflection
|
130
|
+
# @overload inflected_locales(kind)
|
131
|
+
# Gets locales which have configured inflection support for the given +kind+.
|
132
|
+
# @param [Symbol] kind the identifier of a kind
|
133
|
+
# @return [Array<Symbol>] the array containing locales that support inflection
|
134
|
+
def inflected_locales(kind=nil)
|
135
|
+
return [] if (!kind.nil? && kind.to_s.empty?)
|
136
|
+
inflected_locales = LazyHashEnumerator.new(@idb || {})
|
137
|
+
inflected_locales = inflected_locales.
|
138
|
+
reject{ |lang,data| data.nil? || data.empty? }
|
139
|
+
return inflected_locales.keys if kind.nil?
|
140
|
+
kind = kind.to_sym
|
141
|
+
inflected_locales.
|
142
|
+
reject{|land,data| !data.has_kind?(kind)}.
|
143
|
+
keys
|
144
|
+
end
|
145
|
+
alias_method :locales, :inflected_locales
|
146
|
+
alias_method :supported_locales, :inflected_locales
|
147
|
+
|
148
|
+
# Gets known strict inflection kinds.
|
149
|
+
#
|
150
|
+
# @api public
|
151
|
+
# @return [Array<Symbol>] the array containing known inflection kinds
|
152
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
153
|
+
# @overload kinds
|
154
|
+
# Gets known inflection kinds for the current locale.
|
155
|
+
# @return [Array<Symbol>] the array containing known inflection kinds
|
156
|
+
# @overload kinds(locale)
|
157
|
+
# Gets known inflection kinds for the given +locale+.
|
158
|
+
# @param [Symbol] locale the locale for which kinds should be listed
|
159
|
+
# @return [Array<Symbol>] the array containing known inflection kinds
|
160
|
+
def kinds(locale=nil)
|
161
|
+
data_safe(locale).get_kinds
|
162
|
+
end
|
163
|
+
alias_method :inflection_kinds, :kinds
|
164
|
+
|
165
|
+
# Tests if a strict kind exists.
|
166
|
+
#
|
167
|
+
# @api public
|
168
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
169
|
+
# @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
|
170
|
+
# @overload has_kind?(kind)
|
171
|
+
# Tests if a strict kind exists for the current locale.
|
172
|
+
# @param [Symbol] kind the identifier of a kind
|
173
|
+
# @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
|
174
|
+
# @overload has_kind?(kind, locale)
|
175
|
+
# Tests if a strict kind exists for the given +locale+.
|
176
|
+
# @param [Symbol,String] kind the identifier of a kind
|
177
|
+
# @param [Symbol] locale the locale identifier
|
178
|
+
# @return [Boolean] +true+ if the given +kind+ exists, +false+ otherwise
|
179
|
+
def has_kind?(kind, locale=nil)
|
180
|
+
return false if (kind.nil? || kind.to_s.empty?)
|
181
|
+
data_safe(locale).has_kind?(kind.to_sym)
|
182
|
+
end
|
183
|
+
|
184
|
+
# Reads default token of the given strict +kind+.
|
185
|
+
#
|
186
|
+
# @api public
|
187
|
+
# @return [Symbol,nil] the default token or +nil+
|
188
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
189
|
+
# @overload default_token(kind)
|
190
|
+
# This method reads default token of the given +kind+ and the current locale.
|
191
|
+
# @param [Symbol,String] kind the kind of tokens
|
192
|
+
# @return [Symbol,nil] the default token or +nil+ if
|
193
|
+
# there is no default token
|
194
|
+
# @overload default_token(kind, locale)
|
195
|
+
# This method reads default token of the given +kind+ and the given +locale+.
|
196
|
+
# @param [Symbol,String] kind the kind of tokens
|
197
|
+
# @param [Symbol] locale the locale to use
|
198
|
+
# @return [Symbol,nil] the default token or +nil+ if
|
199
|
+
# there is no default token
|
200
|
+
def default_token(kind, locale=nil)
|
201
|
+
return nil if (kind.nil? || kind.to_s.empty?)
|
202
|
+
data_safe(locale).get_default_token(kind.to_sym)
|
203
|
+
end
|
204
|
+
|
205
|
+
# Checks if the given +token+ belonging to a strict kind is an alias.
|
206
|
+
#
|
207
|
+
# @api public
|
208
|
+
# @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
|
209
|
+
# @raise [I18n::InvalidLocale] if the given +locale+ is invalid
|
210
|
+
# @raise [ArgumentError] if the count of arguments is invalid
|
211
|
+
# @overload has_alias?(token, kind)
|
212
|
+
# Uses the current locale and the given +kind+ to check if the given +token+ is an alias.
|
213
|
+
# @param [Symbol,String] token name of the checked token
|
214
|
+
# @param [Symbol,String] kind the kind of the given token
|
215
|
+
# @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
|
216
|
+
# @overload has_alias?(token, kind, locale)
|
217
|
+
# Uses the given +locale+ and +kind+ to check if the given +token+ is an alias.
|
218
|
+
# @param [Symbol,String] token name of the checked token
|
219
|
+
# @param [Symbol,String] kind the kind of the given token
|
220
|
+
# @param [Symbol] locale the locale to use
|
221
|
+
# @return [Boolean] +true+ if the given +token+ is an alias, +false+ otherwise
|
222
|
+
def has_alias?(*args)
|
223
|
+
token, kind, locale = tkl_args(args)
|
224
|
+
return false if (token.nil? || kind.nil?)
|
225
|
+
data_safe(locale).has_alias?(token, kind)
|
226
|
+
end
|
227
|
+
alias_method :token_is_alias?, :has_alias?
|
228
|
+
|
229
|
+
# Checks if the given +token+ belonging to a strict kind is a true token (not alias).
|
230
|
+
#
|
231
|
+
# @api public
|
232
|
+
# @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
|
233
|
+
# @raise [I18n::InvalidLocale] if the given +locale+ is invalid
|
234
|
+
# @raise [ArgumentError] if the count of arguments is invalid
|
235
|
+
# @overload has_true_token?(token, kind)
|
236
|
+
# Uses the current locale and the given +kind+ to check if the given +token+ is a true token.
|
237
|
+
# @param [Symbol,String] token name of the checked token
|
238
|
+
# @param [Symbol,String] kind the kind of the given token
|
239
|
+
# @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
|
240
|
+
# @overload has_true_token?(token, kind, locale)
|
241
|
+
# Uses the given +locale+ and +kind+ to check if the given +token+ is a true token.
|
242
|
+
# @param [Symbol,String] token name of the checked token
|
243
|
+
# @param [Symbol,String] kind the kind of the given token
|
244
|
+
# @param [Symbol] locale the locale to use
|
245
|
+
# @return [Boolean] +true+ if the given +token+ is a true token, +false+ otherwise
|
246
|
+
def has_true_token?(*args)
|
247
|
+
token, kind, locale = tkl_args(args)
|
248
|
+
return false if (token.nil? || kind.nil?)
|
249
|
+
data_safe(locale).has_true_token?(token, kind)
|
250
|
+
end
|
251
|
+
alias_method :token_is_true?, :has_true_token?
|
252
|
+
|
253
|
+
# Checks if the given +token+ belonging to a strict kind exists. It may be an alias or a true token.
|
254
|
+
#
|
255
|
+
# @api public
|
256
|
+
# @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
|
257
|
+
# @raise [I18n::InvalidLocale] if the given +locale+ is invalid
|
258
|
+
# @raise [ArgumentError] if the count of arguments is invalid
|
259
|
+
# @overload has_token?(token, kind)
|
260
|
+
# Uses the current locale and the given kind +kind+ to check if the given +token+ exists.
|
261
|
+
# @param [Symbol,String] token name of the checked token
|
262
|
+
# @param [Symbol,String] kind the kind of the given token
|
263
|
+
# @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
|
264
|
+
# @overload has_token?(token, kind, locale)
|
265
|
+
# Uses the given +locale+ and +kind+ to check if the given +token+ exists.
|
266
|
+
# @param [Symbol,String] token name of the checked token
|
267
|
+
# @param [Symbol,String] kind the kind of the given token
|
268
|
+
# @param [Symbol] locale the locale to use
|
269
|
+
# @return [Boolean] +true+ if the given +token+ exists, +false+ otherwise
|
270
|
+
def has_token?(*args)
|
271
|
+
token, kind, locale = tkl_args(args)
|
272
|
+
return false if (token.nil? || kind.nil?)
|
273
|
+
data_safe(locale).has_token?(token, kind)
|
274
|
+
end
|
275
|
+
alias_method :token_exists?, :has_token?
|
276
|
+
|
277
|
+
# Gets true token for the given +token+ belonging to
|
278
|
+
# a strict kind. If the token is an alias it will be resolved
|
279
|
+
# and a true token (target) will be returned.
|
280
|
+
#
|
281
|
+
# @api public
|
282
|
+
# @return [Symbol,nil] the true token or +nil+
|
283
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
284
|
+
# @overload true_token(token, kind)
|
285
|
+
# Uses the current +locale+ and the given +kind+ to get a real token
|
286
|
+
# for the given +token+. If the token is an alias it will be resolved
|
287
|
+
# and a true token (target) will be returned.
|
288
|
+
# @param [Symbol,String] token the identifier of the checked token
|
289
|
+
# @param [Symbol,String] kind the identifier of a kind
|
290
|
+
# @return [Symbol,nil] the true token or +nil+
|
291
|
+
# @overload true_token(token, kind, locale)
|
292
|
+
# Uses the given +locale+ and +kind+ to get a real token
|
293
|
+
# for the given +token+. If the token is an alias it will be resolved
|
294
|
+
# and a true token (target) will be returned.
|
295
|
+
# @param [Symbol,String] token the identifier of the checked token
|
296
|
+
# @param [Symbol,String] kind the identifier of a kind
|
297
|
+
# @param [Symbol] locale the locale to use
|
298
|
+
# @return [Symbol,nil] the true token or +nil+
|
299
|
+
def true_token(*args)
|
300
|
+
token, kind, locale = tkl_args(args)
|
301
|
+
return nil if (token.nil? || kind.nil?)
|
302
|
+
data_safe(locale).get_true_token(token, kind)
|
303
|
+
end
|
304
|
+
alias_method :resolve_alias, :true_token
|
305
|
+
|
306
|
+
# Gets a kind of the given +token+ (which may be an alias) belonging to a strict kind.
|
307
|
+
#
|
308
|
+
# @api public
|
309
|
+
# @return [Symbol,nil] the kind of the given +token+ or +nil+
|
310
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
311
|
+
# @overload kind(token, kind)
|
312
|
+
# Uses current locale and the given +kind+ to get a kind of
|
313
|
+
# the given +token+ (which may be an alias).
|
314
|
+
# @param [Symbol,String] token name of the token or alias
|
315
|
+
# @param [Symbol,String] kind the identifier of a kind (expectations filter)
|
316
|
+
# @return [Symbol,nil] the kind of the given +token+ or +nil+
|
317
|
+
# @overload kind(token, kind, locale)
|
318
|
+
# Uses the given +locale+ to get a kind of the given +token+ (which may be an alias).
|
319
|
+
# @param [Symbol,String] token name of the token or alias
|
320
|
+
# @param [Symbol,String] kind the identifier of a kind (expectations filter)
|
321
|
+
# @param [Symbol] locale the locale to use
|
322
|
+
# @return [Symbol,nil] the kind of the given +token+ or +nil+
|
323
|
+
def kind(token, kind=nil, locale=nil)
|
324
|
+
return nil if (token.nil? || kind.nil? || token.to_s.empty? || kind.to_s.empty?)
|
325
|
+
data_safe(locale).get_kind(token.to_sym, kind.to_sym)
|
326
|
+
end
|
327
|
+
|
328
|
+
# Gets available inflection tokens belonging to a strict kind and their descriptions.
|
329
|
+
#
|
330
|
+
# @api public
|
331
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
332
|
+
# @return [Hash] the hash containing available inflection tokens and descriptions
|
333
|
+
# @note You cannot deduce where aliases are pointing to, since the information
|
334
|
+
# about a target is replaced by the description. To get targets use the
|
335
|
+
# {#raw_tokens} method. To simply list aliases and their targets use
|
336
|
+
# the {#aliases} method.
|
337
|
+
# @overload tokens(kind)
|
338
|
+
# Gets available inflection tokens and their descriptions for some +kind+ and
|
339
|
+
# the current locale.
|
340
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
341
|
+
# @return [Hash] the hash containing available inflection tokens (including
|
342
|
+
# aliases) as keys and their descriptions as values
|
343
|
+
# @overload tokens(kind, locale)
|
344
|
+
# Gets available inflection tokens and their descriptions of the given
|
345
|
+
# +kind+ and +locale+.
|
346
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
347
|
+
# @param [Symbol] locale the locale to use
|
348
|
+
# @return [Hash] the hash containing available inflection tokens (including
|
349
|
+
# aliases) as keys and their descriptions as values
|
350
|
+
def tokens(kind=nil, locale=nil)
|
351
|
+
return {} if (kind.nil? || kind.to_s.empty?)
|
352
|
+
data_safe(locale).get_tokens(kind.to_sym)
|
353
|
+
end
|
354
|
+
|
355
|
+
# Gets available inflection tokens belonging to a strict kind and their values.
|
356
|
+
#
|
357
|
+
# @api public
|
358
|
+
# @return [Hash] the hash containing available inflection tokens and descriptions (or alias pointers)
|
359
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
360
|
+
# @note You may deduce whether the returned values are aliases or true tokens
|
361
|
+
# by testing if a value is a kind of Symbol or a String.
|
362
|
+
# @overload tokens_raw(kind)
|
363
|
+
# Gets available inflection tokens and their values of the given +kind+ and
|
364
|
+
# the current locale.
|
365
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
366
|
+
# @return [Hash] the hash containing available inflection tokens as keys
|
367
|
+
# and their descriptions as values; in case of aliases the returned
|
368
|
+
# values are Symbols
|
369
|
+
# @overload tokens_raw(kind, locale)
|
370
|
+
# Gets available inflection tokens (and their values) of the given +kind+ and +locale+.
|
371
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
372
|
+
# @param [Symbol] locale the locale to use
|
373
|
+
# @return [Hash] the hash containing available inflection tokens as keys
|
374
|
+
# and their descriptions as values. In case of aliases the returned
|
375
|
+
# values are Symbols
|
376
|
+
def tokens_raw(kind=nil, locale=nil)
|
377
|
+
return {} if (kind.nil? || kind.to_s.empty?)
|
378
|
+
data_safe(locale).get_raw_tokens(kind.to_sym)
|
379
|
+
end
|
380
|
+
alias_method :raw_tokens, :tokens_raw
|
381
|
+
|
382
|
+
# Gets true inflection tokens belonging to a strict kind and their values.
|
383
|
+
#
|
384
|
+
# @api public
|
385
|
+
# @return [Hash] the hash containing available inflection tokens and descriptions
|
386
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
387
|
+
# @note It returns only true tokens, not aliases.
|
388
|
+
# @overload tokens_true(kind)
|
389
|
+
# Gets true inflection tokens (and their values) of the given +kind+ and
|
390
|
+
# the current locale.
|
391
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
392
|
+
# @return [Hash] the hash containing available inflection tokens as keys
|
393
|
+
# and their descriptions as values
|
394
|
+
# @overload tokens_true(kind, locale)
|
395
|
+
# Gets true inflection tokens (and their values) of the given +kind+ and +locale+.
|
396
|
+
# @param [Symbol,String] kind the kind of inflection tokens to be returned
|
397
|
+
# @param [Symbol] locale the locale to use
|
398
|
+
# @return [Hash] the hash containing available inflection tokens as keys
|
399
|
+
# and their descriptions as values
|
400
|
+
def tokens_true(kind=nil, locale=nil)
|
401
|
+
return {} if (kind.nil? || kind.to_s.empty?)
|
402
|
+
data_safe(locale).get_true_tokens(kind.to_sym)
|
403
|
+
end
|
404
|
+
alias_method :true_tokens, :tokens_true
|
405
|
+
|
406
|
+
# Gets inflection aliases belonging to a strict kind and their pointers.
|
407
|
+
#
|
408
|
+
# @api public
|
409
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
410
|
+
# @return [Hash] the Hash containing available inflection aliases (<tt>alias => target</tt>)
|
411
|
+
# @overload aliases(kind)
|
412
|
+
# Gets inflection aliases (and their pointers) of the given +kind+ and the current locale.
|
413
|
+
# @param [Symbol,String] kind the kind of aliases to get
|
414
|
+
# @return [Hash] the Hash containing available inflection aliases
|
415
|
+
# @overload aliases(kind, locale)
|
416
|
+
# Gets inflection aliases (and their pointers) of the given +kind+ and +locale+.
|
417
|
+
# @param [Symbol,String] kind the kind of aliases to get
|
418
|
+
# @param [Symbol] locale the locale to use
|
419
|
+
# @return [Hash] the Hash containing available inflection aliases
|
420
|
+
def aliases(kind=nil, locale=nil)
|
421
|
+
return {} if (kind.nil? || kind.to_s.empty?)
|
422
|
+
data_safe(locale).get_aliases(kind.to_sym)
|
423
|
+
end
|
424
|
+
|
425
|
+
# Gets the description of the given inflection token belonging to a strict kind.
|
426
|
+
#
|
427
|
+
# @api public
|
428
|
+
# @note If the given +token+ is really an alias it
|
429
|
+
# returns the description of the true token that
|
430
|
+
# it points to.
|
431
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
432
|
+
# @return [String,nil] the descriptive string or +nil+
|
433
|
+
# @overload token_description(token, kind)
|
434
|
+
# Uses the current locale and the given +token+ to get a description of that token.
|
435
|
+
# @param [Symbol,String] token the token
|
436
|
+
# @param [Symbol,String] kind the identifier of a kind
|
437
|
+
# @return [String,nil] the descriptive string or +nil+ if something
|
438
|
+
# went wrong (e.g. token was not found)
|
439
|
+
# @overload token_description(token, kind, locale)
|
440
|
+
# Uses the given +locale+ and the given +token+ to get a description of that token.
|
441
|
+
# @param [Symbol,String] token the token
|
442
|
+
# @param [Symbol,String] kind the identifier of a kind
|
443
|
+
# @param [Symbol] locale the locale to use
|
444
|
+
# @return [String,nil] the descriptive string or +nil+ if something
|
445
|
+
# went wrong (e.g. token was not found)
|
446
|
+
def token_description(*args)
|
447
|
+
token, kind, locale = tkl_args(args)
|
448
|
+
return nil if (token.nil? || kind.nil?)
|
449
|
+
data_safe(locale).get_description(token, kind)
|
450
|
+
end
|
451
|
+
|
452
|
+
protected
|
453
|
+
|
454
|
+
# @private
|
455
|
+
def data(locale=nil)
|
456
|
+
@idb[prep_locale(locale)]
|
457
|
+
end
|
458
|
+
|
459
|
+
# @private
|
460
|
+
def data_safe(locale=nil)
|
461
|
+
@idb[prep_locale(locale)] || I18n::Inflector::InflectionData_Strict.new(locale)
|
462
|
+
end
|
463
|
+
|
464
|
+
# This method is the internal helper that prepares arguments
|
465
|
+
# containing +token+, +kind+ and +locale+.
|
466
|
+
#
|
467
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
468
|
+
# @raise [ArgumentError] if the count of arguments is invalid
|
469
|
+
# @return [Array<Symbol,Symbol,Symbol>] the array containing
|
470
|
+
# cleaned and validated +token+, +kind+ and +locale+
|
471
|
+
# @overload tkl_args(token, kind, locale)
|
472
|
+
# Prepares arguments containing +token+, +kind+ and +locale+.
|
473
|
+
# @param [String,Hash] token the token
|
474
|
+
# @param [String,Hash] kind the inflection kind
|
475
|
+
# @param [String,Hash] locale the locale identifier
|
476
|
+
# @return [Array<Symbol,Symbol,Symbol>] the array containing
|
477
|
+
# cleaned and validated +token+, +kind+ and +locale+
|
478
|
+
# @overload tkl_args(token, kind)
|
479
|
+
# Prepares arguments containing +token+ and +locale+.
|
480
|
+
# @param [String,Hash] token the token
|
481
|
+
# @param [String,Hash] kind the inflection kind
|
482
|
+
# @return [Array<Symbol,Symbol,Symbol>] the array containing
|
483
|
+
# cleaned and validated +token+, +kind+ and +locale+
|
484
|
+
# @overload tkl_args(token)
|
485
|
+
# Prepares arguments containing +token+.
|
486
|
+
# @param [String,Hash] token the token
|
487
|
+
# @return [Array<Symbol,Symbol,Symbol>] the array containing
|
488
|
+
# cleaned and validated +token+ and the current locale
|
489
|
+
def tkl_args(args)
|
490
|
+
token, kind, locale = case args.count
|
491
|
+
when 1 then [args[0], nil, nil]
|
492
|
+
when 2 then [args[0], args[1], nil]
|
493
|
+
when 3 then args
|
494
|
+
else raise ArgumentError.new("wrong number of arguments: #{args.count} for (1..3)")
|
495
|
+
end
|
496
|
+
token = token.nil? || token.to_s.empty? ? nil : token.to_sym
|
497
|
+
kind = kind.nil? || kind.to_s.empty? ? nil : kind.to_sym
|
498
|
+
[token,kind,locale]
|
499
|
+
end
|
500
|
+
|
501
|
+
# Processes +locale+ identifier and validates
|
502
|
+
# whether it's correct (not empty and not +nil+).
|
503
|
+
#
|
504
|
+
# @note If the +locale+ is not correct, it
|
505
|
+
# tries to use locale from {I18n.locale} and validates it
|
506
|
+
# as well.
|
507
|
+
# @param [Symbol,String] locale the locale identifier
|
508
|
+
# @raise [I18n::InvalidLocale] if there is no proper locale name
|
509
|
+
# @return [Symbol] the given locale or the global locale
|
510
|
+
def prep_locale(locale=nil)
|
511
|
+
locale ||= I18n.locale
|
512
|
+
raise I18n::InvalidLocale.new(locale) if (locale.nil? || locale.to_s.empty?)
|
513
|
+
locale.to_sym
|
514
|
+
end
|
515
|
+
|
516
|
+
end # class API_Strict
|
517
|
+
|
518
|
+
end # module Inflector
|
519
|
+
end # module I18n
|