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

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.



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

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

Instance Method Details

#get_untranslated(key) ⇒ Object

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



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

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