mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-09 15:28:40 +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:
parent
4bf15de536
commit
7699efd593
@ -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
|
||||
* @return string localised message
|
||||
*/
|
||||
function getMessage($key) {
|
||||
if (!$this->_loaded) $this->load();
|
||||
if (!isset($this->messages[$key])) return '';
|
||||
if (!isset($this->messages[$key])) return "[$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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -108,9 +108,9 @@ class HTMLPurifier_LanguageFactory
|
||||
|
||||
if (!class_exists($class)) {
|
||||
// go fallback
|
||||
$fallback = HTMLPurifier_Language::getFallbackFor($code);
|
||||
$fallback = HTMLPurifier_LanguageFactory::getFallbackFor($code);
|
||||
$depth++;
|
||||
$lang = Language::factory( $fallback );
|
||||
$lang = HTMLPurifier_LanguageFactory::factory( $fallback );
|
||||
$depth--;
|
||||
} else {
|
||||
$lang = new $class;
|
||||
|
@ -7,14 +7,19 @@ class HTMLPurifier_LanguageTest extends UnitTestCase
|
||||
|
||||
var $lang;
|
||||
|
||||
function setup() {
|
||||
$factory = HTMLPurifier_LanguageFactory::instance();
|
||||
$this->lang = $factory->create('en');
|
||||
function test_getMessage() {
|
||||
$lang = new HTMLPurifier_Language();
|
||||
$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() {
|
||||
$this->assertIdentical($this->lang->getMessage('htmlpurifier'), 'HTML Purifier');
|
||||
$this->assertIdentical($this->lang->getMessage('totally-non-existent-key'), '');
|
||||
function test_formatMessage() {
|
||||
$lang = new HTMLPurifier_Language();
|
||||
$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');
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user