CodeIgniter Forums
Change the language dynamically? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Change the language dynamically? (/showthread.php?tid=79120)



Change the language dynamically? - goobydale - 04-22-2021

I am attempting to change the system language dynamically with a call to a controller method. I have in the view :

PHP Code:
<?php if ($this->config->item('language') == "francais") : ?>
<a href="changelangue?lang=english" data-js-langue>EN</a> 
<?php elseif ($this->config->item('language') == "english"):?>
<a href="changelangue?lang=francais" data-js-langue>FR</a>
<?php endif;?>

And in the controller I have this method:

PHP Code:
public function changeLangue(){

    if (!isset(
$_GET['lang']))
    {
        
redirect("vehicules");
    }

    
$this->config->set_item('language'$this->input->get('lang'));
    
redirect("vehicules");


I am being redirected, but the config language is not changing. It seems to always revert to the default language. I even tried changing it in the ' __construct' of the controller in question using the session userdata like so:

PHP Code:
public function __construct()
{
    
parent::__construct();
    if (!
$this->session->userdata('language'))
    {
        
$this->session->set_userdata('language'$this->config->item('language'));
    }else{
        
$this->config->set_item("language"$this->session->userdata('language'));
    }


I have the language folders in system/language and in language.

Am I missing something?


RE: Change the language dynamically? - InsiteFX - 04-22-2021

Working With Locales


RE: Change the language dynamically? - goobydale - 04-23-2021

(04-22-2021, 08:40 PM)InsiteFX Wrote: Working With Locales
Hey thanks for your answer! I should have mentioned I am on 3.1.11. Unless someone officially rebukes it, there's no way to set the language in the config with it having a default value set up and redirecting from with redirect().


RE: Change the language dynamically? - paliz - 04-23-2021

go  app/config/app.php 
chane like bellow 

PHP Code:
public $defaultLocale 'en';

    
/**
     * --------------------------------------------------------------------------
     * Negotiate Locale
     * --------------------------------------------------------------------------
     *
     * If true, the current Request object will automatically determine the
     * language to use based on the value of the Accept-Language header.
     *
     * If false, no automatic detection will be performed.
     *
     * @var boolean
     */
    
public $negotiateLocale true;

    
/**
     * --------------------------------------------------------------------------
     * Supported Locales
     * --------------------------------------------------------------------------
     *
     * If $negotiateLocale is true, this array lists the locales supported
     * by the application in descending order of priority. If no match is
     * found, the first locale will be used.
     *
     * @var string[]
     */
    
public $supportedLocales = ['en','fa']; 


for change  language  add header Accept-Language = En or ....
you can see i add header for angular code
 request.clone({headers: request.headers.set('Accept-Language',  'en')});


RE: Change the language dynamically? - InsiteFX - 04-23-2021

For CodeIgniter 3 read this one.

Enable Multi Language Capability in CodeIgniter