# Using WPML with Livewire in Sage

**URL:** https://discourse.roots.io/t/using-wpml-with-livewire-in-sage/28864
**Category:** sage
**Tags:** sage10, livewire
**Created:** 2024-10-31T10:15:03Z
**Posts:** 8

## Post 1 by @GamendeBrian — 2024-10-31T10:15:04Z

Hi,

I have recently been trying to use Livewire inside the Sage theme.

Already got some projects running on it, also had some problems along the way, but managed to work around it.

Now I have a project which is using WPML.

I made a Livewire filter, but came across the following issue:

When filtering (Livewire updating) on a language besides the default language, the language switches back to the default language.

I’ve debugged and tried a lot of things, but the only “solution” so far is the following.

At the top of my generic filter function I do

```
$locale = substr(app()->getLocale(), 0, 2);
$current_locale = apply_filters( 'wpml_current_language', NULL) ?? 'nl';

if($locale != $current_locale) {
    do_action( 'wpml_switch_language', $locale );
}
```

It works… but its just not the perfect solution.

In Laravel, I would make a Middleware controller to check this, Sage doesn’t really “support” this yet (as far as I know).

Does anyone have a better idea / solution?

Thanks!

---

## Post 2 by @JacobBlana — 2024-10-31T12:13:34Z

We had internally many headaches due to multilanguage setup with livewire. But one day as i was trying to figure out way to made it work for multisites a discovered this piece in livewire docs. Which essentially solves all the problems with content loading from wrong db/language.

> **[Installation | Laravel](https://livewire.laravel.com/docs/installation#configuring-livewires-update-endpoint)**
>
> A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

In short, you just need to match your url localization with update endpoint… For example ‘/es/livewire/update’… Then it just magically works!

---

## Post 3 by @alexgm — 2025-03-02T18:17:46Z

I wasnt aware of this issue until now… What a shame!

Does anybody came up with a fix?

---

## Post 4 by @alexgm — 2025-03-02T18:52:07Z

Okay found out something based on JacobBlana answer.  
SImply put this into your ThemeServiceProvider Boot method.

```
/**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        // Get current language
        // This my implementation. it return the WP lang if WPML or POlylnag isnt installed, otherwise we use the plugin API to retrieve the current lang code.
        $curr_lang = Multilang::get_current_language(); /

        // Define custom Livewire update route
        Livewire::setUpdateRoute(function ($handle) use ($curr_lang) {
            return Route::post("/{$curr_lang}/livewire/update", $handle);
        });

       // Rest of your code....
}
```

Special thanks to Jacob for that!

---

## Post 5 by @GamendeBrian — 2025-03-11T10:22:22Z

I think this is indeed the cleanest solution! :rocket:

---

## Post 6 by @GamendeBrian — 2025-03-11T10:24:27Z

Thanks @JacobBlana and @alexgm for your help!

---

## Post 7 by @alexgmenard — 2025-10-06T06:43:59Z

This has been broken recently… The Route now need a name like so:

```
$curr_lang = Multilang::get_current_language();
Livewire::setUpdateRoute(function ($handle) use ($curr_lang) {
    return Route::post("/{$curr_lang}/livewire/update", $handle)
        ->middleware('web')
        ->name("livewire.update.{$curr_lang}");
});
```

---

## Post 8 by @tombro — 2026-09-02T08:30:12Z

I just ran into this issue. I’m experiencing some issues with the proposed solution and route caching. Only one route is getting cached (current language update route). I propose this update to the accepted answer:

```
$activeLanguages = apply_filters('wpml_active_languages', [], null);
$currentLanguage = apply_filters('wpml_current_language', null);

foreach (array_keys($activeLanguages) as $activeLanguage) {
    $route = Route::post("/{$activeLanguage}/livewire/update", [HandleRequests::class, 'handleUpdate'])
        ->middleware('web')
        ->name("livewire.update.{$activeLanguage}");

    if ($activeLanguage === $currentLanguage) {
        Livewire::setUpdateRoute(fn () => $route);
    }
}
```

I also experienced another issue with Livewire and WPML: [WPML + Livewire: incorrect script URL and update URL · Issue #567 · roots/acorn · GitHub](https://github.com/roots/acorn/issues/567)  
This can be temporarily fixed by adding `URL::forceRootUrl(WP_HOME);` right after acorn is booted.
