Class: R18n::Untranslated

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

Overview

Return if translation isn’t exists. Unlike nil, it didn’t raise error when you try to access for subtranslations.

You can set format to print untranslated string by filters. For example: Disable standart output:

R18n::Filters.off(:untranslated)

For development environment:

R18n::Filters.add(R18n::Untranslated, :untranslated_html) do
  |content, config, translated_path, untranslated_path, path|
  "#{translated_path}<span style='color: red'>#{untranslated_path}</span>"
end

For production environment:

R18n::Filters.add(R18n::Untranslated, :hide_untranslated) { '' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(translated_path, untranslated_path, locale, filters) ⇒ Untranslated

Returns a new instance of Untranslated.



50
51
52
53
54
55
# File 'lib/r18n-core/untranslated.rb', line 50

def initialize(translated_path, untranslated_path, locale, filters)
  @translated_path   = translated_path
  @untranslated_path = untranslated_path
  @locale            = locale
  @filters           = filters
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *params) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/r18n-core/untranslated.rb', line 77

def method_missing(name, *params)
  # It is need to fix some hack in specs
  if name == :to_ary
    raise NoMethodError, "undefined method `to_ary' for #{to_s}"
  end

  self[name]
end

Instance Attribute Details

#localeObject (readonly)

Main locale, where string was try to find



48
49
50
# File 'lib/r18n-core/untranslated.rb', line 48

def locale
  @locale
end

#translated_pathObject (readonly)

Path, that exists in translation.



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

def translated_path
  @translated_path
end

#untranslated_pathObject (readonly)

Path, that isn’t in translation.



42
43
44
# File 'lib/r18n-core/untranslated.rb', line 42

def untranslated_path
  @untranslated_path
end

Class Method Details

._load(str) ⇒ Object

Load object from Marshalizing.



72
73
74
75
# File 'lib/r18n-core/untranslated.rb', line 72

def self._load(str)
  arr = str.split(":", 3)
  new arr[1], arr[2], R18n.locale(arr[0]), GlobalFilterList.instance
end

Instance Method Details

#==(untrsl) ⇒ Object

Is another locale has same code.



103
104
105
106
107
108
109
# File 'lib/r18n-core/untranslated.rb', line 103

def ==(untrsl)
  return false unless untrsl.is_a? Untranslated
  return false unless locale            == untrsl.locale
  return false unless translated_path   == untrsl.translated_path
  return false unless untranslated_path == untrsl.untranslated_path
  true
end

#[](*params) ⇒ Object



86
87
88
89
# File 'lib/r18n-core/untranslated.rb', line 86

def [](*params)
  Untranslated.new(translated_path, "#{@untranslated_path}.#{params.first}",
                   @locale, @filters)
end

#_dump(limit) ⇒ Object

Override marshal_dump to avoid Marshalizing filter procs



67
68
69
# File 'lib/r18n-core/untranslated.rb', line 67

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

#pathObject

Path to translation.



58
59
60
# File 'lib/r18n-core/untranslated.rb', line 58

def path
  "#{@translated_path}#{@untranslated_path}"
end

#to_sObject Also known as: to_str



95
96
97
98
# File 'lib/r18n-core/untranslated.rb', line 95

def to_s
  @filters.process(:all, Untranslated, path, @locale, path,
                  [@translated_path, @untranslated_path, path])
end

#translated?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/r18n-core/untranslated.rb', line 62

def translated?
  false
end

#|(default) ⇒ Object



91
92
93
# File 'lib/r18n-core/untranslated.rb', line 91

def |(default)
  default
end