Class: LocaleSchema::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/locale_schema/translation.rb

Constant Summary collapse

HTML_SAFE_LEAF_NAMES =
%w{markdown textile html}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Translation

Returns a new instance of Translation.



7
8
9
10
11
12
# File 'lib/locale_schema/translation.rb', line 7

def initialize(args={})
  @key     = args[:key]
  @options = args[:options] || {}
  @context = args[:context]
  @scope   = args[:scope].split('/') || []
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/locale_schema/translation.rb', line 3

def context
  @context
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/locale_schema/translation.rb', line 3

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/locale_schema/translation.rb', line 3

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/locale_schema/translation.rb', line 3

def scope
  @scope
end

Instance Method Details

#defaultsObject



22
23
24
25
26
27
# File 'lib/locale_schema/translation.rb', line 22

def defaults
  [
    :"#{context}.#{scope.join('.')}.#{key}",
    :"#{context}.application.#{key}"
  ]
end

#html_safe?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/locale_schema/translation.rb', line 18

def html_safe?
  HTML_SAFE_LEAF_NAMES.include?(leaf_name)
end

#leaf_nameObject



14
15
16
# File 'lib/locale_schema/translation.rb', line 14

def leaf_name
  key.to_s.split('.').last
end

#to_sObject



29
30
31
32
33
34
35
36
37
# File 'lib/locale_schema/translation.rb', line 29

def to_s
  s = I18n.t(defaults[0], options.merge(:default => defaults[1..-1]))

  if leaf_name == 'markdown'
    s = ::Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(s)
  end

  s
end