Skip to content

Commit

Permalink
fix: translate.php breaks regular PHP translations
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 20, 2023
1 parent 6123c1a commit 578c34d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,10 @@ public function setTextdomain($file = false)
*/
public static function textdomain($file)
{
if (!str_contains($file, '.latte')) return false;
// if the translation was not invoked from a cached latte file
// we return the file itself, which is the PHP file that called the
// translation method
if (!str_contains($file, '.latte--')) return $file;
$content = file_get_contents($file);
preg_match('/source: (.*?) /', $content, $matches);
return $matches[1];
Expand Down
14 changes: 7 additions & 7 deletions translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

use ProcessWire\RockFrontend;

function __($str)
function __($str, $textdomain = null, $context = '')
{
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\__($str, $textdomain);
if (!$textdomain) $textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\__($str, $textdomain, $context);
}

function _x($str, $context): bool|array|string|null
function _x($str, $context, $textdomain = null): bool|array|string|null
{
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
if (!$textdomain) $textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\_x($str, $context, $textdomain);
}

function _n($textsingular, $textplural, $count): bool|array|string|null
function _n($textsingular, $textplural, $count, $textdomain = null): bool|array|string|null
{
$backtrace = debug_backtrace(limit: 1);
$textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
if (!$textdomain) $textdomain = RockFrontend::textdomain($backtrace[0]["file"]);
return \ProcessWire\_n($textsingular, $textplural, $count, $textdomain);
}

0 comments on commit 578c34d

Please sign in to comment.