Class: I18n::Inflector::API
- Inherits:
-
API_Strict
- Object
- API_Strict
- I18n::Inflector::API
- Includes:
- Interpolate
- Defined in:
- lib/i18n-inflector/api.rb,
lib/i18n-inflector/long_comments.rb
Overview
Instance of this class, the inflector, is attached to I18n backend. This class contains common operations that can be performed on inflections. It can operate on both unnamed an named patterns (regular and strict kinds). This class is used by backend methods to interpolate strings and load inflections.
It uses the databases containing instances of InflectionData and InflectionData_Strict that are stored in the Hashes and indexed by locale names.
Note that strict kinds used to handle named patterns internally are stored in a different database than regular kinds. Most of the methods of this class are also aware of strict kinds and will call proper methods handling strict inflection data when the @ symbol is detected at the beginning of the given identifier of a kind.
Usage
You can access the instance of this class attached to default I18n backend by calling:
I18n.backend.inflector
or in a short form:
I18n.inflector
In case of named patterns (strict kinds):
I18n.inflector.strict
Direct Known Subclasses
Constant Summary
Constants included from Config
Config::MULTI_REGEXP, Config::MULTI_RESTR, Config::OPTION_PREFIX, Config::OPTION_PREFIX_REGEXP, Config::PATTERN_REGEXP, Config::PATTERN_RESTR, Config::TOKENS_REGEXP, Config::TOKENS_RESTR
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
This reader allows to reach internal configuration of the engine.
-
#options ⇒ I18n::Inflector::InflectionOptions
readonly
Options controlling the engine.
-
#strict ⇒ I18n::Inflector::API_Strict
readonly
This reader allows to reach a reference of the object that is a kind of API_Strict and handles inflections for named patterns (strict kinds).
Instance Method Summary collapse
-
#add_database(db, db_strict = nil) ⇒ I18n::Inflector::InflectionData, ...
(also: #add_databases)
Attaches instance of InflectionData and optionally InflectionData_Strict to the inflector.
-
#default_token(kind, locale = nil) ⇒ Symbol?
Reads default token for the given
kind
. -
#delete_databases(locale)
Deletes the internal databases for the specified locale.
-
#each_alias(kind = nil, locale = nil) {|alias, target| ... } ⇒ LazyHashEnumerator
Iterates through inflection aliases and their pointers.
-
#each_inflected_locale(kind = nil) {|locale| ... } ⇒ LazyArrayEnumerator
(also: #each_locale, #each_supported_locale)
Iterates through locales which have configured inflection support.
-
#each_token(kind = nil, locale = nil) {|token, description| ... } ⇒ LazyHashEnumerator
Iterates through available inflection tokens and their descriptions.
-
#each_token_raw(kind = nil, locale = nil) {|token, value| ... } ⇒ LazyHashEnumerator
(also: #each_raw_token)
Iterates through available inflection tokens and their values.
-
#each_token_true(kind = nil, locale = nil) {|token, description| ... } ⇒ LazyHashEnumerator
(also: #each_true_token)
Iterates through true inflection tokens and their values.
-
#has_alias?(*args) ⇒ Boolean
(also: #token_has_alias?)
Checks if the given
token
is an alias. -
#has_kind?(kind, locale = nil) ⇒ Boolean
Tests if a kind exists.
-
#has_token?(*args) ⇒ Boolean
(also: #token_exists?)
Checks if the given
token
exists. -
#has_true_token?(*args) ⇒ Boolean
(also: #token_has_true?)
Checks if the given
token
is a true token (not alias). -
#inflected_locale?(locale = nil) ⇒ Boolean
(also: #locale?, #locale_supported?)
Checks if the given locale was configured to support inflection.
-
#inflected_locales(kind = nil) ⇒ Array<Symbol>
Gets locales which have configured inflection support.
-
#initialize ⇒ API
constructor
Initilizes the inflector by creating internal databases used for storing inflection data and options.
-
#kind(*args) ⇒ Symbol?
Gets a kind for the given
token
(which may be an alias). -
#kinds(locale = nil) ⇒ Array<Symbol>
(also: #inflection_kinds)
Gets known regular inflection kinds.
-
#new_database(locale) ⇒ I18n::Inflector::InflectionData
Creates a database for the specified locale.
-
#new_databases(locale) ⇒ Array<I18n::Inflector::InflectionData,I18n::Inflector::InflectionData_Strict>
Creates internal databases (regular and strict) for the specified locale.
-
#tkl_args(args) ⇒ Array<Symbol,Symbol,Symbol>
protected
This method is the internal helper that prepares arguments containing
token
,kind
andlocale
. -
#token_description(*args) ⇒ String?
Gets the description of the given inflection token.
-
#true_token(*args) ⇒ Symbol?
(also: #resolve_alias)
Gets true token for the given
token
.
Methods included from Interpolate
Methods inherited from API_Strict
#aliases, #delete_database, #each_kind, #prep_locale, #tokens, #tokens_raw, #tokens_true
Constructor Details
#initialize ⇒ API
Initilizes the inflector by creating internal databases used for storing inflection data and options.
83 84 85 86 |
# File 'lib/i18n-inflector/api.rb', line 83 def initialize super(nil, nil) @idb_strict = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
This reader allows to reach internal configuration of the engine. It is shared among all instances of the Inflector and also available as I18n::Inflector::Config.
70 71 72 |
# File 'lib/i18n-inflector/api.rb', line 70 def config I18n::Inflector::Config end |
#options ⇒ I18n::Inflector::InflectionOptions (readonly)
Options controlling the engine.
67 68 69 |
# File 'lib/i18n-inflector/api.rb', line 67 def @options end |
#strict ⇒ I18n::Inflector::API_Strict (readonly)
This reader allows to reach a reference of the object that is a kind of I18n::Inflector::API_Strict and handles inflections for named patterns (strict kinds).
75 76 77 |
# File 'lib/i18n-inflector/api.rb', line 75 def strict @strict ||= I18n::Inflector::API_Strict.new(@idb_strict, @options) end |
Instance Method Details
#add_database(db) ⇒ I18n::Inflector::InflectionData? #add_database(db, db_strict) ⇒ Array<I18n::Inflector::InflectionData,I18n::Inflector::InflectionData_Strict>? Also known as: add_databases
It doesn’t create a copy of inflection data, but registers the given object(s).
Attaches instance of InflectionData and optionally InflectionData_Strict to the inflector.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/i18n-inflector/api.rb', line 134 def add_database(db, db_strict=nil) r = super(db) return r if (r.nil? || db_strict.nil?) r_strict = strict.add_database(db_strict) if r_strict.nil? delete_database(db.locale) return nil end [r, r_strict] end |
#default_token(kind) ⇒ Symbol? #default_token(kind, locale) ⇒ Symbol?
If kind
begins with the @ symbol then the variant of this method operating on strict kinds will be called (I18n::Inflector::API_Strict#default_token)
Reads default token for the given kind
.
267 268 269 270 271 272 273 |
# File 'lib/i18n-inflector/api.rb', line 267 def default_token(kind, locale=nil) return nil if (kind.nil? || kind.to_s.empty?) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.default_token(kind.to_s[1..-1], locale) end super end |
#delete_databases(locale)
It detaches the databases from I18n::Inflector::API instance. Other objects referring to them may still use it.
This method returns an undefined value.
Deletes the internal databases for the specified locale.
154 155 156 157 |
# File 'lib/i18n-inflector/api.rb', line 154 def delete_databases(locale) delete_database(locale) strict.delete_database(locale) end |
#each_alias(kind) ⇒ LazyHashEnumerator #each_alias(kind, locale) ⇒ LazyHashEnumerator
Iterates through inflection aliases and their pointers.
635 636 637 638 639 640 |
# File 'lib/i18n-inflector/api.rb', line 635 def each_alias(kind=nil, locale=nil, &block) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.each_alias(kind.to_s[1..-1], locale, &block) end super end |
#each_inflected_locale ⇒ LazyArrayEnumerator #each_inflected_locale(kind) ⇒ LazyArrayEnumerator Also known as: each_locale, each_supported_locale
This method uses information from both regular and strict kinds. The locale identifiers may be duplicated!
Iterates through locales which have configured inflection support.
219 220 221 |
# File 'lib/i18n-inflector/api.rb', line 219 def each_inflected_locale(kind=nil, &block) super + strict.inflected_locales(kind) end |
#each_token ⇒ LazyHashEnumerator #each_token(kind) ⇒ LazyHashEnumerator #each_token(kind, locale) ⇒ LazyHashEnumerator
By default it uses regular kinds database, not strict kinds.
You cannot deduce where aliases are pointing to, since the information about a target is replaced by the description. To get targets use the I18n::Inflector::API_Strict#raw_tokens method. To simply list aliases and their targets use the I18n::Inflector::API_Strict#aliases method.
Iterates through available inflection tokens and their descriptions.
537 538 539 540 541 542 |
# File 'lib/i18n-inflector/api.rb', line 537 def each_token(kind=nil, locale=nil) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.each_token(kind.to_s[1..-1], locale) end super end |
#each_token_raw ⇒ LazyHashEnumerator #each_token_raw(kind) ⇒ LazyHashEnumerator #each_token_raw(kind, locale) ⇒ LazyHashEnumerator Also known as: each_raw_token
You may deduce whether the returned values are aliases or true tokens by testing if a value is a type of Symbol or String.
Iterates through available inflection tokens and their values.
571 572 573 574 575 576 |
# File 'lib/i18n-inflector/api.rb', line 571 def each_token_raw(kind=nil, locale=nil) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.each_token_raw(kind.to_s[1..-1], locale) end super end |
#each_token_true ⇒ LazyHashEnumerator #each_token_true(kind) ⇒ LazyHashEnumerator #each_token_true(kind, locale) ⇒ LazyHashEnumerator Also known as: each_true_token
It returns only true tokens, not aliases.
Iterates through true inflection tokens and their values.
605 606 607 608 609 610 |
# File 'lib/i18n-inflector/api.rb', line 605 def each_token_true(kind=nil, locale=nil, &block) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.each_token_true(kind.to_s[1..-1], locale, &block) end super end |
#has_alias?(token) ⇒ Boolean #has_alias?(token, locale) ⇒ Boolean #has_alias?(token, kind, locale) ⇒ Boolean #has_alias?(token, strict_kind) ⇒ Boolean Also known as: token_has_alias?
By default it uses regular kinds database, not strict kinds.
Checks if the given token
is an alias.
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/i18n-inflector/api.rb', line 306 def has_alias?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s reutrn false if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.has_alias?(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).has_alias?(token.to_sym, kind) end |
#has_kind?(kind) ⇒ Boolean #has_kind?(kind, locale) ⇒ Boolean
If kind
begins with the @ symbol then the variant of this method operating on strict kinds will be called (I18n::Inflector::API_Strict#has_kind?)
Tests if a kind exists.
242 243 244 245 246 247 |
# File 'lib/i18n-inflector/api.rb', line 242 def has_kind?(kind, locale=nil) if kind.to_s[0..0] == Markers::STRICT_KIND return strict.has_kind?(kind.to_s[1..-1], locale) end super end |
#has_token?(token) ⇒ Boolean #has_token?(token, locale) ⇒ Boolean #has_token?(token, kind, locale) ⇒ Boolean #has_token?(token, strict_kind) ⇒ Boolean Also known as: token_exists?
By default it uses regular kinds database, not strict kinds.
Checks if the given token
exists. It may be an alias or a true token.
398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/i18n-inflector/api.rb', line 398 def has_token?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s return false if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.has_token?(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).has_token?(token.to_sym, kind) end |
#has_true_token?(token) ⇒ Boolean #has_true_token?(token, locale) ⇒ Boolean #has_true_token?(token, kind, locale) ⇒ Boolean #has_true_token?(token, strict_kind) ⇒ Boolean Also known as: token_has_true?
By default it uses regular kinds database, not strict kinds.
Checks if the given token
is a true token (not alias).
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/i18n-inflector/api.rb', line 352 def has_true_token?(*args) token, kind, locale = tkl_args(args) return false if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s return false if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.has_true_token?(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).has_true_token?(token.to_sym, kind) end |
#inflected_locale?(locale) ⇒ Boolean #inflected_locale? ⇒ Boolean Also known as: locale?, locale_supported?
That method uses information from regular and strict kinds.
Checks if the given locale was configured to support inflection.
172 173 174 |
# File 'lib/i18n-inflector/api.rb', line 172 def inflected_locale?(locale=nil) super || strict.inflected_locale?(locale) end |
#inflected_locales ⇒ Array<Symbol> #inflected_locales(kind) ⇒ Array<Symbol>
This method uses information from both regular and strict kinds.
Gets locales which have configured inflection support.
193 194 195 196 197 198 199 200 201 |
# File 'lib/i18n-inflector/api.rb', line 193 def inflected_locales(kind=nil) if kind.to_s[0..0] == Markers::STRICT_KIND strict.inflected_locales(kind.to_s[1..-1]) else kind = kind.to_s.empty? ? nil : kind.to_sym r = ( @inflected_locales_cache[kind] ||= super(kind).uniq ) r.nil? ? r : r.dup end end |
#kind(token) ⇒ Symbol? #kind(token, locale) ⇒ Symbol? #kind(token, kind, locale) ⇒ Symbol? #kind(token, strict_kind) ⇒ Symbol?
By default it uses regular kinds database, not strict kinds.
Gets a kind for the given token
(which may be an alias).
493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/i18n-inflector/api.rb', line 493 def kind(*args) token, kind, locale = tkl_args(args) return nil if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s return nil if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.kind(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).get_kind(token.to_sym, kind) end |
#kinds ⇒ Array<Symbol> #kinds(locale) ⇒ Array<Symbol> Also known as: inflection_kinds
To get all inflection kinds (regular and strict) for default inflector use: I18n.inflector.kinds + I18n.inflector.strict.kinds
Gets known regular inflection kinds.
46 |
# File 'lib/i18n-inflector/long_comments.rb', line 46 def kinds(locale=nil); super end |
#new_database(locale) ⇒ I18n::Inflector::InflectionData
Creates a database for the specified locale.
94 95 96 97 |
# File 'lib/i18n-inflector/api.rb', line 94 def new_database(locale) locale = prep_locale(locale) @idb[locale] = I18n::Inflector::InflectionData.new(locale) end |
#new_databases(locale) ⇒ Array<I18n::Inflector::InflectionData,I18n::Inflector::InflectionData_Strict>
Creates internal databases (regular and strict) for the specified locale.
106 107 108 109 110 |
# File 'lib/i18n-inflector/api.rb', line 106 def new_databases(locale) normal = new_databases(locale) strict = strict.new_database(locale) [normal, strict] end |
#tkl_args(token, kind, locale) ⇒ Array<Symbol,Symbol,Symbol> (protected) #tkl_args(token, locale) ⇒ Array<Symbol,Symbol,Symbol> (protected) #tkl_args(token) ⇒ Array<Symbol,Symbol,Symbol> (protected) #tkl_args(token, strict_kind) ⇒ Array<Symbol,Symbol,Symbol> (protected)
This method leaves kind
as is when it’s nil
or empty. It sets token
to nil
when it’s empty.
This method is the internal helper that prepares arguments containing token
, kind
and locale
.
739 740 741 742 743 744 745 746 747 |
# File 'lib/i18n-inflector/api.rb', line 739 def tkl_args(args) token, kind, locale = case args.count when 1 then [args[0], nil, nil] when 2 then args[1].to_s[0..0] == Markers::STRICT_KIND ? [args[0], args[1], nil] : [args[0], nil, args[1]] when 3 then args else raise I18n::ArgumentError.new("wrong number of arguments: #{args.count} for (1..3)") end [token,kind,locale] end |
#token_description(token) ⇒ String? #token_description(token, locale) ⇒ String? #token_description(token, kind, locale) ⇒ String? #token_description(token, strict_kind) ⇒ String?
If the given token
is really an alias it returns the description of the true token that it points to. By default it uses regular kinds database, not strict kinds.
Gets the description of the given inflection token.
680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/i18n-inflector/api.rb', line 680 def token_description(*args) token, kind, locale = tkl_args(args) return nil if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s return nil if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.token_description(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).get_description(token.to_sym, kind) end |
#true_token(token) ⇒ Symbol? #true_token(token, locale) ⇒ Symbol? #true_token(token, kind, locale) ⇒ Symbol? #true_token(token, strict_kind) ⇒ Symbol? Also known as: resolve_alias
By default it uses regular kinds database, not strict kinds.
Gets true token for the given token
. If the token is an alias it will be resolved and a true token (target) will be returned.
448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/i18n-inflector/api.rb', line 448 def true_token(*args) token, kind, locale = tkl_args(args) return nil if (token.nil? || token.to_s.empty?) unless kind.nil? kind = kind.to_s return nil if kind.empty? if kind[0..0] == Markers::STRICT_KIND return strict.true_token(token, kind[1..-1], locale) end kind = kind.to_sym end data_safe(locale).get_true_token(token.to_sym, kind) end |