first commit
This commit is contained in:
126
class/smarty_plugins/compiler.t.php
Normal file
126
class/smarty_plugins/compiler.t.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* {t}{/t} compile tags for Smarty WITHOUT translations.
|
||||
*
|
||||
* Examples:
|
||||
* {t}Test{/t}
|
||||
* {t promenna="prekladovy"}Testovaci {promenna} text{/t} => vygeneruje "Testovaci prekladovy text"
|
||||
* {t escape=false}<b>test</b>{/t} => povolit HTML tagy ve vystupu
|
||||
* {t count=$pocet plural="Products"}Produkt{/t} => "Produkt" pokud count=1, Produkty pokud 1 < count < 5, Produktů pokud count > 5
|
||||
*/
|
||||
class Smarty_Compiler_T extends Smarty_Internal_CompileBase
|
||||
{
|
||||
public $optional_attributes = ['_any'];
|
||||
|
||||
/**
|
||||
* @var Smarty_Internal_SmartyTemplateCompiler
|
||||
*/
|
||||
private $compiler;
|
||||
|
||||
/**
|
||||
* @param $compiler Smarty_Internal_SmartyTemplateCompiler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function compile($args, $compiler)
|
||||
{
|
||||
$_args = $this->getAttributes($compiler, $args);
|
||||
$this->compiler = $compiler;
|
||||
|
||||
$lexer = $compiler->parser->lex;
|
||||
|
||||
if ($lexer->value == '}') {
|
||||
$lexer->yylex();
|
||||
}
|
||||
|
||||
$term = '';
|
||||
do {
|
||||
$term .= $lexer->value;
|
||||
$lexer->yylex();
|
||||
} while ($lexer->value != '{/t}');
|
||||
|
||||
unset($_args['1']);
|
||||
|
||||
return $this->translate($term, $_args);
|
||||
}
|
||||
|
||||
public function export_params($params)
|
||||
{
|
||||
return '['.
|
||||
join(',',
|
||||
array_map(
|
||||
function ($name) use ($params) {
|
||||
return "'{$name}' => {$params[$name]}";
|
||||
},
|
||||
array_diff(array_keys($params), ['nocache', 'domain', 'count', 'plural', 'plural5', 'escape'])
|
||||
)
|
||||
)
|
||||
.']';
|
||||
}
|
||||
|
||||
public function translate($text, $params)
|
||||
{
|
||||
if (empty($text)) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'escape' => 'html',
|
||||
'domain' => null,
|
||||
];
|
||||
|
||||
$params = array_merge($defaults, $params);
|
||||
|
||||
switch ($params['escape']) {
|
||||
case 'html':
|
||||
$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;
|
||||
}
|
||||
|
||||
// Can do partial static translation if parameters are present
|
||||
$hasPlural = isset($params['count']) || isset($params['plural']);
|
||||
$hasParams = array_diff(array_keys($params), ['nocache', 'domain', 'escape']);
|
||||
if ($hasPlural || $hasParams) {
|
||||
// Do plural form translation
|
||||
if ($hasPlural) {
|
||||
$plural = getVal('plural', $params, $text);
|
||||
$plural5 = getVal('plural5', $params, $plural);
|
||||
$text = str_replace("'", "\\'", $text);
|
||||
$text = "(({$params['count']}) == 1) ? '{$text}' : ((({$params['count']}) > 1 && ({$params['count']}) < 5) ? {$plural} : {$plural5})";
|
||||
} else {
|
||||
$text = "'".str_replace("'", "\\'", $text)."'";
|
||||
}
|
||||
|
||||
if ($hasParams) {
|
||||
$params = $this->export_params($params);
|
||||
$text = "\KupShop\KupShopBundle\Util\StringUtil::replacePlaceholders({$text}, {$params})";
|
||||
}
|
||||
|
||||
return "<?php echo {$text}; ?>";
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
||||
class Smarty_Compiler_Tclose extends Smarty_Internal_CompileBase
|
||||
{
|
||||
public $required_attributes = [];
|
||||
public $optional_attributes = [];
|
||||
public $shorttag_order = [];
|
||||
|
||||
public function compile($args, $compiler)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user