Skip to content

Leave components that translate unfolded - #245

Open
iruoy wants to merge 1 commit into
livewire:mainfrom
iruoy:fix/translations-in-folded-components
Open

iruoy wants to merge 1 commit into
livewire:mainfrom
iruoy:fix/translations-in-folded-components

Conversation

@iruoy

@iruoy iruoy commented Nov 19, 2026

Copy link
Copy Markdown

The scenario

In an app with more than one locale, folded components show their translations in the locale of whichever request compiled the view first. That text is shown to everyone until view:clear.

<flux:sidebar.toggle /> {{-- aria-label="{{ __('Toggle sidebar') }}" --}}

The first request is in nl, so the view compiles with aria-label="Zijbalk tonen of verbergen". A later request in en reuses the compiled view and still gets the Dutch label. A plain {{ __('Toggle sidebar') }} in the same template does follow the locale. 51 folded Flux views call __().

The problem

Folding renders the component once, at compile time, and stores the HTML. __() is evaluated during that render, and compiled views don't depend on the locale.

#227 deferred each translation to a runtime call through placeholders, and was reverted. Adding __( to checkProblematicPatterns() would be simpler, but that check throws for every component that uses __(), which would break Flux. Checking the component's source wouldn't be enough either: a folded parent also renders its children at compile time, so a translation inside a child gets baked into the parent.

The solution

While folding, BladeRenderer swaps the translator for a TranslationRecorder (with Lang::swap(), as #227 did). The recorder passes every call through to the real translator and notes that a translation happened. If one did, the render throws TranslatedWhileFoldingException once everything is restored, and Folder leaves the component unfolded. It's then compiled as usual and renders in the current locale.

$translator = app('translator');
$translationRecorder = new TranslationRecorder($translator);

Lang::swap($translationRecorder);

try {
    // render as before…
} finally {
    // restore as before…
    Lang::swap($translator);
}

if ($translationRecorder->translated()) {
    throw new TranslatedWhileFoldingException($path);
}
  • What's covered. __(), trans(), trans_choice(), @lang, Lang::get() and Lang::choice() are recorded. Other translator methods (has(), string(), …) are recorded through __call, since they depend on the locale too.
  • Children. A child's translation runs while the parent folds, so the parent is left unfolded as well. Each nested fold wraps whatever translator is current, so this also holds when the child is folded on its own first.
  • Not reported as an error. Translating is a normal thing for a component to do, so this isn't an InvalidBlazeFoldUsageException, and it doesn't throw in throw mode either. Otherwise every Flux component that translates would throw in throw mode, as Blaze's own FluxTest modal case shows.
  • The trade-off. Components that translate lose folding and fall back to function compilation, so single-locale apps lose some speed on those components. If that matters, an opt-out for single-locale apps could be a follow-up. This PR keeps translated output correct by default.

Tests: a component that translates and a folded parent of one both render in the current locale after the view has been compiled in another one. They aren't folded, even in throw mode. The translator is restored afterwards. The new tests fail on main and the full suite passes. I also checked it in a Laravel 13 app with Flux 2.22: rendered in nl then en, and the other way round, flux:sidebar.toggle now follows the locale. The README's "Global state" section has a sentence on it.

Fixes #221

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Result: Default

Attempt Blade Blaze Change
#1 56.00ms 6.72ms 88%
#2 55.74ms 6.73ms 87.9%
#3 55.17ms 6.77ms 87.7%
#4 55.51ms 6.70ms 87.9%
#5 55.43ms 6.74ms 87.8%
#6 55.73ms 6.72ms 87.9%
#7 55.21ms 6.74ms 87.8%
#8 55.06ms 6.72ms 87.8%
#9 56.11ms 6.74ms 88%
#10 55.40ms 6.74ms 87.8%
Snapshot 55.97ms 6.77ms 87.9%
Result 55.47ms (~) 6.74ms (~) 87.8% (~)

Median of 10 attempts, 5000 iterations x 10 rounds, 10.7s total

To run a specific benchmark, comment /benchmark <name>
attributes, aware, class, default, forwarding, forwarding-merge, merge, named-slots, no-attributes, slot, compilation

@ghabriel25

ghabriel25 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

@iruoy In real app, most of the views has translation so I'm not sure if skip folding is good solution. Skip folding for most of the views will affecting performance imo.

@iruoy

iruoy commented Nov 20, 2026

Copy link
Copy Markdown
Author

@ghabriel25 I agree it’s a workaround. I think working translations are worth the trade-off for now. It would help to know why #227 was reverted before revisiting that approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Folded components bake translations (__()) in the locale of the first compiling request

2 participants