/** * Get a message from the message blob * * @param $key string Message key to retrieve a message for. * @param $options mixed (optional) A textdomain-name or an array with one or more * of the following options: * - domain: overrides the currently selected textdomain, and if needed loads it from disk * - lang: overrides the currently selected language * - variables: numerical array to do variable replacements ($1> var[0], $2> var[1], etc.) * - raw-variables: boolean to determine whether the variables should be escaped as well * - parsemag: boolean to determine whether the message sould be tranformed * using magic phrases (PLURAL, etc.) * - escape: Optionally the return can be escaped. By default this takes place after variable * replacement. Set 'raw-variables' to true if you just want the raw message * to be escaped and have escaped the variables already. * - * 'plain' * - * 'html' (<>"& escaped) * - * 'htmlspecialchars' (alias of 'html') * - * 'htmlentities' (foreign/UTF-8 chars converted as well) * * @param $fail string Alternate value to return in case the message doesn't exist */ public function msg($key = 0, $options = array(), $fail = null) { // Make sure a proper key was passed. if (!TsIntuitionUtil::nonEmptyStr($key)) { return $this->bracketMsg($key, $fail); } $defaultOptions = array('domain' => $this->getDomain(), 'lang' => $this->getLang(), 'variables' => array(), 'raw-variables' => false, 'escape' => 'plain', 'parsemag' => true, 'externallinks' => false, 'wikilinks' => false); // If $options was a domain string, convert it now. if (TsIntuitionUtil::nonEmptyStr($options)) { $options = array('domain' => $options); } // If $options is still not an array, ignore it and use default // Otherwise merge the options with the defaults. if (!is_array($options)) { $options = $defaultOptions; } else { $options = array_merge($defaultOptions, $options); } // First character of the message-key is case-insensitive. $key = lcfirst($key); // Load if not already loaded $domain = $this->loadTextdomain($options['domain']); $lang = $options['lang']; // In case the domain name was invalid or inexistant if (!isset($this->messageBlob[$domain])) { return $this->bracketMsg($key, $fail); } // Just in case, one last check: $rawMsg = $this->rawMsg($domain, $lang, $key); if (is_null($rawMsg)) { $this->errTrigger("Message \"{$key}\" in domain \"{$domain}\" not found", __METHOD__, E_NOTICE); // Fall back to a simple [keyname] return $this->bracketMsg($key, $fail); } /* Now that we've got the message, do post-processing. */ $msg = $rawMsg; $escapeDone = false; // Escape now or do it after variable replacement ? if ($options['raw-variables'] === true) { $msg = TsIntuitionUtil::strEscape($msg, $options['escape']); $escapeDone = true; } // Variable replacements foreach ($options['variables'] as $i => $val) { $n = $i + 1; $msg = str_replace("\${$n}", $val, $msg); } // Some parsing work if ($options['parsemag'] === true) { $msg = $this->getMessagesFunctions()->parse($msg, $lang); } // If not already escaped, do it now if (!$escapeDone) { $escapeDone = true; $msg = TsIntuitionUtil::strEscape($msg, $options['escape']); } if (is_string($options['wikilinks'])) { $msg = TsIntuitionUtil::parseWikiLinks($msg, $options['wikilinks']); } if ($options['externallinks']) { $msg = TsIntuitionUtil::parseExternalLinks($msg); } // Finally return $msg; }