0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 10:45:18 +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
* @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);
}
}
?>

View File

@ -102,15 +102,15 @@ class HTMLPurifier_LanguageFactory
// you can bypass the conditional include by loading the
// file yourself
if (file_exists($file) && !class_exists($class)) {
include_once $file;
}
include_once $file;
}
}
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;
@ -174,15 +174,15 @@ class HTMLPurifier_LanguageFactory
// merge fallback with current language
foreach ( $this->keys as $key ) {
if (isset($cache[$key]) && isset($fallback_cache[$key])) {
if (isset($cache[$key]) && isset($fallback_cache[$key])) {
if (isset($this->mergeable_keys_map[$key])) {
$cache[$key] = $cache[$key] + $fallback_cache[$key];
} elseif (isset($this->mergeable_keys_list[$key])) {
$cache[$key] = array_merge( $fallback_cache[$key], $cache[$key] );
}
} else {
$cache[$key] = $fallback_cache[$key];
}
} else {
$cache[$key] = $fallback_cache[$key];
}
}
}

View File

@ -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');
}
}