PHP code example of alexjoffroy / laravel-localization

1. Go to this page and download the library: Download alexjoffroy/laravel-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/ */

    

alexjoffroy / laravel-localization example snippets


'default_locale' => config('app.fallback_locale'),

'supported_locales' => [
    'en' => [
        'native' => 'English',
        'regional_code' => 'en_GB',
        'charset' => 'UTF-8',
        'constants' => ['LC_TIME'],
    ],
    'fr' => [
        'native' => 'Français',
        'regional_code' => 'fr_FR',
        'charset' => 'UTF-8',
        'constants' => ['LC_TIME'],
    ],
],

'hide_default_locale_in_url' => false,

// app/Http/Kernel.php

protected $middleware = [
    // ...
    \AlexJoffroy\Localization\Http\Middlewares\SetLocaleFromCurrentRoute::class,
];

// routes/web.php

Route::locales(function() {
    Route::get(
        trans('routes.about'), 
        'App\Http\Controllers\AboutController@index'
    )->name('about');
});

$l10n = app('localization');
// or
$l10n = L10n::getFacadeRoot();
// or
$l10n = l10n();

// Given `en` is the current locale

$l10n->getLocale(); // `en`

$l10n->setLocale('fr'); // Set the current locale to `fr`

// Given `en` is the current locale

$l10n->isCurrentLocale('en'); // true

$l10n->isCurrentLocale('not-current'); // false


$l10n->isSupportedLocale('en'); // true

$l10n->isSupportedLocale('not-supported'); // false


// Given `en` is the default locale

$l10n->isDefaultLocale('en'); // true

$l10n->isDefaultLocale('not-default'); // false

$l10n->getSupportedLocales(); // All supported locales (from supported_locales)

$l10n->getSupportedLocale('en'); // Given supported locale (from supported_locales)

$l10n->getSupportedLocaleKeys(); // All supported locale keys (from supported_locales)

$l10n->getDefaultLocale(); // Default locale (from default_locale)


// Given `en` is the default locale

$l10n->shouldHideLocaleInUrl('en'); // True if `hide_default_locale_in_url` is true 

$l10n->shouldHideLocaleInUrl('fr'); // False, even if `hide_default_locale_in_url` is true 

$l10n->route('about', [], true, 'en'); // `https://yourapp.com/en/about`

$l10n->route('about', [], false, 'en'); // `/en/about`

$l10n->route('about', [], true, 'fr'); // `https://yourapp.com/fr/a-propos`

// Shortcut will fallback to current locale
$l10n->route('about'); // `https://yourapp.com/en/about` 


// Given the current app url is `https://yourapp.com/en/about`

$l10n->currentRoute('fr'); // `https://yourapp.com/fr/a-propos`

$l10n->currentRoute('fr', false); // `/fr/a-propos`

// Default view
$l10n->renderSwitch();

// Custom view
$l10n->renderSwitch('path.to.view');

// Custom view, with additional data
$l10n->renderSwitch('path.to.view', ['foo' => 'bar']);

L10n::getLocale();
L10n::setLocale();
L10n::route();
L10n::currentRoute();
// etc

// Get the Localization instance
$l10n = l10n(); 

// Get the current locale
$current = locale(); 

// Set the current locale
locale('en');

// Get supported locale
$supported = locales();
bash
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="config"
bash
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="views"
blade
<select name="switch" id="switch">
    @foreach($l10n->getSupportedLocales() as $locale => $localeSettings)    
        <option value="{{ $locale }}" {{ $l10n->isCurrentLocale($locale) ? 'selected' : '' }}>
            {{ ucfirst($localeSettings['native']) }}
        </option>
    @endforeach
</select>