diff --git a/README.md b/README.md
index 5371a0e3..955e1c30 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/src/BladeRenderer.php b/src/BladeRenderer.php
index 998a5b47..58174f63 100644
--- a/src/BladeRenderer.php
+++ b/src/BladeRenderer.php
@@ -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;
@@ -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);
@@ -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);
diff --git a/src/Exceptions/TranslatedWhileFoldingException.php b/src/Exceptions/TranslatedWhileFoldingException.php
new file mode 100644
index 00000000..9c50a505
--- /dev/null
+++ b/src/Exceptions/TranslatedWhileFoldingException.php
@@ -0,0 +1,18 @@
+' . $html . '');
+ } catch (TranslatedWhileFoldingException) {
+ return $node;
} catch (Throwable $th) {
if ($this->manager->shouldThrow()) {
throw $th;
diff --git a/src/Folder/TranslationRecorder.php b/src/Folder/TranslationRecorder.php
new file mode 100644
index 00000000..019d902d
--- /dev/null
+++ b/src/Folder/TranslationRecorder.php
@@ -0,0 +1,64 @@
+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);
+ }
+}
diff --git a/tests/Folder/FolderTest.php b/tests/Folder/FolderTest.php
index 8dfa1849..f76f959e 100644
--- a/tests/Folder/FolderTest.php
+++ b/tests/Folder/FolderTest.php
@@ -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("