CodeIgniter Forums
LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER (/showthread.php?tid=83410)



LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER - SubrataJ - 09-23-2022

I have a multilanguage site which was working fine until I had to create a filter for checking permission from the routes page,
this is my permission filter:- 
PHP Code:
public function before(RequestInterface $request$arguments null)
    {  
        
if (session()->get('isProviderLoggedIn') || Session()->get('isLoggedIn')) {
            helper('permission');
            if (is_admin() || permissions($arguments[0])) { 
                return true;
            } else if(is_provider() || permissions($arguments[0])){
                return true;
            } else {
                if (Session()->get('isLoggedIn')) {
                    return redirect()->route('admin-dashboard');
                } else if(session()->get('isProviderLoggedIn')) {
                    return redirect()->route('provider-dashboard');
                }
            }
        } else {
            return redirect()->to(site_url('admin'));
        }
    
this is how I am using this filter
PHP Code:
    $routes->get('admin/settings/roles/permission''RolesController::permission', ['filter' => 'permission:role_permission_view']); 

if I remove this filter part from routes, multilanguage is working fine, but with this filter, the locale is being set to 'en' by default.


RE: LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER - InsiteFX - 09-24-2022

Then there is something in your filter that is either blocking or over writing the local from being set.

Maybe your redirect is by passing the local.


RE: LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER - SubrataJ - 09-24-2022

(09-24-2022, 12:59 AM)InsiteFX Wrote: Then there is something in your filter that is either blocking or over writing the local from being set.

Maybe your redirect is by passing the local.

I am setting the locale from a very separate controller there is no way permission filter can be a reason, though I am not sure about the insights. 

Controller where I am setting locale-:
PHP Code:
class Language extends BaseController
{
    public function index()
    {
        $locale strtolower(substr($this->request->getLocale(), 02));
        $locale in_array($locale$this->request->config->supportedLocales) ? $locale $this->request->config->defaultLocale;
        $session session();
        $session->remove('lang');
        $session->set('lang'$locale);
        return redirect()->back();
    }


and locale filter to set locale
PHP Code:
public function before(RequestInterface $request$arguments null)
    {
        if (session()->has('lang'))
        {
            service('language')->setLocale(session('lang'));
        } else {
            // Save locale to session
            session()->set('lang'service('request')->getLocale());
        }
    



RE: LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER - InsiteFX - 09-25-2022

Most users set it in the BaseController. Here is IncludeBeer's Site that shows how they did it.

Creating a multilingual website with CodeIgniter 4 (part 1)

Creating a multilingual website with CodeIgniter 4 (part 2)