Skip to content

Commit

Permalink
feat: improve translation code #20 by Jens
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 12, 2023
1 parent 0d38b49 commit d11708a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
45 changes: 14 additions & 31 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2242,17 +2242,23 @@ public function vscodeLink($path)

public function _($str)
{
return \ProcessWire\__($str, $this->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = self::textdomain($backtrace[0]["file"]);
return \ProcessWire\__($str, $textdomain);
}

public function _x($str, $context)
{
return \ProcessWire\_x($str, $context, $this->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = self::textdomain($backtrace[0]["file"]);
return \ProcessWire\_x($str, $context, $textdomain);
}

public function _n($textsingular, $textplural, $count)
{
return \ProcessWire\_n($textsingular, $textplural, $count, $this->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = self::textdomain($backtrace[0]["file"]);
return \ProcessWire\_n($textsingular, $textplural, $count, $textdomain);
}

public function setTextdomain($file = false)
Expand All @@ -2263,35 +2269,12 @@ public function setTextdomain($file = false)
/**
* Method to find the correct textdomain file for translations in latte files
*/
public function textdomain()
public static function textdomain($file)
{
$trace = Debug::backtrace();
if ($this->textdomain) return $this->textdomain;

// loop the backtrace to find the file where the translation happens
foreach ($trace as $item) {
$call = $item['call'];
$file = explode(":", $item['file'], 2)[0];

// no latte file? continue!
if (!strpos($file, ".latte--")) continue;

// we have a translation in a latte file
if (
str_starts_with($call, '$rockfrontend->_(')
|| str_starts_with($call, '__(')
|| str_starts_with($call, '_x(')
|| str_starts_with($call, '_n(')
) {
// get the sourcefile from the note in the cached file
$content = file_get_contents($this->toPath($file));
$from = strpos($content, "/** source: ") + 12;
$to = strpos($file, ".latte--") - 5;
$templateFile = substr($content, $from, $to);
return $templateFile;
}
}
return false;
if (!str_contains($file, '.latte')) return false;
$content = file_get_contents($file);
preg_match('/source: (.*?) /', $content, $matches);
return $matches[1];
}

/** END translation support in LATTE files */
Expand Down
23 changes: 14 additions & 9 deletions translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
* {=_n('found one item', 'fount multiple items', $num)}
*/

use function ProcessWire\wire;
use ProcessWire\RockFrontend;

function __($str)
{
$rf = wire()->modules->get('RockFrontend');
return \ProcessWire\__($str, $rf->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\__($str, $textdomain);
}
function _x($str, $context)

function _x($str, $context): bool|array|string|null
{
$rf = wire()->modules->get('RockFrontend');
return \ProcessWire\_x($str, $context, $rf->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\_x($str, $context, $textdomain);
}
function _n($textsingular, $textplural, $count)

function _n($textsingular, $textplural, $count): bool|array|string|null
{
$rf = wire()->modules->get('RockFrontend');
return \ProcessWire\_n($textsingular, $textplural, $count, $rf->textdomain());
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\_n($textsingular, $textplural, $count, $textdomain);
}

0 comments on commit d11708a

Please sign in to comment.