mirror of
https://github.com/phpbb/phpbb.git
synced 2025-02-24 12:03:21 +01:00
[feature/twig] Transform {L_, {LA_ to use the lang() function
PHPBB3-11598
This commit is contained in:
parent
658d1b6afe
commit
9749405129
@ -17,11 +17,24 @@ if (!defined('IN_PHPBB'))
|
||||
|
||||
class phpbb_template_twig_extension extends Twig_Extension
|
||||
{
|
||||
/** @var phpbb_user */
|
||||
protected $user;
|
||||
|
||||
public function __construct($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'phpbb';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token parser instance to add to the existing list.
|
||||
*
|
||||
* @return array An array of Twig_TokenParser instances
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(
|
||||
@ -35,6 +48,11 @@ class phpbb_template_twig_extension extends Twig_Extension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of filters to add to the existing list.
|
||||
*
|
||||
* @return array An array of filters
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
@ -42,6 +60,23 @@ class phpbb_template_twig_extension extends Twig_Extension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of global functions to add to the existing list.
|
||||
*
|
||||
* @return array An array of global functions
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new Twig_SimpleFunction('lang', array($this->user, 'lang')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of operators to add to the existing list.
|
||||
*
|
||||
* @return array An array of operators
|
||||
*/
|
||||
public function getOperators()
|
||||
{
|
||||
return array(
|
||||
|
@ -59,6 +59,12 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
|
||||
$code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
|
||||
|
||||
// Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }}
|
||||
$code = preg_replace('#{L_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\') }}', $code);
|
||||
|
||||
// Replace all of our JS escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|escape('js') }}
|
||||
$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)}#', '{{ lang(\'$1\')|escape(\'js\') }}', $code);
|
||||
|
||||
// Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}
|
||||
$code = preg_replace('#{([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code);
|
||||
|
||||
|
@ -131,7 +131,11 @@ class phpbb_template_twig implements phpbb_template
|
||||
// @todo remove
|
||||
$this->clear_cache();
|
||||
|
||||
$this->twig->addExtension(new phpbb_template_twig_extension);
|
||||
$this->twig->addExtension(
|
||||
new phpbb_template_twig_extension(
|
||||
$this->user
|
||||
)
|
||||
);
|
||||
|
||||
$lexer = new phpbb_template_twig_lexer($this->twig);
|
||||
|
||||
@ -430,27 +434,13 @@ class phpbb_template_twig implements phpbb_template
|
||||
{
|
||||
$vars = array();
|
||||
|
||||
// Work-around for now
|
||||
if (!empty($this->user->lang))
|
||||
{
|
||||
foreach ($this->user->lang as $key => $value)
|
||||
{
|
||||
if (!is_string($value))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$vars['L_' . strtoupper($key)] = $value;
|
||||
$vars['LA_' . strtoupper($key)] = addslashes($value);
|
||||
}
|
||||
}
|
||||
|
||||
$vars = array_merge(
|
||||
$vars,
|
||||
$this->context->get_rootref(),
|
||||
$this->context->get_tpldata(),
|
||||
array(
|
||||
'definition' => new phpbb_template_twig_definition(),
|
||||
'user' => $this->user,
|
||||
)
|
||||
);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user