Class: Tater
- Inherits:
-
Object
- Object
- Tater
- Defined in:
- lib/tater.rb,
lib/tater/hash.rb,
lib/tater/utils.rb,
lib/tater/aliases.rb,
lib/tater/version.rb
Overview
:nodoc:
Defined Under Namespace
Modules: HashExcept, Utils Classes: MissingLocalizationFormat, UnLocalizableObject
Constant Summary collapse
- DEFAULT =
'default'
- DELIMITING_REGEX =
/(\d)(?=(\d\d\d)+(?!\d))/.freeze
- HASH =
{}.freeze
- SEPARATOR =
'.'
- SUBSTITUTION_REGEX =
/%(|\^)[aAbBpP]/.freeze
- VERSION =
'3.0.6'
Instance Attribute Summary collapse
-
#available ⇒ Array<String>
readonly
An array of the available locale codes found in loaded messages.
- #locale ⇒ String
- #messages ⇒ Hash readonly
Instance Method Summary collapse
-
#available?(locale) ⇒ Boolean
Is this locale available in our current set of messages?.
-
#cascades? ⇒ Boolean
Do lookups cascade by default?.
-
#includes?(key, options = HASH) ⇒ Boolean
Check that there’s a key at the given path.
-
#initialize(cascade: false, locale: nil, messages: nil, path: nil) ⇒ Tater
constructor
A new instance of Tater.
- #inspect ⇒ String
-
#load(path: nil, messages: nil) ⇒ Object
Load messages into our internal cache, either from a path containing YAML files or a Hash of messages.
-
#localize(object, options = HASH) ⇒ String
(also: #l)
Localize an Array, Date, Time, DateTime, or Numeric object.
-
#lookup(key, locale: nil, cascade: nil) ⇒ Object
Lookup a key in the messages hash, using the current locale or an override.
-
#translate(key, options = HASH) ⇒ String
(also: #t)
Translate a key path and optional interpolation arguments into a string.
Constructor Details
#initialize(cascade: false, locale: nil, messages: nil, path: nil) ⇒ Tater
Returns a new instance of Tater.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tater.rb', line 45 def initialize(cascade: false, locale: nil, messages: nil, path: nil) @available = [] @cache = {} @cascade = cascade @locale = locale @messages = {} load(path: path) if path load(messages: ) if end |
Instance Attribute Details
#available ⇒ Array<String> (readonly)
An array of the available locale codes found in loaded messages.
29 30 31 |
# File 'lib/tater.rb', line 29 def available @available end |
#locale ⇒ String
32 33 34 |
# File 'lib/tater.rb', line 32 def locale @locale end |
#messages ⇒ Hash (readonly)
35 36 37 |
# File 'lib/tater.rb', line 35 def @messages end |
Instance Method Details
#available?(locale) ⇒ Boolean
Is this locale available in our current set of messages?
71 72 73 |
# File 'lib/tater.rb', line 71 def available?(locale) available.include?(locale.to_s) end |
#cascades? ⇒ Boolean
Do lookups cascade by default?
64 65 66 |
# File 'lib/tater.rb', line 64 def cascades? @cascade end |
#includes?(key, options = HASH) ⇒ Boolean
Check that there’s a key at the given path.
219 220 221 222 223 |
# File 'lib/tater.rb', line 219 def includes?(key, = HASH) return !lookup(key).nil? if .empty? !(key, ).nil? end |
#inspect ⇒ String
57 58 59 |
# File 'lib/tater.rb', line 57 def inspect %(#<Tater:#{ object_id } @cascade=#{ @cascade } @locale="#{ @locale }" @available=#{ @available }>) end |
#load(path: nil, messages: nil) ⇒ Object
Load messages into our internal cache, either from a path containing YAML files or a Hash of messages.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tater.rb', line 82 def load(path: nil, messages: nil) return if path.nil? && .nil? if path Dir.glob(File.join(path, '**', '*.{yml,yaml}')).each do |file| @messages = Utils.deep_merge(@messages, YAML.load_file(file)) end Dir.glob(File.join(path, '**', '*.rb')).each do |file| @messages = Utils.deep_merge(@messages, Utils.deep_stringify_keys(eval(File.read(file), binding, file))) # rubocop:disable Security/Eval end end @messages = Utils.deep_merge(@messages, Utils.deep_stringify_keys()) if @messages = Utils.deep_freeze(@messages) # Update our available locales. @available.replace(@messages.keys.map(&:to_s).sort) # Not only does this clear our cache but it establishes the basic structure # that we rely on in other methods. @cache.clear @messages.each_key do |key| @cache[key] = { false => {}, true => {} } end end |
#localize(object, options = HASH) ⇒ String Also known as: l
Localize an Array, Date, Time, DateTime, or Numeric object.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/tater.rb', line 144 def localize(object, = HASH) case object when String object when Numeric localize_numeric(object, ) when Date, Time, DateTime localize_datetime(object, ) when Array localize_array(object, ) else raise(UnLocalizableObject, "The object class #{ object.class } cannot be localized by Tater.") end end |
#lookup(key, locale: nil, cascade: nil) ⇒ Object
Lookup a key in the messages hash, using the current locale or an override.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/tater.rb', line 174 def lookup(key, locale: nil, cascade: nil) locale = if locale.nil? @locale else locale.to_s end cascade = @cascade if cascade.nil? @cache[locale][cascade][key] ||= begin path = key.split(SEPARATOR) = @messages[locale].dig(*path) if .nil? && cascade = while path.length > 1 path.delete_at(path.length - 2) attempt = @messages[locale].dig(*path) break attempt unless attempt.nil? end end end end |
#translate(key, options = HASH) ⇒ String Also known as: t
Translate a key path and optional interpolation arguments into a string. It’s effectively a combination of #lookup and #interpolate.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/tater.rb', line 250 def translate(key, = HASH) if .empty? case ( = lookup(key)) when String when Proc .call(key) else "Tater lookup failed: #{ locale }.#{ key }" end else case ( = (key, )) when String return unless Utils.interpolation_string?() Utils.interpolate!(, .except(:cascade, :default, :locale, :locales)) when Proc .call(key, .except(:cascade, :default, :locale, :locales)) else [:default] || "Tater lookup failed: #{ [:locale] || [:locales] || locale }.#{ key }" end end end |