Skip to content

Commit

Permalink
feat: add translation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Feb 15, 2023
1 parent 87db485 commit 877ddb9
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function getModuleInfo()
{
return [
'title' => 'RockFrontend',
'version' => '2.22.0',
'version' => '2.23.0',
'summary' => 'Module for easy frontend development',
'autoload' => true,
'singular' => true,
Expand Down Expand Up @@ -207,6 +207,36 @@ public function ready()
$this->addAssets();
}


/**
* Helper function to support translatable strings in latte/twig files
*
* Usage:
* <a href=...>{$rf->_('login')}</a>
* <a href=...>{$rf->_('logout')}</a>
*
* See https://github.com/processwire/processwire-requests/issues/480
*/
public function _($str)
{
$trace = Debug::backtrace();
foreach ($trace as $item) {
$call = $item['call'];
// renderFile[Latte|Twig]
if (strpos($call, '$rockfrontend->renderFile') !== 0) continue;
preg_match("/(.*)\"(.*)\"(.*)/", $call, $matches);
// path to file that was rendered (eg main.latte)
$path = $matches[2];
break;
}
$url = str_replace(
$this->wire->config->paths->root,
$this->wire->config->urls->root,
$path
);
return \ProcessWire\__($str, $url);
}

/**
* Add assets to the html markup
* @return void
Expand Down

0 comments on commit 877ddb9

Please sign in to comment.