PHP code example of josiasmontag / laravel-localization

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

    

josiasmontag / laravel-localization example snippets


    protected $middlewareGroups = [
        'web' => [
        
            // Other Middleware
            
            \Lunaweb\Localization\Middleware\LocalizationHandler::class,
        ],
    ];

return [
    
    // Add any language you want to support
    'locales' => [
        'en' => ['name' => 'English'],
        'de' => ['name' => 'German'],
    ],

    // The default locale is configured in config/app.php (locale)

    // Default locale will not be shown in the url.
    // If enabled and 'en' is the default language:
    // / -> English page, /de -> German page
    // If disabled:
    // /en -> English Page, /de -> German page
    'hide_default_locale_in_url' => true,

    // Use query parameter if there are no localized routes available.
    // Set it to null to disable usage of query parameter.
    'locale_query_parameter' => 'hl',

    // Enable redirect if there is a localized route available and the user locale was detected (via HTTP header or session)
    'redirect_to_localized_route' =>  true,

    // Try to detect user locale via Accept-Language header.
    'detect_via_http_header' => true,

    // Remember the user locale using a cookie.
    'remember_via_cookie' => true,

    // Cookie expire time in minutes
    'cookie_expires' => 20160 // 14 days

];



Localization::localizedRoutesGroup(function() {
    Route::get('/', 'HomeController@uploadDocuments')->name('index');
    Route::get('/register', 'RegisterController@showRegisterForm')->name('register');
});

'locales' => [
   'en' => ['domain'=> 'domain.com', 'name' => 'English'],
   'de' => ['domain'=> 'domain.de', 'name' => 'German'],
   'fr' => ['domain'=> 'domain.fr', 'name' => 'French'],
],

Route::get('/contact', 'ContactController@showContactForm')
    ->localization('en')
    ->name('contact');
    
Route::get('/kontakt', 'ContactController@showContactForm')
    ->localization('de')
    ->name('de.contact');


Localization::isLocalizedRoute()

Route::current()->getLocalization() === null

Route::current()->getLocalization()

Localization::getLocaleUrl($localeCode)

Localization::getLocaleRoute($localeCode)

@if(Localization::isLocalizedRoute())
   @foreach(Localization::getLocales() as $localeCode => $properties)
        <link rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode) }}">
   @endforeach
@endif

<ul>
    @foreach(Localization::getLocales() as $localeCode => $properties)
        <li>
            <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode, true) }}">
                 {{ $properties['native'] }} </a>
        </li>
    @endforeach
</ul>