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

Translation and language issue with routing on multilang #2428

Closed
afbora opened this issue Feb 5, 2020 · 4 comments
Closed

Translation and language issue with routing on multilang #2428

afbora opened this issue Feb 5, 2020 · 4 comments

Comments

@afbora
Copy link
Contributor

@afbora afbora commented Feb 5, 2020

Describe the bug
I can't get correct translations on the routes. The translation of the default language is always returned.

To Reproduce
Steps to reproduce the behavior:

  1. Enable multi-language
  2. Add two or more languages
  3. Add some translations
  4. Create route that echo a translation
  5. Enter route url as /tr/test
  6. See wrong string as Hello instead of Merhaba

Sample

\site\languages\tr.php

return [
    'code' => 'tr',
    'default' => false,
    'direction' => 'ltr',
    'locale' =>'tr',
    'name' => 'TR',
    'translations' => [
        'test' => 'Merhaba'
    ],
    'url' => NULL
];

\site\languages\en.php

return [
    'code' => 'en',
    'default' => true,
    'direction' => 'ltr',
    'locale' =>'en',
    'name' => 'EN',
    'translations' => [
        'test' => 'Hello'
    ],
    'url' => NULL
];

\site\config\config.php

return [
    'debug' => true,
    'languages' => true,
    'routes' => [
        [
            'pattern' => 'test',
            'language' => '*',
            'action'  => function($language) {
                echo t('test'); // Returns hello always as default language translation
            }
        ]
    ]
];

Kirby Version
3.3.3

Additional context
The language is returning correct as expected.

return [
    'debug' => true,
    'languages' => true,
    'routes' => [
        [
            'pattern' => 'test',
            'language' => '*',
            'action'  => function($language) {
                echo $language->code(); // Returns correct language
            }
        ]
    ]
];
@afbora

This comment has been minimized.

Copy link
Contributor Author

@afbora afbora commented Feb 5, 2020

I temporarily found a solution like this:

tt('test', null, [], $language->code());
@afbora

This comment has been minimized.

Copy link
Contributor Author

@afbora afbora commented Feb 8, 2020

I can fix the issue with adding setCurrentTranslation and setCurrentLanguage method to LanguageRouter class like following:

public function call(string $path = null)
{
    $language = $this->language;
    $kirby    = $language->kirby();
    $router   = new Router($this->routes());
    
    // Fix for translations of requested language
    $kirby->setCurrentTranslation($language);

    // Fix for current language
    $kirby->setCurrentLanguage($language);

    try {
        return $router->call($path, $kirby->request()->method(), function ($route) use ($language) {
            if ($page = $route->page()) {
                return $route->action()->call($route, $language, $page, ...$route->arguments());
            } else {
                return $route->action()->call($route, $language, ...$route->arguments());
            }
        });
    } catch (Exception $e) {
        return $kirby->resolve($path, $language->code());
    }
}

https://github.com/getkirby/kirby/blob/3.3.3/src/Cms/LanguageRouter.php#L113-L130

I have no idea about a general solution, but maybe the above implement can give you an idea.
I guess maybe the same problem might be in the API part as well.


Again, in order to give an idea, it is fixed by using the resolve() (includes requests that set the language and locale) method before.

public function call(string $path = null)
{
    $language = $this->language;
    $kirby    = $language->kirby();
    $router   = new Router($this->routes());
    
    $default  = $kirby->resolve($path, $language->code());

    try {
        return $router->call($path, $kirby->request()->method(), function ($route) use ($language) {
            if ($page = $route->page()) {
                return $route->action()->call($route, $language, $page, ...$route->arguments());
            } else {
                return $route->action()->call($route, $language, ...$route->arguments());
            }
        });
    } catch (Exception $e) {
        return $default;
    }
}
@neildaniels

This comment has been minimized.

Copy link
Contributor

@neildaniels neildaniels commented Feb 12, 2020

I'm running into this issue as well. The workaround of tt('test', null, [], $language->code()) isn't really practical in my situation so a permanent fix for this would be greatly appreciated.

In the meantime, I'm using @afbora discovery as so:

    [
        'pattern' => 'my/route',
        'language' => '*',
        'action' => function($language) {
            $kirby = kirby();
            // Workaround for https://github.com/getkirby/kirby/issues/2428
            $kirby->setCurrentTranslation($language);
            $kirby->setCurrentLanguage($language);
            
            return snippet('some/snippet', [], true);
        }
    ],
@distantnative distantnative added this to the 3.3.5 milestone Feb 15, 2020
@afbora afbora changed the title Translations issue with routing on multilang Translation and language issue with routing on multilang Feb 18, 2020
afbora added a commit that referenced this issue Mar 9, 2020
@afbora afbora self-assigned this Mar 9, 2020
bastianallgeier added a commit that referenced this issue Mar 9, 2020
@bastianallgeier

This comment has been minimized.

Copy link
Contributor

@bastianallgeier bastianallgeier commented Mar 9, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.