Locale select

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
vision
Posts: 23
Joined: 01 Dec 2022, 05:35

Locale select

Post by vision » 27 Apr 2023, 06:11

Hello!
My site have 2 languages. When I use a filter in catalog, to filter, for example, by "color" - it shows me filtered products, but changes site language to language set in my "env.php" file at "APP_LOCALE=" line, instead of selected by user. Steps I’ve taken:

Code: Select all

SHOP_MULTILOCALE=true
setting enabled at env.php file.

Added the allowed language currency combination in the Locale > Locale panel (2 lagguages\ 1 currency for both of them).

How can I fix that, I want language selected by user to be remembered when "filtering"?

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Locale select

Post by aimeos » 29 Apr 2023, 07:42

Does the "locale" parameter gets lost in the target URL?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

vision
Posts: 23
Joined: 01 Dec 2022, 05:35

Re: Locale select

Post by vision » 02 May 2023, 05:08

aimeos wrote: 29 Apr 2023, 07:42 Does the "locale" parameter gets lost in the target URL?
Yes.
URL: .../shop/hot-deals~63?locale=ru&currency=RUB
URL with filter: .../shop/hot-deals~63?f_search=&f_price%5B0%5D=0&f_price%5B1%5D=2001&f_price%5B1%5D=2001&f_attrid%5B%5D=17

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Locale select

Post by aimeos » 02 May 2023, 17:36

If SHOP_MULTILOCALE is enabled, the locale parameter should be always part of the URL path, not the query string because those query parameters are removed by browsers when using GET form requests:
https://github.com/aimeos/aimeos/blob/m ... hp#L23-L28

Did you change anything in the ./route/web.php file?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

vision
Posts: 23
Joined: 01 Dec 2022, 05:35

Re: Locale select

Post by vision » 03 May 2023, 11:56

aimeos wrote: 02 May 2023, 17:36 If SHOP_MULTILOCALE is enabled, the locale parameter should be always part of the URL path, not the query string because those query parameters are removed by browsers when using GET form requests:
https://github.com/aimeos/aimeos/blob/m ... hp#L23-L28

Did you change anything in the ./route/web.php file?
Yes, I found it and fixed it.
For now my routes/web.php:

Code: Select all

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/ready', function() {
    return 'OK';
});

$params = [];
$conf = ['prefix' => '', 'where' => []];

// Aimeos routes

if( env( 'SHOP_MULTILOCALE' ) )
{
    $conf['prefix'] .= '{locale}';
    $conf['where']['locale'] = '[a-z]{2}(\_[A-Z]{2})?';
    $params = ['locale' => app()->getLocale()];
}

if( env( 'SHOP_MULTISHOP' ) )
{
    $conf['prefix'] .= '/{site}';
    $conf['where']['site'] = '[A-Za-z0-9\.\-]+';
}

// Custom routes

Route::group( $conf, function() {

    Route::match( array( 'GET', 'POST' ), '/', array(
        'as' => 'aimeos_home',
        'uses' => 'Aimeos\Shop\Controller\CatalogController@listAction'
    ) )->where( ['locale' => '[a-z]{2}(\_[A-Z]{2})?', 'site' => '[A-Za-z0-9\.\-]+'] );

    Route::match( array( 'GET' ), 'admin-aimeos', array(
        'as' => 'aimeos_shop_admin',
        'uses' => 'Aimeos\Shop\Controller\AdminController@indexAction'
    ) )->where( ['locale' => '[a-z]{2}(\_[A-Z]{2})?', 'site' => '[A-Za-z0-9\.\-]+'] );

})->middleware(['web']);


if( $conf['prefix'] )
{
    Route::get('/', function () use ($params) {
        return redirect(airoute('aimeos_home', $params));
    });
}

Route::group($conf ?? [], function() {
    require __DIR__.'/auth.php';
});
Now it works fine with "home page", its URL: http://devshop.tm.ru/ru
But all links from this page are still without {locale} prefix.
Example "Hot deals" button's URL: http://devshop.tm.ru/shop/hot-deals~63?locale=ru
If I try to type URL manually (http://devshop.tm.ru/ru/shop/hot-deals~63?locale=ru), I get an error:

aimeos-laravel:~2022.10
Illuminate\Routing\Exceptions\UrlGenerationException
PHP 8.1.14
9.52.7
"Missing required parameter for [Route: login] [URI: {locale}/login] [Missing parameter: locale]."

From this part of code: vendor/aimeos/aimeos-laravel/src/views/base.blade.php: 57

Code: Select all

@if (Auth::guest())

					<li class="nav-item login"><a class="nav-link" href="{{ airoute( 'login' ) }}" title="{{ __( 'Login' ) }}"><span class="name">{{ __( 'Login' ) }}</span></a></li>

				@else
I can fix that via adding into the config/shop.php:

Code: Select all

'routes' => [
	'default' => ['prefix' => '{locale}/shop', ...],
But login route still doesn't work with an error:

resources/views/auth/login.blade.php: 5
"Missing required parameter for [Route: login] [URI: {locale}/login] [Missing parameter: locale]."

Code: Select all

 <form method="POST" action="{{ route('login') }}">
So how can I fix it properly?

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Locale select

Post by aimeos » 07 May 2023, 11:06

The reason are your two custom routes you've added to the routes/web.php. They simply don't work because the first won't have any locale and the admin login requires a locale that's not there. If you remove that custom routes again, it should work as expected.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

vision
Posts: 23
Joined: 01 Dec 2022, 05:35

Re: Locale select

Post by vision » 10 May 2023, 05:55

aimeos wrote: 07 May 2023, 11:06 The reason are your two custom routes you've added to the routes/web.php. They simply don't work because the first won't have any locale and the admin login requires a locale that's not there. If you remove that custom routes again, it should work as expected.
I removed all custom code from routes/web.php. Still doesn't work with the same error. if I comment these lines, everything works:

Code: Select all

//Route::group($conf ?? [], function() {
    require __DIR__.'/auth.php';
//});
Why? It seems login route doesn't want to work with any locale prefix.

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Locale select

Post by aimeos » 11 May 2023, 06:59

There can be route conflicts with the auth routes. Can you please post the output of "composer show"?

A possible solution is to add a prefix like "auth" to all authentication routes so they are not matched by any other routes.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

vision
Posts: 23
Joined: 01 Dec 2022, 05:35

Re: Locale select

Post by vision » 11 May 2023, 10:21

aimeos wrote: 11 May 2023, 06:59 There can be route conflicts with the auth routes. Can you please post the output of "composer show"?

A possible solution is to add a prefix like "auth" to all authentication routes so they are not matched by any other routes.
Thank You a lot.! I'll try it.

Post Reply