Code Monkey home page Code Monkey logo

Comments (1)

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

Hi,

You can add the middleware to the index route to get a locale from the session or cookie or the browser.
Then I just do a redirect to a localized route:

// Routes

Route::get('/', [HomeController::class, 'redirect'])->name('index')->middleware(\CodeZero\LocalizedRoutes\Middleware\SetLocale::class);

Route::localized(function () {
    Route::get('/', [HomeController::class, 'home'])->name('home');
});

// HomeController

public function redirect()
{
    return redirect()->route('home');
}

public function home()
{
    return view('home');
}

For the browser detection I'm using https://github.com/codezero-be/browser-locale.
By default it will spit out something like this:

$browser = new \CodeZero\BrowserLocale\BrowserLocale('en-US,nl;q=0.8');
$filter = \CodeZero\BrowserLocale\Filters\CombinedFilter;
$locales = $browser->filter($filter);
//=> Result: ['en-US', 'en', 'nl']

So probably 'en' and 'en-UK' for you.

This list will be checked by https://github.com/codezero-be/laravel-localizer against the supported locales for a match.

You can create a new BrowserDetector that uses a different BrowserLocale Filter or some custom logic. This is the default one:

https://github.com/codezero-be/laravel-localizer/blob/master/src/Detectors/BrowserDetector.php

To actually use it you need to publish the Localized config file in your project and add the new class to the detectors array.

https://github.com/codezero-be/laravel-localizer/blob/master/config/localizer.php

You may even want to create 2 detectors, a CountryDetector and a LanguageDetector and add them to the config in order of priority. Only the first match is used. There are also 2 BrowserLocale Filters for that.

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.