Class: R18n::TranslatedString
- Inherits:
-
String
- Object
- String
- R18n::TranslatedString
- Defined in:
- lib/r18n-core/translated_string.rb
Overview
String, which is translated to some locale and loading from Translation.
Constant Summary collapse
- NON_KEYS_METHODS =
[ *Untranslated::NON_KEYS_METHODS, :html_safe, :to_text ].freeze
Instance Attribute Summary collapse
-
#locale ⇒ Object
readonly
String locale.
-
#path ⇒ Object
readonly
Path for this translation.
Class Method Summary collapse
-
._load(str) ⇒ Object
Load object from Marshalizing.
Instance Method Summary collapse
-
#_dump(_limit) ⇒ Object
Override marshal_dump to avoid Marshalizing filter procs.
-
#as_json(_options = nil) ⇒ Object
Define ‘as_json` for ActiveSupport compatibility.
-
#get_untranslated(key) ⇒ Object
Return untranslated for deeper node ‘key`.
-
#initialize(value, locale, path, filters = nil) ⇒ TranslatedString
constructor
Returns a new string object containing a copy of ‘str`, which translated for `path` to `locale`.
-
#method_missing(name, *_params) ⇒ Object
Return untranslated, when user try to go deeper in translation.
- #respond_to_missing?(name, *args) ⇒ Boolean
-
#to_s ⇒ Object
Override to_s to make string html safe if ‘html_safe` method is defined.
-
#translated? ⇒ Boolean
Return true for translated strings.
-
#|(_other) ⇒ Object
Return self for translated string.
Constructor Details
#initialize(value, locale, path, filters = nil) ⇒ TranslatedString
Returns a new string object containing a copy of ‘str`, which translated for `path` to `locale`
33 34 35 36 37 38 |
# File 'lib/r18n-core/translated_string.rb', line 33 def initialize(value, locale, path, filters = nil) super(value.to_s) @filters = filters @locale = locale @path = path end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *_params) ⇒ Object
Return untranslated, when user try to go deeper in translation.
89 90 91 92 93 |
# File 'lib/r18n-core/translated_string.rb', line 89 def method_missing(name, *_params) return super if NON_KEYS_METHODS.include?(name) get_untranslated(name.to_s) end |
Instance Attribute Details
#locale ⇒ Object (readonly)
String locale
26 27 28 |
# File 'lib/r18n-core/translated_string.rb', line 26 def locale @locale end |
#path ⇒ Object (readonly)
Path for this translation.
29 30 31 |
# File 'lib/r18n-core/translated_string.rb', line 29 def path @path end |
Class Method Details
._load(str) ⇒ Object
Load object from Marshalizing.
70 71 72 73 |
# File 'lib/r18n-core/translated_string.rb', line 70 def self._load(str) arr = str.split(':', 3) new arr[2], R18n.locale(arr[0]), arr[1] end |
Instance Method Details
#_dump(_limit) ⇒ Object
Override marshal_dump to avoid Marshalizing filter procs
65 66 67 |
# File 'lib/r18n-core/translated_string.rb', line 65 def _dump(_limit) [@locale.code, @path, to_str].join(':') end |
#as_json(_options = nil) ⇒ Object
Define ‘as_json` for ActiveSupport compatibility.
60 61 62 |
# File 'lib/r18n-core/translated_string.rb', line 60 def as_json( = nil) to_str end |
#get_untranslated(key) ⇒ Object
Return untranslated for deeper node ‘key`. It is in separated methods to be used in R18n I18n backend.
77 78 79 80 |
# File 'lib/r18n-core/translated_string.rb', line 77 def get_untranslated(key) translated = @path.empty? ? '' : "#{@path}." Untranslated.new(translated, key, @locale, @filters) end |
#respond_to_missing?(name, *args) ⇒ Boolean
95 96 97 98 99 |
# File 'lib/r18n-core/translated_string.rb', line 95 def respond_to_missing?(name, *args) return super if NON_KEYS_METHODS.include?(name) true end |
#to_s ⇒ Object
Override to_s to make string html safe if ‘html_safe` method is defined.
51 52 53 54 55 56 57 |
# File 'lib/r18n-core/translated_string.rb', line 51 def to_s if respond_to?(:html_safe) super.html_safe else String.new(super) end end |
#translated? ⇒ Boolean
Return true for translated strings.
46 47 48 |
# File 'lib/r18n-core/translated_string.rb', line 46 def translated? true end |
#|(_other) ⇒ Object
Return self for translated string.
41 42 43 |
# File 'lib/r18n-core/translated_string.rb', line 41 def |(_other) self end |