Code Monkey home page Code Monkey logo

Comments (2)

luxiflex avatar luxiflex commented on May 29, 2024 2

Thanks a lot! I've copied the SetLocale middleware and placed the controller logic in there.

  public function handle($request, Closure $next)
  {
      // This package sets a custom route attribute for the locale.
      // If it is present, use this as the locale.
      $locale = $request->route()->getAction('localized-routes-locale')
          ?: $this->getLocaleFromFallbackRoute($request);

      App::make(LocaleHandler::class)->handleLocale($locale);

      //custom logic
      session()->put('locale', $locale);
      App::setLocale($locale);

      if (auth()->check() && auth()->user()->language != $locale) {
          auth()->user()->update(['language' => $locale]);
      }

      return $next($request);
  }

Now to switch language i can simply use

<a class="dropdown-item" href="{{ Route::localizedUrl('en') }}"><i class="flag-icon flag-icon-us"></i> English</a>
<a class="dropdown-item" href="{{ Route::localizedUrl('nl') }}"><i class="flag-icon flag-icon-nl"></i> Nederlands</a>

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024 1

Hello,

After some research I found the following problems:

redirect()->back() fetches URL::previous(), which returns the plain text URL in the referer header of the current request (the one where you redirect()->back()).

That plain text URL has a locale slug so that will always update the locale when you redirect to it.

Unfortunately, my code can only convert the locale and parameters of the current route or URL, because it needs a Route object to get the parameters.

There may or may not be a way to do this, but I will need to research this further.

If possible, you could add the User logic to the SetLocale middleware, or create a Store implementation in the Localizer package, if you use that. And then use a locale switcher like this for example:

@if (Route::isLocalized() || Route::isFallback())
    @foreach(Config::get('localized-routes.supported-locales') as $locale)
        @if ( ! App::isLocale($locale))
            <li>
                <a href="{{ Route::localizedUrl($locale) }}">
                    {{ strtoupper($locale) }}
                </a>
            </li>
        @endif
    @endforeach
@endif

from laravel-localized-routes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.