Class: Mullet::HTML::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mullet/html/message.rb

Overview

Holds the resource key and variable names that need to be resolved to format a localized message.

Constant Summary collapse

ARGUMENT_SEPARATOR =
','

Instance Method Summary collapse

Constructor Details

#initialize(message_arguments) ⇒ Message

Constructor



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mullet/html/message.rb', line 15

def initialize(message_arguments)
  arguments = message_arguments.split(ARGUMENT_SEPARATOR)
  if arguments.empty?()
    raise TemplateException.new(
        "incorrect syntax in message #{message_arguments}")
  end

  @message_key = arguments.shift().strip()
  if @message_key.empty?()
    raise TemplateException.new(
        "empty message key in message #{message_arguments}")
  end

  @argument_keys = []
  arguments.each do |argument_key|
    argument_key = argument_key.strip()
    if argument_key.empty?()
      raise TemplateException.new(
          "empty argument key in message #{message_arguments}")
    end
    @argument_keys << argument_key.to_sym()
  end
end

Instance Method Details

#translate(render_context) ⇒ Object

Formats localized message.



44
45
46
47
48
49
50
51
52
# File 'lib/mullet/html/message.rb', line 44

def translate(render_context)
  arguments = Hash.new()
  @argument_keys.each do |argument_key|
    arguments.store(
        argument_key, render_context.get_display_value(argument_key))
  end

  return I18n.translate(@message_key, arguments)
end