protected function formatMessages($messages, $format, $type = '')
    {

        if ($format === self::Format_Array) {
            $result = $messages;
        } else {
            $result = array();
            foreach ($messages as $message) {
                if (is_array($message)) {
                    $messagePart = "{$message['code']}: ";
                    $messagePart .= $this->t($message['message']);
                    if ($message['codetag']) {
                        $messagePart .= " ({$message['codetag']})";
                    }
                    $message = $this->t($type) . ' ' . $messagePart;
                }
                if ($format === self::Format_FormattedText) {
                    $message = "* $message\n\n";
                } elseif ($format === self::Format_Html) {
                    $message = "<li>" . nl2br(htmlspecialchars($message, ENT_NOQUOTES)) . "</li>\n";
                }
                $result[] = $message;
            }
            if ($format === self::Format_FormattedText) {
                $result = implode('', $result);
            } elseif ($format === self::Format_Html) {
                $result = "<ul>\n" . implode('', $result) . "</ul>\n";
            }
        }
        return $result;
    }