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(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

#localeObject (readonly)

String locale



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

def locale
  @locale
end

#pathObject (readonly)

Path for this translation.



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

def path
  @path
end

Class Method Details

._load(str) ⇒ Object

Load object from Marshalizing.



73
74
75
76
# File 'lib/r18n-core/translated_string.rb', line 73

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



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(_options = 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.

Returns:

  • (Boolean)


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

def html_safe?
  respond_to? :html_safe
end

#to_sObject

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.

Returns:

  • (Boolean)


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