Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/coaster/core_ext/object_translation.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#_translate(*args) ⇒ Object
Foo::Bar.new._translate #=> return translation ‘class.Foo.Bar.self’ Foo::Bar.new._translate(‘.title’) #=> return translation ‘class.Foo.Bar.title’ Foo::Bar.new._translate(‘title’) #=> return translation ‘title’ Foo::Bar.new._translate(:force) #=> ignore ‘message’ even if message exists.
- #_translate_params ⇒ Object
Class Method Details
._translate(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/coaster/core_ext/object_translation.rb', line 5 def _translate(*args) = args.last.is_a?(Hash) ? args.pop.dup : {} .merge!(_translate_params) key = args.shift subkey = nil if key.is_a?(String) if key.start_with?('.') subkey = key else return I18n.t(key, *args, ) end elsif key.is_a?(Symbol) subkey = ".#{key.to_s}" elsif key.nil? # do nothing else return I18n.t(key, *args, ) end key_class = .delete(:class) || self subkey = '.self' unless subkey key = key_class.name.gsub(/::/, '.') key = 'class.' + key + subkey unless .key?(:original_throw) [:original_throw] = .delete(:throw) end [:tkey] ||= key .merge!(throw: true) .symbolize_keys! result = catch(:exception) do I18n.t(key, *args, ) end if result.is_a?(I18n::MissingTranslation) Coaster.logger && Coaster.logger.warn(result) unless .key?(:original_missing) .merge!(original_missing: result) end if key_class.superclass == Object || key_class == Object return [:description] if [:description].present? throw :exception, result if [:original_throw] missing = [:original_missing] || result msg = missing. msg.instance_variable_set(:@missing, missing) msg.instance_variable_set(:@tkey, [:tkey]) msg else [:class] = key_class.superclass _translate(subkey, *args, ) end else result.instance_variable_set(:@translated, true) result.instance_variable_set(:@tkey, [:tkey]) result end end |
._translate_params ⇒ Object
66 67 68 |
# File 'lib/coaster/core_ext/object_translation.rb', line 66 def _translate_params {} end |
Instance Method Details
#_translate(*args) ⇒ Object
Foo::Bar.new._translate #=> return translation ‘class.Foo.Bar.self’ Foo::Bar.new._translate(‘.title’) #=> return translation ‘class.Foo.Bar.title’ Foo::Bar.new._translate(‘title’) #=> return translation ‘title’ Foo::Bar.new._translate(:force) #=> ignore ‘message’ even if message exists
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/coaster/core_ext/object_translation.rb', line 76 def _translate(*args) = args.last.is_a?(Hash) ? args.pop.dup : {} key = args.shift || (respond_to?(:tkey) ? tkey : nil) if respond_to?(:description) && description.present? && description != 'false' && description != self.class.name if !key.is_a?(String) && key != :force desc = description return desc unless desc.instance_variable_get(:@raw) end .merge!(description: description) end .merge!(_translate_params) self.class._translate(key, *args, ) end |
#_translate_params ⇒ Object
92 93 94 |
# File 'lib/coaster/core_ext/object_translation.rb', line 92 def _translate_params {} end |