PHP code example of jersyfi / laravel-localization

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

    

jersyfi / laravel-localization example snippets


return [

    /**
     * Applications default locale need to be set because the config('app.locale')
     * gives back the current locale and not the value from config
     */
    'default_locale' => 'de',

    /**
     * Application locales determines all locals that exists in the application
     * You can ignore or delete the locales from app.locales if you set some
     */
    'locales' => [
        'en',
        'de'
    ],
    
    /**
     * Redirect to default locale when not found
     */
    'redirect_default' => false,

    /**
     * Detect user locale via http header
     * When no locale is stored in session user gets redirected
     */
    'detect_locale' => false,
    
    /**
     * Application can store the prefered_locale in the users database table
     */
    'store_users_database' => true,
    
    /**
     * Setup for the users database table
     * Only if 'store_users_database' is set to true
     */
    'database' => [
        'users_table_name' => 'users',
        'prefered_locale_column_name' => 'prefered_locale',
    ],

];

use Jersyfi\Localization\Http\Controllers\LocaleController;

Route::get('/', [LocaleController::class, 'localize'])
    ->name('locale');

Route::prefix('{locale}')
    ->middleware('locale')
    ->group(function () {

        // your localized routes here
    });

$slug = Localization::getLocaleSlug('en_GB'); // en-gb
$slug = Localization::getLocaleSlug(); // de

$locales = Localization::getLocales(); // ['en', 'de']

$locale = Localization::getDefaultLocale(); // de

$locales = Localization::getLocalesWithoutDefault(); // ['en']

$locales = Localization::getLocalesWithoutCurrent(); // ['de']

$url = Localization::currentRouteURL(); // https://test.de/de/home?query=true

$url = Localization::currentRouteLocaleURL('en'); // https://test.de/en/home?query=true

$url = Localization::currentRouteDefaultLocaleURL(); // https://test.de/de/home?query=true

$valid = Localization::localeIsValid('de'); // true
$valid = Localization::localeIsValid('de', 'en'); // true
$valid = Localization::localeIsValid('de', 'sp'); // false

Route::get('/', [LocaleController::class, 'localize'])
    ->name('locale');

Route::prefix('{locale}')
    ->middleware('locale')
    ->group(function () {

        // your localized routes here
        Route::get('/', [HomeController::class, 'index'])
            ->name('home');
    });
bash
php artisan vendor:publish --provider="Jersyfi\Localization\LocalizationServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Jersyfi\Localization\LocalizationServiceProvider" --tag="migrations"