Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ Blaze tries to avoid folding when it's likely to cause problems, but it cannot d

> This applies to internal logic. Passing global state into the component via attributes or slots can be fine — however, there are exceptions. We'll explore these in the following sections.

Translations (`__()`, `trans()`, `trans_choice()`, `@lang`) depend on the request locale too. Blaze detects them while folding and leaves a component that translates unfolded, so it renders in the current locale.

### Static attributes

Let's explore the folding process with a simple example where everything works smoothly.
Expand Down
15 changes: 15 additions & 0 deletions src/BladeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Lang;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\ComponentSlot;
use Livewire\Blaze\Exceptions\InvalidBlazeFoldUsageException;
use Livewire\Blaze\Exceptions\TranslatedWhileFoldingException;
use Livewire\Blaze\Folder\TranslationRecorder;
use Livewire\Blaze\Runtime\BlazeRuntime;
use Livewire\Blaze\Support\Utils;
use ReflectionClass;
Expand Down Expand Up @@ -107,6 +110,12 @@ function (string $input) use ($path) {
'folding' => true,
]);

// Translations depend on the request locale, so a component that translates is left unfolded...
$translator = app('translator');
$translationRecorder = new TranslationRecorder($translator);

Lang::swap($translationRecorder);

$obLevel = ob_get_level();
$hash = Utils::hash($path);
$compiled = $this->blade->getCompiledPath($path);
Expand Down Expand Up @@ -149,6 +158,12 @@ function (string $input) use ($path) {
$restoreCompiler();
$restoreFactory();
$restoreRuntime();

Lang::swap($translator);
}

if ($translationRecorder->translated()) {
throw new TranslatedWhileFoldingException($path);
}

$result = Unblaze::replaceUnblazePrecompiledDirectives($result);
Expand Down
18 changes: 18 additions & 0 deletions src/Exceptions/TranslatedWhileFoldingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Livewire\Blaze\Exceptions;

/**
* Thrown when a component translated text while being folded.
*
* Translations depend on the request locale, while folded output is compiled once and shared by
* every locale. Translating is not a misuse of folding, so the component is left unfolded
* rather than reported, even in throw mode.
*/
class TranslatedWhileFoldingException extends \Exception
{
public function __construct(string $componentPath)
{
parent::__construct("Component '{$componentPath}' translates text and can't be folded");
}
}
3 changes: 3 additions & 0 deletions src/Folder/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Event;
use Livewire\Blaze\Events\ComponentFolded;
use Livewire\Blaze\Exceptions\InvalidBlazeFoldUsageException;
use Livewire\Blaze\Exceptions\TranslatedWhileFoldingException;
use Livewire\Blaze\Parser\Nodes\ComponentNode;
use Livewire\Blaze\Parser\Nodes\Node;
use Livewire\Blaze\Parser\Nodes\SlotNode;
Expand Down Expand Up @@ -71,6 +72,8 @@ public function fold(Node $node): Node
));

return new TextNode('<?php ob_start(); ?>' . $html . '<?php echo ltrim(ob_get_clean()); ?>');
} catch (TranslatedWhileFoldingException) {
return $node;
} catch (Throwable $th) {
if ($this->manager->shouldThrow()) {
throw $th;
Expand Down
64 changes: 64 additions & 0 deletions src/Folder/TranslationRecorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Livewire\Blaze\Folder;

use Illuminate\Contracts\Translation\Translator;

/**
* Stands in for the translator during folding and records whether anything was translated.
*
* Translations depend on the request locale, but folded output is compiled once and shared by
* every locale. A component that translates while folding is therefore left unfolded. Calls
* are passed through to the real translator, so rendering itself behaves as usual.
*/
class TranslationRecorder implements Translator
{
protected bool $translated = false;

public function __construct(
protected Translator $translator,
) {
}

public function get($key, array $replace = [], $locale = null)
{
$this->translated = true;

return $this->translator->get($key, $replace, $locale);
}

public function choice($key, $number, array $replace = [], $locale = null)
{
$this->translated = true;

return $this->translator->choice($key, $number, $replace, $locale);
}

public function getLocale()
{
return $this->translator->getLocale();
}

public function setLocale($locale)
{
$this->translator->setLocale($locale);
}

/**
* Whether anything was translated while this recorder stood in for the translator.
*/
public function translated(): bool
{
return $this->translated;
}

/**
* Other translator methods (has(), string(), ...) depend on the locale as well.
*/
public function __call($method, $parameters)
{
$this->translated = true;

return $this->translator->{$method}(...$parameters);
}
}
16 changes: 16 additions & 0 deletions tests/Folder/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,19 @@

expect($result)->toBeInstanceOf(TextNode::class);
});

test('does not fold components that translate, even in throw mode', function (string $component) {
$node = app(Parser::class)->parse("<x-foldable.{$component} />")[0];
$folded = app(Folder::class)->fold($node);

expect($folded)->toBeInstanceOf(ComponentNode::class);
})->with(['translated', 'translated-parent']);

test('restores the translator after folding', function () {
$translator = app('translator');

$node = app(Parser::class)->parse('<x-foldable.translated />')[0];
app(Folder::class)->fold($node);

expect(app('translator'))->toBe($translator);
});
12 changes: 12 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,15 @@ public function render()

view('mix')->render();
})->throwsNoExceptions();

test('renders translations in folded components in the current locale', function (string $component) {
app('translator')->addLines(['*.Save & close' => 'Uložiť a zavrieť'], 'sk');

app()->setLocale('sk');

expect(Blade::render("<x-foldable.{$component} />"))->toContain('aria-label="Uložiť a zavrieť"');

app()->setLocale('en');

expect(Blade::render("<x-foldable.{$component} />"))->toContain('aria-label="Save &amp; close"');
})->with(['translated', 'translated-parent']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@blaze(fold: true)

<div>
<x-foldable.translated />
</div>
3 changes: 3 additions & 0 deletions tests/fixtures/views/components/foldable/translated.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@blaze(fold: true)

<button aria-label="{{ __('Save & close') }}">{{ trans_choice('apples', 2) }}</button>
Loading