mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-03-23 14:27:02 +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
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
@ -102,15 +102,15 @@ class HTMLPurifier_LanguageFactory
|
|||||||
// you can bypass the conditional include by loading the
|
// you can bypass the conditional include by loading the
|
||||||
// file yourself
|
// file yourself
|
||||||
if (file_exists($file) && !class_exists($class)) {
|
if (file_exists($file) && !class_exists($class)) {
|
||||||
include_once $file;
|
include_once $file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -174,15 +174,15 @@ class HTMLPurifier_LanguageFactory
|
|||||||
|
|
||||||
// merge fallback with current language
|
// merge fallback with current language
|
||||||
foreach ( $this->keys as $key ) {
|
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])) {
|
if (isset($this->mergeable_keys_map[$key])) {
|
||||||
$cache[$key] = $cache[$key] + $fallback_cache[$key];
|
$cache[$key] = $cache[$key] + $fallback_cache[$key];
|
||||||
} elseif (isset($this->mergeable_keys_list[$key])) {
|
} elseif (isset($this->mergeable_keys_list[$key])) {
|
||||||
$cache[$key] = array_merge( $fallback_cache[$key], $cache[$key] );
|
$cache[$key] = array_merge( $fallback_cache[$key], $cache[$key] );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$cache[$key] = $fallback_cache[$key];
|
$cache[$key] = $fallback_cache[$key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user