Jump to content

Featured Replies

Posted

Hello all! I'm building my own language switcher because i'd like my switcher to be added the mobile user menu. I have got it working, it switches to the language that I click on. But now I have ran into the issue that when you are on a page like "/contact/" you'd still be redirected to the homepage.

I have tried a lot and I can't seem to get it working. Can anybody with more experience point me into the right direction?
Much appreciated, thanks!

/**
 * @brief	UserMenu extension: languageSwitcher
 */
class languageSwitcher extends MenuExtension
{
    /**
     * Modify the account menu by adding language switcher items.
     *
     * @param string $position Position of the menu (content, settings, logout)
     * @return array<MenuItem> 
     */
    public function mobileMenu( string $position = 'content' ): array
    {
        $menuItems = [];
    
        if ($position === 'content') {
            $currentUrl = \IPS\Request::i()->url(); // Get the current page URL

            // English language switch link
            $englishUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=1', 'front')
                ->csrf(); // Add CSRF token
    
            $englishLink = new \IPS\Helpers\Menu\Link( 
                $englishUrl, 
                'English', 
                'ipsMenu_item' 
            );
            $menuItems[] = $englishLink;
    
            // Dutch language switch link
            $dutchUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=2', 'front')
                ->csrf(); // Add CSRF token
    
            $dutchLink = new \IPS\Helpers\Menu\Link( 
                $dutchUrl, 
                'Dutch', 
                'ipsMenu_item' 
            );
            $menuItems[] = $dutchLink;
        }
    
        return $menuItems;
    }
     



Solved by Daniel F

Go to solution
  • Solution
$dutchUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=2', 'front')
                ->csrf() // Add CSRF token
				->setQueryString( 'ref', base64_encode( $currentUrl ) ); // set ref url

should do it.

You can always add the 'ref' parameter to an url which contains the base64 encoded target URL.

 

Much appreciated, thanks!

So you added this to your code:

 
$currentUrl = \IPS\Request::i()->url(); // Get the current page URL

And never used anymore? You must find that code is useful but didn't tried to explore further? :)

  • Author

Thanks @Daniel F, this got it working :).

@DawPi At first I had something like the code below there, but this didn't work so I removed it, but forgot the remove $currentUrl. That's why haha.

->setQueryString( 'return', base64_encode( $currentUrl ) );

Recently Browsing 0

  • No registered users viewing this page.