Class: R18n::TranslatedString

Inherits:
String
  • Object
show all
Defined in:
lib/r18n-core/translated_string.rb

Overview

String, which is translated to some locale and loading from Translation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, locale, path, filters = nil) ⇒ TranslatedString

Returns a new string object containing a copy of str, which translated for path to locale



32
33
34
35
36
37
# File 'lib/r18n-core/translated_string.rb', line 32

def initialize(str, locale, path, filters = nil)
  super(str)
  @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.



82
83
84
# File 'lib/r18n-core/translated_string.rb', line 82

def method_missing(name, *params)
  get_untranslated(name.to_s)
end

Instance Attribute Details

#localeObject (readonly)

String locale



25
26
27
# File 'lib/r18n-core/translated_string.rb', line 25

def locale
  @locale
end

#pathObject (readonly)

Path for this translation.



28
29
30
# File 'lib/r18n-core/translated_string.rb', line 28

def path
  @path
end

Class Method Details

._load(str) ⇒ Object

Load object from Marshalizing.



69
70
71
72
# File 'lib/r18n-core/translated_string.rb', line 69

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



64
65
66
# File 'lib/r18n-core/translated_string.rb', line 64

def _dump(limit)
  [@locale.code, @path, to_str].join(":")
end

#get_untranslated(key) ⇒ Object

Return untranslated for deeper node ‘key`. It is in separated methods to be used in R18n I18n backend.



76
77
78
79
# File 'lib/r18n-core/translated_string.rb', line 76

def get_untranslated(key)
  translated = @path.empty? ? '' : "#{@path}."
  Untranslated.new(translated, key, @locale, @filters)
end

#html_safe?Boolean

Mark translated string as html safe, because R18n has own escape system.

Returns:

  • (Boolean)


50
51
52
# File 'lib/r18n-core/translated_string.rb', line 50

def html_safe?
  true
end

#to_sObject

Override to_s to make string html safe if ‘html_safe` method is defined.



55
56
57
58
59
60
61
# File 'lib/r18n-core/translated_string.rb', line 55

def to_s
  if respond_to? :html_safe
    html_safe
  else
    String.new(self)
  end
end

#translated?Boolean

Return true for translated strings.

Returns:

  • (Boolean)


45
46
47
# File 'lib/r18n-core/translated_string.rb', line 45

def translated?
  true
end

#|(default) ⇒ Object

Return self for translated string.



40
41
42
# File 'lib/r18n-core/translated_string.rb', line 40

def |(default)
  self
end