Files
Smarty-Extended/test/plugins/block.t.php
Clemens Schwaighofer 077b9f28ad Test plugins as loaded modifier, block t update
Update old block t data to match new block t class type

Test if we can load plugin parts, this is a side test for if we want
to not add them to the main Smarty load class

Remove old test source code, no longer needed
2024-08-20 10:56:59 +09:00

218 lines
5.2 KiB
PHP

<?php
/**
* smarty-gettext.php - Gettext support for smarty
*
* To register as a smarty block function named 't', use:
* $smarty->register_block('t', 'smarty_translate');
*
* NOTE: native php support for conext sensitive does not exist
* Those jumps are disabled
*
* @package smarty-gettext
* @version $Id: block.t.php 4738 2022-05-06 01:28:48Z clemens $
* @link http://smarty-gettext.sf.net/
* @author Sagi Bashari <sagi@boom.org.il>
* @copyright 2004 Sagi Bashari
* @copyright Elan Ruusamäe
* @copyright 2024 Clemens Schwaighofer
*/
/**
* Replaces arguments in a string with their values.
* Arguments are represented by % followed by their number.
*
* @param string $str Source string
* @param mixed mixed Arguments, can be passed in an array or through single variables.
* @return string Modified string
*/
function smarty_gettext_strarg($str/*, $varargs... */)
{
$tr = [];
$p = 0;
$nargs = func_num_args();
for ($i = 1; $i < $nargs; $i++) {
$arg = func_get_arg($i);
if (is_array($arg)) {
foreach ($arg as $aarg) {
$tr['%' . ++$p] = $aarg;
}
} else {
$tr['%' . ++$p] = $arg;
}
}
return strtr($str, $tr);
}
/**
* raise a notice for missing callable, indicates L10n functions where not loaded
*
* @param string $function
* @return void
*/
function reportL10nNotInitialized(string $function): void
{
trigger_error("Missing " . $function . ", L10n::loadFunctions() called?", E_NOTICE);
}
/**
* Smarty block function, provides gettext support for smarty.
*
* The block content is the text that should be translated.
*
* Any parameter that is sent to the function will be represented as %n in the translation text,
* where n is 1 for the first parameter. The following parameters are reserved:
* - escape - sets escape mode:
* - 'html' for HTML escaping, this is the default.
* - 'js' for javascript escaping.
* - 'url' for url escaping.
* - 'no'/'off'/0 - turns off escaping
* - plural - The plural version of the text (2nd parameter of ngettext())
* - count - The item count for plural mode (3rd parameter of ngettext())
* - domain - Textdomain to be used, default if skipped (dgettext() instead of gettext())
* - context - gettext context. reserved for future use.
*
* @param array<string,int|string> $params
* @param string $text
* @return string
*/
function smarty_block_t($params, $text)
{
if (!isset($text)) {
return $text;
}
$escape = 'html';
$plural = null;
$count = null;
$domain = null;
$context = null;
foreach ($params as $_key => $_value) {
switch ($_key) {
// set escape mode, default html escape
case 'escape':
$escape = (string)$_value;
break;
// set plural parameters 'plural' and 'count'.
case 'plural':
$plural = $_value;
break;
// set count, only for plural, else ignored
case 'count':
$count = $_value;
break;
// get domain param
case 'domain':
$domain = (string)$_value;
break;
// get context param
case 'context':
$context = (string)$_value;
break;
}
}
// use plural if required parameters are set
if (isset($count) && isset($plural)) {
if (isset($domain) && isset($context)) {
if (is_callable('_dnpgettext')) {
$text = _dnpgettext($domain, $context, $text, $plural, $count);
} else {
reportL10nNotInitialized('_dnpgettext');
}
} elseif (isset($domain)) {
if (is_callable('_dngettext')) {
$text = _dngettext($domain, $text, $plural, $count);
} else {
reportL10nNotInitialized('_dngettext');
}
} elseif (isset($context)) {
if (is_callable('_npgettext')) {
$text = _npgettext($context, $text, $plural, $count);
} else {
reportL10nNotInitialized('_npgettext');
}
} else {
if (is_callable('_ngettext')) {
$text = _ngettext($text, $plural, $count);
} else {
reportL10nNotInitialized('_ngettext');
}
}
} else { // use normal
if (isset($domain) && isset($context)) {
if (is_callable('_dpgettext')) {
$text = _dpgettext($domain, $context, $text);
} else {
reportL10nNotInitialized('_dpgettext');
}
} elseif (isset($domain)) {
if (is_callable('_dgettext')) {
$text = _dgettext($domain, $text);
} else {
reportL10nNotInitialized('_dgettext');
}
} elseif (isset($context)) {
if (is_callable('_pgettext')) {
$text = _pgettext($context, $text);
} else {
reportL10nNotInitialized('_pgettext');
}
} else {
if (is_callable('_gettext')) {
$text = _gettext($text);
} else {
reportL10nNotInitialized('_gettext');
}
}
}
// run strarg if there are parameters
if (count($params)) {
$text = smarty_gettext_strarg($text, $params);
}
switch ($escape) {
case 'html':
// default
$text = nl2br(htmlspecialchars($text));
break;
case 'javascript':
case 'js':
// javascript escape
$text = strtr(
$text,
[
'\\' => '\\\\',
"'" => "\\'",
'"' => '\\"',
"\r" => '\\r',
"\n" => '\\n',
'</' => '<\/'
]
);
break;
case 'url':
// url escape
$text = urlencode($text);
break;
// below is a list for explicit OFF
case 'no':
case 'off':
case 'false':
case '0':
case 0:
// explicit OFF
default:
break;
}
return $text;
}
// __END__