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

Pipes localePage and localeUrl not working correctly #753

Open
blubcow opened this issue Jan 9, 2025 · 2 comments
Open

Pipes localePage and localeUrl not working correctly #753

blubcow opened this issue Jan 9, 2025 · 2 comments

Comments

@blubcow
Copy link

blubcow commented Jan 9, 2025

OctoberCMS v 3.7.8
Translate v 2.2.10

Pipes localePage and localeUrl do not work

1) Site Definition "CMS route prefix" must be set

Else the pipes don't return anything meaningful

2) When "CMS route prefix" is set, the url is wrong

When we open https://localhost/en/blog/post

{{ this.request.url|localeUrl('de') }}
{{ 'blog/post'|localePage('de') }}

will result in

https://localhost/de/en/blog/post
https://localhost/en/de/blog/post

Edit: Fix for localePage and Fix for localeUrl attached


Note: The documentation on https://octobercms.com/plugin/rainlab-translate is not up-to date. The section "Selecting a Language" from the README doc would be very helpful :)

@blubcow
Copy link
Author

blubcow commented Jan 10, 2025

Here is a solution the localePage pipe error. Should be working like a charm now.
Sorry for not creating a pull request.

File Translator.php line 180

public function getPageInLocale($name, $locale = null, $params = [])
    {
        $page = CmsPage::find($name);
        if (!$name || !$page) {
            return null;
        }

        if (!$locale || !Locale::isValid($locale)) {
            $locale = $this->defaultLocale;
        }

        $site = Site::getSiteForLocale($locale);
        if (!$site) {
            return null;
        }

        $router = new \October\Rain\Router\Router;
        $urlPattern = array_get($page->attributes, 'viewBag.localeUrl.'.$locale, $page->url);
        
        $newPath = trim($router->urlFromPattern($urlPattern, $params), '/');
        $newPath = $site->attachRoutePrefix($newPath);
        
        // Error ======== The Cms facade already has a url prefix - this has to be removed
        $cms = new Cms\Helpers\Cms();
        $cms->setUrlPrefix('');
        return $cms->url($newPath);
    }

This bug could also be solved using the Facade

{
        ...
        Cms::setUrlPrefix('');
        $url = Cms::url($newPath);
        Cms::setUrlPrefix(Site::getSiteForLocale($this->activeLocale)->route_prefix);
        
        return $url;
}

@blubcow
Copy link
Author

blubcow commented Jan 10, 2025

And here is the fix for the localeUrl pipe

File Translator.php line 155

public function getPathInLocale($path, $locale = null)
{
        if (!$locale || !Locale::isValid($locale)) {
            $locale = $this->defaultLocale;
        }

        $site = Site::getSiteForLocale($this->activeLocale);
        if (!$site) {
            return $path;
        }

        $newPath = $site->removeRoutePrefix($path);
        return $locale . $newPath;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant