1. Go to this page and download the library: Download laravel-tipi/localization library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Tipi\Localization\Facades\Localization;
$registry = Localization::locales();
$registry->all(); // Collection<string, Locale>, every locale, keyed by code, in order
$registry->active(); // active locales only
$registry->activeCodes(); // ['en', 'ka']
$registry->find('ka'); // ?Locale
$registry->get('ka'); // Locale, or throws LocaleNotFoundException
$registry->getActive('ka');// Locale, or throws LocaleNotFoundException / UnsupportedLocaleException
$registry->isActive('ka'); // bool
$registry->default(); // the global default Locale
$registry->defaultCode(); // 'en'
$locale = $registry->get('ka');
$locale->code; // 'ka'
$locale->name; // 'Georgian'
$locale->nativeName; // 'ქართული'
$locale->nativeAbbreviation; // 'ქა'
$locale->countryCode; // 'GE'
$locale->textDirection; // TextDirection::Ltr
$locale->isActive();
$locale->isDefault(); // the GLOBAL default; for a context use $context->isDefault($locale)
// config/localization.php
'contexts_driver' => 'config',
'default_context' => 'web',
'contexts' => [
'web' => [
'locales' => '*', // every active locale
],
'admin' => [
'locales' => ['en', 'ka'],
'default' => 'en', // optional; omitted means "follow the global default"
],
],
$admin = Localization::context('admin');
$admin->name; // 'admin'
$admin->supported(); // Collection<string, Locale>, in global order
$admin->codes(); // ['en', 'ka']
$admin->supports('ru'); // false
$admin->find('ru'); // null
$admin->get('ru'); // throws UnsupportedLocaleException
$admin->default(); // context default Locale
$admin->defaultCode(); // 'en'
$admin->isDefault('en'); // true: the context default, which may differ from $locale->isDefault()
Localization::context(); // the current context
Localization::contexts()->names(); // ['web', 'admin']
Localization::contexts()->all(); // Collection<string, LocaleContext>
Localization::contexts()->has('shop');
use Tipi\Localization\Http\Middleware\UseLocaleContext;
Route::middleware(['web', UseLocaleContext::with('admin')])->group(function () {
// ...
});
// or with the alias
Route::middleware(['web', 'localization.context:admin'])->group(/* ... */);
'contexts_driver' => 'database',
use Tipi\Localization\Actions\CreateLocaleContext;
use Tipi\Localization\Actions\DeleteLocaleContext;
use Tipi\Localization\Actions\UpdateLocaleContext;
use Tipi\Localization\Data\CreateLocaleContextData;
use Tipi\Localization\Data\UpdateLocaleContextData;
app(CreateLocaleContext::class)->execute(
new CreateLocaleContextData(name: 'shop', locales: ['en', 'ka'], default: 'ka'),
);
app(UpdateLocaleContext::class)->execute(
'admin',
new UpdateLocaleContextData(locales: ['en', 'ka', 'ru'], default: 'en'),
);
app(UpdateLocaleContext::class)->execute('kiosk', new UpdateLocaleContextData(locales: null)); // "*"
app(DeleteLocaleContext::class)->execute('shop');
use Illuminate\Support\Facades\Route;
use Tipi\Localization\Facades\Localization;
Localization::routes(function () {
Route::get('/', HomeController::class)->name('home');
Route::get('/wines/{wine}', [WineController::class, 'show'])->name('wines.show');
Route::post('/contact', ContactController::class)->name('contact');
});
Localization::currentUrl('ka'); // this page in Georgian (params + query kept), or null off localized routes
Localization::currentRouteName(); // 'wines.show' or null
Localization::routeIs('wines.*'); // bool
Localization::hasRoute('wines.show');
Localization::select('ka'); // current context: remembered + applied immediately
Localization::select('ka', 'admin'); // another context: remembered only
Localization::preference(); // ?Locale, the remembered choice (if still supported)
Localization::forgetPreference('admin');