Netflex Toolbox Help

Internationalization

The toolbox package includes some helpers for locale dependent url generations.

Locale dependent url generation

Consider a customer page that has two different pages; one in Norwegian and English. Both english and norwegian has a News section that shows different news.

Domain

Route

Name

test.no

/nyheter

nb_NO.news.index

test.co.uk

/news

en_GB.news.index

Given a similar url structure, the LocaleDependentUrlGenerator will allow you to write

<a href="{{ route('news.index') }}>Text</a>

regardless of which site you are on, but will navigate to /news when on test.co.uk and /nyheter when on test.no.

Usage

The middleware does require some setup in order for it to work just like the laravel one. You can adjust your RouteServiceProvider or use the LocaleDependentUrlGenerator trait that configures it, and then running registerLocaleDependentUrlGenerator() function.

<?php use Netflex\Toolbox\Traits; use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract; class RouteServiceProvider extends ServiceProvider { use LocaleDependentUrlGenerator; public function register() { $this->registerLocaleDependentUrlGenerator(); } }

Setting locale in edit and preview mode

Since we don't really have control over the edit and preview mode endpoint and the endpoint will be to the same domain, regardless of what domain the viewed should have, most common domain/locale resolvers we use might not work for edit/preview mode.

In order to get around this issue, extending __current_page__ in the service provider can give you a simple way of adjusting the locale by using it as some sort of middleware system.

While probably not a very clean solution, it does allow us to ensure we also set the locale when we're fetching the page.

$this->app->extend('__current_page__', function(?\Netflex\Pages\Page $page, $app) { if((preview_mode() || edit_mode()) && $page && $page->lang) { App::setLocale($page->lang); } return $page; });
Last modified: 16 September 2024