mvc: fix lang= hint in HTML; closes #7336

This commit is contained in:
Franco Fichtner 2024-03-18 11:29:35 +01:00
parent df5680b123
commit 6de05e0437
6 changed files with 33 additions and 16 deletions

View File

@ -31,8 +31,8 @@ $di->set('url', function () use ($config) {
* Setting up the view component
*/
$di->set('view', function () use ($config) {
$view = new View();
// if configuration defines more view locations, convert phalcon config items to array
if (is_string($config->application->viewsDir)) {
$view->setViewsDir($config->application->viewsDir);
@ -43,24 +43,26 @@ $di->set('view', function () use ($config) {
}
$view->setViewsDir($viewDirs);
}
$view->registerEngines(array(
'.volt' => function ($view) use ($config) {
$view->registerEngines([
'.volt' => function ($view) use ($config) {
$volt = new VoltEngine($view, $this);
$volt->setOptions(array(
$volt->setOptions([
'path' => $config->application->cacheDir,
'separator' => '_'
));
// register additional volt template functions
]);
// register additional volt template functions and filters
$volt->getCompiler()->addFunction('theme_file_or_default', 'view_fetch_themed_filename');
$volt->getCompiler()->addFunction('file_exists', 'view_file_exists');
$volt->getCompiler()->addFunction('cache_safe', 'view_cache_safe');
$volt->getCompiler()->addFilter('safe', 'view_html_safe');
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
]);
return $view;
}, true);
@ -96,8 +98,6 @@ $di->setShared('session', function () {
return $session;
});
/**
* Setup router
*/

View File

@ -230,6 +230,8 @@ class ControllerBase extends ControllerRoot
$cnf = Config::getInstance();
$this->view->setVar('lang', $this->translator);
$this->view->setVar('langcode', str_replace('_', '-', $this->langcode));
$rewrite_uri = explode("?", $_SERVER["REQUEST_URI"])[0];
$this->view->menuSystem = $menu->getItems($rewrite_uri);
/* XXX generating breadcrumbs requires getItems() call */

View File

@ -45,7 +45,6 @@ class ControllerRoot extends Controller
*/
public $translator;
/**
* log handle
*/
@ -56,6 +55,11 @@ class ControllerRoot extends Controller
*/
protected $logged_in_user = null;
/**
* current language code
*/
protected $langcode = 'en_US';
/**
* Wrap close session, for long running operations.
*/
@ -70,7 +74,7 @@ class ControllerRoot extends Controller
protected function setLang()
{
$config = Config::getInstance()->object();
$lang = 'en_US';
$lang = $this->langcode;
foreach ($config->system->children() as $key => $node) {
if ($key == 'language') {
@ -100,6 +104,8 @@ class ControllerRoot extends Controller
/* somehow this is not done by Phalcon */
bind_textdomain_codeset('OPNsense', $locale);
putenv('LANG=' . $locale);
$this->langcode = $lang;
}
/**

View File

@ -38,8 +38,6 @@ class ViewTranslator extends Gettext
{
public function _($translateKey, array $placeholders = []): string
{
$translateValue = parent::_($translateKey, $placeholders);
/* gettext() embedded in JavaScript can cause syntax errors */
return str_replace("\n", '
', htmlspecialchars($translateValue ?? '', ENT_QUOTES | ENT_HTML401));
return view_html_safe(parent::_($translateKey, $placeholders));
}
}

View File

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en-US" class="no-js">
<html lang="{{ langcode|safe }}" class="no-js">
<head>
<meta charset="UTF-8" />

View File

@ -46,6 +46,17 @@ function view_cache_safe($url)
return $url;
}
/**
* return safe HTML encoded version of input string
* @param string $text to make HTML safe
* @return string
*/
function view_html_safe($text)
{
/* gettext() embedded in JavaScript can cause syntax errors */
return str_replace("\n", '&#10;', htmlspecialchars($text ?? '', ENT_QUOTES | ENT_HTML401));
}
try {
/**
* Read the configuration