/** * Get a message from the message blob. * * @param string $key Message key to retrieve a message for. * @param string|array $options [optional] A domain name or an array with one or more * of the following options: * - domain: overrides the currently selected domain, 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 mixed $fail [optional] Value if the message doesn't exist. Defaults to null. */ public function msg($key = 0, $options = array(), $fail = null) { if (!IntuitionUtil::nonEmptyStr($key)) { // Invalid message 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 (IntuitionUtil::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); } // Normalise key. First character is case-insensitive. $key = lcfirst($key); $msg = $this->rawMsg($options['domain'], $options['lang'], $key); if ($msg === null) { $this->errTrigger("Message \"{$key}\" for lang \"{$options['lang']}\" in domain \"{$options['domain']}\" not found", __METHOD__, E_NOTICE); return $this->bracketMsg($key, $fail); } // Now that we've got the message, apply any post processing $escapeDone = false; // If using raw variables, escape message before replacement if ($options['raw-variables'] === true) { $msg = IntuitionUtil::strEscape($msg, $options['escape']); $escapeDone = true; } // Replace variables foreach ($options['variables'] as $i => $val) { $n = $i + 1; $msg = str_replace("\${$n}", $val, $msg); } if ($options['parsemag'] === true) { $msg = $this->getMessagesFunctions()->parse($msg, $options['lang']); } // If not using raw vars, escape the message now (after variable replacement). if (!$escapeDone) { $escapeDone = true; $msg = IntuitionUtil::strEscape($msg, $options['escape']); } if (is_string($options['wikilinks'])) { $msg = IntuitionUtil::parseWikiLinks($msg, $options['wikilinks']); } if ($options['externallinks']) { $msg = IntuitionUtil::parseExternalLinks($msg); } return $msg; }
/** * 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 (!IntuitionUtil::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 (IntuitionUtil::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 = IntuitionUtil::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 = IntuitionUtil::strEscape($msg, $options['escape']); } if (is_string($options['wikilinks'])) { $msg = IntuitionUtil::parseWikiLinks($msg, $options['wikilinks']); } if ($options['externallinks']) { $msg = IntuitionUtil::parseExternalLinks($msg); } // Finally return $msg; }