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) ⇒ void
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
tokenis an alias. -
#has_kind?(kind, locale = nil) ⇒ Boolean
Tests if a kind exists.
-
#has_token?(*args) ⇒ Boolean
(also: #token_exists?)
Checks if the given
tokenexists. -
#has_true_token?(*args) ⇒ Boolean
(also: #token_has_true?)
Checks if the given
tokenis 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.
-
#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 included from Config
all_consts, gen_regexp, get_i18n_reserved_keys
Methods inherited from API_Strict
#aliases, #delete_database, #each_kind, #tokens, #tokens_raw, #tokens_true
Constructor Details
#initialize ⇒ API
Initilizes the inflector by creating internal databases used for storing inflection data and options.
82 83 84 85 |
# File 'lib/i18n-inflector/api.rb', line 82 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.
69 70 71 |
# File 'lib/i18n-inflector/api.rb', line 69 def config I18n::Inflector::Config end |
#options ⇒ I18n::Inflector::InflectionOptions (readonly)
Options controlling the engine.
66 67 68 |
# File 'lib/i18n-inflector/api.rb', line 66 def 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).
74 75 76 |
# File 'lib/i18n-inflector/api.rb', line 74 def strict @strict ||= I18n::Inflector::API_Strict.new(@idb_strict, ) 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.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/i18n-inflector/api.rb', line 133 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.
266 267 268 269 270 271 |
# File 'lib/i18n-inflector/api.rb', line 266 def default_token(kind, locale = nil) return nil if kind.nil? || kind.to_s.empty? return strict.default_token(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND super end |
#delete_databases(locale) ⇒ void
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.
630 631 632 633 634 |
# File 'lib/i18n-inflector/api.rb', line 630 def each_alias(kind = nil, locale = nil, &) return strict.each_alias(kind.to_s[1..], locale, &) if kind.to_s[0..0] == Markers::STRICT_KIND 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, &) 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.
535 536 537 538 539 |
# File 'lib/i18n-inflector/api.rb', line 535 def each_token(kind = nil, locale = nil) return strict.each_token(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND 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.
568 569 570 571 572 |
# File 'lib/i18n-inflector/api.rb', line 568 def each_token_raw(kind = nil, locale = nil) return strict.each_token_raw(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND 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.
601 602 603 604 605 |
# File 'lib/i18n-inflector/api.rb', line 601 def each_token_true(kind = nil, locale = nil, &) return strict.each_token_true(kind.to_s[1..], locale, &) if kind.to_s[0..0] == Markers::STRICT_KIND 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.
304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/i18n-inflector/api.rb', line 304 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? return strict.has_alias?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND 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 |
# File 'lib/i18n-inflector/api.rb', line 242 def has_kind?(kind, locale = nil) return strict.has_kind?(kind.to_s[1..], locale) if kind.to_s[0..0] == Markers::STRICT_KIND 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.
396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/i18n-inflector/api.rb', line 396 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? return strict.has_token?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND 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).
350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/i18n-inflector/api.rb', line 350 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? return strict.has_true_token?(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND 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..]) else kind = kind.to_s.empty? ? nil : kind.to_sym r = (@inflected_locales_cache[kind] ||= super.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).
491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/i18n-inflector/api.rb', line 491 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? return strict.kind(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND 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.
45 |
# File 'lib/i18n-inflector/long_comments.rb', line 45 def kinds(locale = nil) = super |
#new_database(locale) ⇒ I18n::Inflector::InflectionData
Creates a database for the specified locale.
93 94 95 96 |
# File 'lib/i18n-inflector/api.rb', line 93 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.
105 106 107 108 109 |
# File 'lib/i18n-inflector/api.rb', line 105 def new_databases(locale) normal = new_databases(locale) strict = strict.new_database(locale) [normal, strict] 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.
674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/i18n-inflector/api.rb', line 674 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? return strict.token_description(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND 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.
446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/i18n-inflector/api.rb', line 446 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? return strict.true_token(token, kind[1..], locale) if kind[0..0] == Markers::STRICT_KIND kind = kind.to_sym end data_safe(locale).get_true_token(token.to_sym, kind) end |