Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set default locale with language switcher #66

Open
kmergen opened this issue Jan 13, 2024 · 5 comments
Open

Cannot set default locale with language switcher #66

kmergen opened this issue Jan 13, 2024 · 5 comments
Assignees
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@kmergen
Copy link

kmergen commented Jan 13, 2024

What steps will reproduce the problem?

My default locaal is 'de'.

What is the expected result?

An url without 'de'

What do you get instead?

An url with 'de'

Additional info

I have this language switcher:

 <li>
    <a href="#."><?= $translator->translate('menu.language') ?></a>
    <ul>
        <li><a href="<?= $urlGenerator->generateFromCurrent(['_language' => 'en'], fallbackRouteName: 'home') ?>">English</a></li>
        <li><a href="<?= $urlGenerator->generateFromCurrent(['_language' => 'de'], fallbackRouteName: 'home') ?>">Deutsch</a></li>
        <li><a href="<?= $urlGenerator->generateFromCurrent(['_language' => 'nl'], fallbackRouteName: 'home') ?>">Nederlands</a></li>
        <li><a href="<?= $urlGenerator->generateFromCurrent(['_language' => 'fr'], fallbackRouteName: 'home') ?>">Français</a></li>

    </ul>
</li>

my config params.php

  'app' => [
        'charset' => 'UTF-8',
        'locale' => 'de',
        'name' => 'My Project',
    ],

    'yiisoft/aliases' => [
        'aliases' => require __DIR__ . '/aliases.php',
    ],

    'yiisoft/translator' => [
        'locale' => 'de',
        'fallbackLocale' => 'de',
        'defaultCategory' => 'app',
    ],

If I call my website url without language the german website is show as expected.
If I switch to English the german website is shown again without 'en'. (website)
If I switch to German the german website is shown but with the 'de'
(website/de)

Q A
Version 1.0.?
PHP version 8.1
Operating system Windows 11
@samdark
Copy link
Member

samdark commented Jan 15, 2024

Likely that's the issue of Locale middleware.

@samdark samdark transferred this issue from yiisoft/translator Jan 15, 2024
@samdark
Copy link
Member

samdark commented Jan 15, 2024

Actually... have you configured it?

@kmergen
Copy link
Author

kmergen commented Jan 15, 2024

params.php

'yiisoft/translator' => [
        'locale' => 'de',
        'fallbackLocale' => 'de',
        'defaultCategory' => 'app',
    ],

With other words. I cannot switch to English language.

sourceLocale is always 'en' (How can i change this).

If i choose English the handle function in

src\EventHandler\SetLocaleEventHandler.php

is not called

final class SetLocaleEventHandler
{
    public function __construct(
        private TranslatorInterface $translator,
        private WebView $webView
    ) {
    }

    public function handle(SetLocaleEvent $event): void
    {
        $this->translator->setLocale($event->getLocale());
        $this->webView->setLocale($event->getLocale());
    }
}

@arogachev arogachev self-assigned this Jan 15, 2024
@arogachev arogachev added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jan 15, 2024
@arogachev
Copy link

Are you using Locale middleware? Could you show its config?

@kmergen
Copy link
Author

kmergen commented Jan 17, 2024

Yes,

config\web\params.php

<?php

declare(strict_types=1);

use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\Router\Middleware\Router;
use Yiisoft\Session\SessionMiddleware;
use Yiisoft\Yii\Middleware\Locale;

return [
    'middlewares' => [
        ErrorCatcher::class,
        SessionMiddleware::class,
        Locale::class,
        Router::class,
    ],

    'locale' => [
        'locales' => ['de' => 'de-DE', 'en' => 'en-US', 'nl' => 'nl-NL', 'fr' => 'fr-FR'],
        'ignoredRequests' => [
            '/debug**',
        ],
    ],
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants