0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-05 14:11:52 +00:00

Implement bare minimum extra functions for language implementation.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1156 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-18 02:25:27 +00:00
parent 4bf15de536
commit 7699efd593
3 changed files with 39 additions and 16 deletions

View File

@ -43,16 +43,34 @@ class HTMLPurifier_Language
} }
/** /**
* Retrieves a localised message. Does not perform any operations. * Retrieves a localised message.
* @param $key string identifier of message * @param $key string identifier of message
* @return string localised message * @return string localised message
*/ */
function getMessage($key) { function getMessage($key) {
if (!$this->_loaded) $this->load(); if (!$this->_loaded) $this->load();
if (!isset($this->messages[$key])) return ''; if (!isset($this->messages[$key])) return "[$key]";
return $this->messages[$key]; return $this->messages[$key];
} }
/**
* Formats a localised message with passed parameters
* @param $key string identifier of message
* @param $param Parameter to substitute in (arbitrary number)
* @return string localised message
*/
function formatMessage($key) {
if (!$this->_loaded) $this->load();
if (!isset($this->messages[$key])) return "[$key]";
$raw = $this->messages[$key];
$args = func_get_args();
$substitutions = array();
for ($i = 1; $i < count($args); $i++) {
$substitutions['$' . $i] = $args[$i];
}
return strtr($raw, $substitutions);
}
} }
?> ?>

View File

@ -108,9 +108,9 @@ class HTMLPurifier_LanguageFactory
if (!class_exists($class)) { if (!class_exists($class)) {
// go fallback // go fallback
$fallback = HTMLPurifier_Language::getFallbackFor($code); $fallback = HTMLPurifier_LanguageFactory::getFallbackFor($code);
$depth++; $depth++;
$lang = Language::factory( $fallback ); $lang = HTMLPurifier_LanguageFactory::factory( $fallback );
$depth--; $depth--;
} else { } else {
$lang = new $class; $lang = new $class;

View File

@ -7,14 +7,19 @@ class HTMLPurifier_LanguageTest extends UnitTestCase
var $lang; var $lang;
function setup() { function test_getMessage() {
$factory = HTMLPurifier_LanguageFactory::instance(); $lang = new HTMLPurifier_Language();
$this->lang = $factory->create('en'); $lang->_loaded = true;
$lang->messages['htmlpurifier'] = 'HTML Purifier';
$this->assertIdentical($lang->getMessage('htmlpurifier'), 'HTML Purifier');
$this->assertIdentical($lang->getMessage('totally-non-existent-key'), '[totally-non-existent-key]');
} }
function test_getMessage() { function test_formatMessage() {
$this->assertIdentical($this->lang->getMessage('htmlpurifier'), 'HTML Purifier'); $lang = new HTMLPurifier_Language();
$this->assertIdentical($this->lang->getMessage('totally-non-existent-key'), ''); $lang->_loaded = true;
$lang->messages['error'] = 'Error is $1 on line $2';
$this->assertIdentical($lang->formatMessage('error', 'fatal', 32), 'Error is fatal on line 32');
} }
} }