CodeIgniter Forums
How to set default Locale {locale} on default redirected controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How to set default Locale {locale} on default redirected controller? (/showthread.php?tid=81851)



How to set default Locale {locale} on default redirected controller? - luckmoshy - 05-10-2022

Hi all! I have an app that works ok in different languages but want to filter and fix on home URL to append locale as
PHP Code:
/*Filter class*/
    /*I get too many redirect error message*/
        
$uri = &$request->uri;
        
$segments array_filter($uri->getSegments());
        
$checkSegments count($segments);

   
//  the first 2 letters (en => en)
        
$userLocale strtolower(substr($request->getLocale(), 02));

            
$locale in_array($userLocale$request->config->supportedLocales) ? 
                                         
$userLocale :
                                         
$this->request->config->defaultLocale;

       if if (
$checkSegments == 0)
        {
        return 
redirect()->to("/{$locale}");
        } 

I want if any incoming request should fall on eg: (www. example.com/en) and not (www. example.com)
PHP Code:
public function index(){
      return 
view('homepage');/*I want to fix in homepage should be appended with surport locale  = exapmple.com/{locale}*/
}
/* I get 404 error */
      
$routes->get('/{locale}''HomePage::index',['filter'=>'locales']); /*get 404 error*/ 



RE: How to set default Locale {locale} on default redirected controller? - demyr - 05-10-2022

Well, On Routes.php you can assign {locale} for the homepage and maybe this easy approach might give you idea for the rest:

PHP Code:
$lang $this->request->uri->getSegment(1); //this is the language code part.
        if($lang == ''){
            return redirect()->to(site_url('/en'));
        


So, it will automatically redirect to your default language, whatever it is..


RE: How to set default Locale {locale} on default redirected controller? - InsiteFX - 05-11-2022

IncludeBeer has a very nice tutorial on creating a Multi-Language web site for a Blog.

IncludeBeer Website