Skip to content

Commit

Permalink
feat: add setTextdomain
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Apr 12, 2023
1 parent d9074f0 commit 24e3962
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class RockFrontend extends WireData implements Module, ConfigurableModule

private $scripts;
private $styles;
private $textdomain;

/** @var array */
private $translations = [];
Expand Down Expand Up @@ -1840,15 +1841,28 @@ public function _n($textsingular, $textplural, $count)
return \ProcessWire\_n($textsingular, $textplural, $count, $this->textdomain());
}

public function setTextdomain($file = false)
{
$this->textdomain = $file;
}

/**
* Method to find the correct textdomain file for translations in latte files
*/
public function textdomain()
{
$trace = Debug::backtrace();
if ($this->textdomain) return $this->textdomain;
foreach ($trace as $item) {
$call = $item['call'];
// renderFile[Latte|Twig]

// Translations in RockPageBuilder
$match = false;
if (strpos($item['file'], "/modules/RockPageBuilder/Block.php")) $match = true;
elseif (strpos($call, '$rockfrontend->renderFile') === 0) $match = true;
if (!$match) continue;

if (strpos($call, '$rockfrontend->renderFile') !== 0) continue;
preg_match("/(.*)\"(.*)\"(.*)/", $call, $matches);
// path to file that was rendered (eg main.latte)
Expand Down
4 changes: 4 additions & 0 deletions docs/template-engines/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ These are the three versions that you can use to translate strings in your LATTE

Note that when using the function-syntax you must prepend the function call with an equal sign! While the translation will - in theory - also work without the equal sign you will not be able to translate the string in the backend, because the regex will not find it!

### setTextdomain

TBD

## Twig

Twig is not shipped with RockFrontend by default, but can easily be added via composer in the PW root directory:
Expand Down

0 comments on commit 24e3962

Please sign in to comment.