trzew
trzew

Reputation: 445

Website translation in Laravel using translations

I am beginner in Laravel. I make my project in Laravel 7.

I have this code:

router.php

$locale = App::getLocale();

if (App::isLocale('pl')) {
    Route::get('/', 'HomeController@index')->name('home');
    Route::get('/o-nas', 'HomeController@about')->name('about');
    Route::get('/oferta', 'HomeController@offer')->name('offer');
    Route::get('/realizacje', 'HomeController@realizations')->name('realizations');
    Route::get('/kontakt', 'HomeController@contact')->name('contact');
    Route::get('/polityka-prywatnosci', 'HomeController@privacyPolicy')->name('privacy-policy');
}

Route::get('/en', 'HomeController@index')->name('home2');

if (App::isLocale('en')) {
    Route::get('/en/about-us', 'HomeController@about')->name('about');
    Route::get('/en/offer', 'HomeController@offer')->name('offer');
    Route::get('/en/realizations', 'HomeController@realizations')->name('realizations');
    Route::get('/en/contact', 'HomeController@contact')->name('contact');
    Route::get('/en/privacy-policy', 'HomeController@privacyPolicy')->name('privacy-policy');
}

Route::any('{query}',
    function() { return redirect('/'); })
    ->where('query', '.*');

in resources/lang I make 2 directory: pl and en and put router.php:

return array(
    'start' => 'start',
    'about'   => 'about us',
    'offer'   => 'offer',
    'realizations'   => 'realizations',
    'contact'   => 'contact'
)

In blade I make this url:

<ul class="navbar-nav">
                        <li class="nav-item d-block d-lg-none  d-xl-block text-center">
                            <a class="nav-link active" href="{{ route('home') }}">Start</a>
                        </li>
                        <li class="nav-item text-center">
                            <a class="nav-link" href="{{ route('about') }}">O nas</a>
                        </li>
                        <li class="nav-item text-center">
                            <a class="nav-link" href="{{ route('offer') }}">Oferta</a>
                        </li>
                        <li class="nav-item text-center">
                            <a class="nav-link" href="{{ route('realizations') }}">Realizacje </a>
                        </li>
                        <li class="nav-item text-center">
                            <a class="nav-link" href="{{ route('contact') }}">Kontakt</a>
                        </li>
                        <li class="nav-item mx-xl-4 mb-2 mb-md-0 text-center">
                            <a class="nav-link order-visit" href="{{ route('contact') }}">Zamów wizytę</a>
                        </li>
                        <li class="nav-item text-center d-inline-block d-lg-none language-box2">
                            <a href="{{ route('home') }}">PL</a> | <a href="{{ route('home2') }}">EN</a>
                        </li>
                    </ul>

Is this code correct for translations?

I have a problem, without changing the language I cannot go to e.g. domain.com/en/contact, domain.com/en/about-us etc

How can I repair this problem?

Upvotes: 1

Views: 43

Answers (1)

Bahaeddine
Bahaeddine

Reputation: 119

in the routes , for exepmle the home check the lang and redirect

   <li class="nav-item d-block d-lg-none  d-xl-block text-center">
         @if (App::isLocale('pl'))
         <a class="nav-link active" href="{{ route('home') }}">Start</a>
         @else
         <a class="nav-link active" href="{{ route('home2') }}">Start</a>
         @endif
    </li>

      

Upvotes: 1

Related Questions