CodeIgniter Forums

Full Version: How to change base config dynamic?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to change base config dynamic?
In Ci3 If you would like to dynamically set a config item or change an existing one, you can do so using:

Code:
$this->config->set_item('item_name', 'item_value');

Can I dynamically set the controller to Ci4? 
Example: $defaultLocale, $supportedLocales, $appTimezone
You can try like this.
Config('App')->foo = 'bar';
(01-09-2019, 09:32 AM)titounnes Wrote: [ -> ]You can try like this.
Config('App')->foo = 'bar';

Or a bug, or I do not understand.
Lang added

[Image: lang.png]
app/congig/App.php
Code:
public $defaultLocale = 'en';
public $negotiateLocale = false;
public $supportedLocales = ['en', 'ru'];
 
in controller
Code:
public function index()
{
    
   Config('App')->negotiateLocale  = true;
   Config('App')->defaultLocale = 'ru';
       
       $data = array(
           'title' => 'Blog data array',
           'heading' =>  lang('app.secLine'),
       );
       
       echo view('header', $data);
       echo view('welcome', $data);
       echo view('footer', $data);
    
}

in views 
Code:
<?=  lang('app.firstLine');  ?>

With language file takes from the English.
@ramstudio what's your Browser language set to? The negotiation feature works with the browser to detect the language it should use. It's possible that if you have your browser language set to English than it's negotiating the language and setting it to English, even though the sites default is Russian.

What happens if you either turn off negotiateLocale or take English out of the supportedLocales?
(01-11-2019, 08:40 AM)kilishan Wrote: [ -> ]@ramstudio what's your Browser language set to? The negotiation feature works with the browser to detect the language it should use. It's possible that if you have your browser language set to English than it's negotiating the language and setting it to English, even though the sites default is Russian.

What happens if you either turn off negotiateLocale or take English out of the supportedLocales?

In Config\App.php
Code:
public $defaultLocale = 'en';
public $negotiateLocale = false;
public $supportedLocales = ['en','ru'];

In Controller\Home.php
Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\Config\BaseConfig;

class Home extends Controller
{
   public function index()
    {
    
   Config('App')->defaultLocale = 'ru';
   
   $cfg = Config('App')->defaultLocale = 'ru';
   
       $data = array(
           'title' => 'Blog data array',
           'heading' =>  lang('app.secLine'),
           'locale' =>  $cfg,
       );
       
       echo view('header', $data);
       echo view('welcome', $data);
       echo view('footer', $data);
     
   }

In Views\welcome.php
Code:
<?php
echo $tmp;
echo '<br/>';
echo $heading;
?>        

The output comes from the English language file.
Although the controller Set Russian.

$negotiateLocale When you change the controller true or false - not working.
$negotiateLocale When you change the config/App.php true or false - working.
Looks like a workflow we didn't take into account then. Will have to look into that. Thanks.
(01-11-2019, 01:14 PM)kilishan Wrote: [ -> ]Looks like a workflow we didn't take into account then. Will have to look into that. Thanks.

Thank you very much for your reply.
I look forward to the release of Codeigniter 4.
I've created an issue for this over at GitHub for us to investigate.

However, I did have one thought. I believe the language negotiation is happening prior to the controller being ran, which means it's too late at that point. If you change that dynamically in a pre system event listener it might work there.

I don't have any time to investigate until later but that might get you up and running.
Doh! I got to thinking about this after the previous posts and remembered that the request object has a setLocale() method. So, from your controllers you should be able to simply do:

Code:
$this->request->setLocale('ru');

As long as that is set prior to using a language string I believe it should do what you're looking for.
(01-11-2019, 11:40 PM)kilishan Wrote: [ -> ]Doh! I got to thinking about this after the previous posts and remembered that the request object has a setLocale() method. So, from your controllers you should be able to simply do:

Code:
$this->request->setLocale('ru');

As long as that is set prior to using a language string I believe it should do what you're looking for.

This is even better than I could have imagined. Thank you so much.