mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-29 14:48:53 +00:00
[feature/twig] Transform {L_, {LA_ to use the lang() function
PHPBB3-11598
This commit is contained in:
parent
658d1b6afe
commit
9749405129
3 changed files with 47 additions and 16 deletions
|
@ -17,11 +17,24 @@ if (!defined('IN_PHPBB'))
|
||||||
|
|
||||||
class phpbb_template_twig_extension extends Twig_Extension
|
class phpbb_template_twig_extension extends Twig_Extension
|
||||||
{
|
{
|
||||||
|
/** @var phpbb_user */
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
public function __construct($user)
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'phpbb';
|
return 'phpbb';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the token parser instance to add to the existing list.
|
||||||
|
*
|
||||||
|
* @return array An array of Twig_TokenParser instances
|
||||||
|
*/
|
||||||
public function getTokenParsers()
|
public function getTokenParsers()
|
||||||
{
|
{
|
||||||
return array(
|
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()
|
public function getFilters()
|
||||||
{
|
{
|
||||||
return array(
|
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()
|
public function getOperators()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
|
|
@ -59,6 +59,12 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
||||||
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
|
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
|
||||||
$code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
|
$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 }}
|
// Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}
|
||||||
$code = preg_replace('#{([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code);
|
$code = preg_replace('#{([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code);
|
||||||
|
|
||||||
|
|
|
@ -131,7 +131,11 @@ class phpbb_template_twig implements phpbb_template
|
||||||
// @todo remove
|
// @todo remove
|
||||||
$this->clear_cache();
|
$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);
|
$lexer = new phpbb_template_twig_lexer($this->twig);
|
||||||
|
|
||||||
|
@ -430,27 +434,13 @@ class phpbb_template_twig implements phpbb_template
|
||||||
{
|
{
|
||||||
$vars = array();
|
$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 = array_merge(
|
||||||
$vars,
|
$vars,
|
||||||
$this->context->get_rootref(),
|
$this->context->get_rootref(),
|
||||||
$this->context->get_tpldata(),
|
$this->context->get_tpldata(),
|
||||||
array(
|
array(
|
||||||
'definition' => new phpbb_template_twig_definition(),
|
'definition' => new phpbb_template_twig_definition(),
|
||||||
|
'user' => $this->user,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue