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.
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`.
-
#html_safe? ⇒ Boolean
Return true if ‘html_safe` method is defined, otherwise false.
-
#initialize(value, locale, path, filters = nil) ⇒ TranslatedString
constructor
Returns a new string object containing a copy of
str
, which translated forpath
tolocale
. -
#method_missing(name, *_params) ⇒ Object
Return untranslated, when user try to go deeper in translation.
-
#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
31 32 33 34 35 36 |
# File 'lib/r18n-core/translated_string.rb', line 31 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.
86 87 88 |
# File 'lib/r18n-core/translated_string.rb', line 86 def method_missing(name, *_params) get_untranslated(name.to_s) end |
Instance Attribute Details
#locale ⇒ Object (readonly)
String locale
24 25 26 |
# File 'lib/r18n-core/translated_string.rb', line 24 def locale @locale end |
#path ⇒ Object (readonly)
Path for this translation.
27 28 29 |
# File 'lib/r18n-core/translated_string.rb', line 27 def path @path end |
Class Method Details
Instance Method Details
#_dump(_limit) ⇒ Object
Override marshal_dump to avoid Marshalizing filter procs
68 69 70 |
# File 'lib/r18n-core/translated_string.rb', line 68 def _dump(_limit) [@locale.code, @path, to_str].join(':') end |
#as_json(_options = nil) ⇒ Object
Define ‘as_json` for ActiveSupport compatibility.
63 64 65 |
# File 'lib/r18n-core/translated_string.rb', line 63 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.
80 81 82 83 |
# File 'lib/r18n-core/translated_string.rb', line 80 def get_untranslated(key) translated = @path.empty? ? '' : "#{@path}." Untranslated.new(translated, key, @locale, @filters) end |
#html_safe? ⇒ Boolean
Return true if ‘html_safe` method is defined, otherwise false.
49 50 51 |
# File 'lib/r18n-core/translated_string.rb', line 49 def html_safe? respond_to? :html_safe end |
#to_s ⇒ Object
Override to_s to make string html safe if ‘html_safe` method is defined.
54 55 56 57 58 59 60 |
# File 'lib/r18n-core/translated_string.rb', line 54 def to_s if respond_to? :html_safe super.html_safe else String.new(super) end end |
#translated? ⇒ Boolean
Return true for translated strings.
44 45 46 |
# File 'lib/r18n-core/translated_string.rb', line 44 def translated? true end |
#|(_other) ⇒ Object
Return self for translated string.
39 40 41 |
# File 'lib/r18n-core/translated_string.rb', line 39 def |(_other) self end |