CodeIgniter Forums
How to change base config dynamic? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How to change base config dynamic? (/showthread.php?tid=72587)



How to change base config dynamic? - ramstudio - 01-09-2019

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


RE: How to change base config dynamic? - titounnes - 01-09-2019

You can try like this.
Config('App')->foo = 'bar';


RE: How to change base config dynamic? - ramstudio - 01-10-2019

(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.


RE: How to change base config dynamic? - kilishan - 01-11-2019

@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?


RE: How to change base config dynamic? - ramstudio - 01-11-2019

(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.


RE: How to change base config dynamic? - kilishan - 01-11-2019

Looks like a workflow we didn't take into account then. Will have to look into that. Thanks.


RE: How to change base config dynamic? - ramstudio - 01-11-2019

(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.


RE: How to change base config dynamic? - kilishan - 01-11-2019

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.


RE: How to change base config dynamic? - kilishan - 01-11-2019

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.


RE: How to change base config dynamic? - ramstudio - 01-12-2019

(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.