Welcome Guest, Not a member yet? Register   Sign In
Dynamically changing supported languages
#1

In the project I have developed, languages can be added or removed from the management panel.

How can I do I want to dynamically change the $supportedLocales in the app.php file.
Reply
#2

Same question for me too, did you find a solution? or does anyone have a solution to this?

Thanks
Reply
#3

Have you tried implicit registrars? I never tried it, but it looks like it’s used to override the global configuration programmatically: http://codeigniter.com/user_guide/genera...registrars
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#4

config("APP")->supportedLocales = [any lang array you need];
Reply
#5

Thanks to both of you for your answer, however the solution proposed by aramiss is the fastest and simplest to set the value on the fly, for the moment I think I will adopt this solution.

But I also discovered another way to solve my request, CodeIgniter 4 Settings (https://github.com/codeigniter4/settings) , maybe it's even better because it allows me to save the data, maybe in the future I will use this also for other purposes.
Reply
#6

I came back with another problem to ask you about this.

If I have for example only one locale in the App.php config file:
PHP Code:
public $supportedLocales=['en']; 

And I use the ARAmiss solution in BaseController.php:
PHP Code:
/**
 * Constructor.
 */
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        config("App")->supportedLocales = ['en','fr','es','it'];


I expect that if for example I open "https://mysite.com/fr"

service('request')->getLocale() returns "fr" to me instead it always returns "en".

I guess it's because getLocale() always reads the configuration file App.php and not the new value set in BaseController.php. So, not finding the language in the config file, it always offers me the default one.

There is a way to fix this problem. Is there a way to get around this or do I have to abandon the system adopted by codeigniter for the management of the locale and write me something new?

I hope I was clear in my explanation.

Thanks
Reply
#7

To do that your first need to set negotiateLocale to true to autodetect the language. But, even if you do this, the language is selected before hitting the controller, so maybe it won't work for unsupported languages. You probably need to create a filter to deal with this problem.

But I think this is working against the framework and will lead in other problems. I'm not sure what is the best solution here. I can't remember the name, but someone posted a project to store the configuration in the database to overwrite the config from the files. Maybe that would work.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#8

Thanks includebeer, but negotiateLocale don't help me in this case, for now I created my getLocale() function in BaseController.php, This allows me to partially solve the problem, I only have to solve it with the setLocale() function to be able to correctly load the language files under App/Language, but for me I think it is more difficult.

PHP Code:
protected function getLocale()
{
        
$defaultLocale Config("App")->defaultLocale;
        
$locales Config("App")->supportedLocales;

        if (
service('uri')->getTotalSegments() >= 1)
        {
            
$locale service('uri')->getSegment(1);

            if (
in_array($locale$locales))
            {
                return 
$locale;
            }
        }

        return 
$defaultLocale;



Maybe I could indicate in the config file some of the most used languages in my projects so that setLocale() works in most of my cases, for example ['de', 'en', 'pt', 'es', 'fr', 'it'].
In the end, however, the language and routes control is done based on the settings that I pass dynamically from the database. It's not the best but it might work.
Reply
#9

The settings is Lonnie's.

Lonnies - settings
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

Yes actually, I didn't say it, but then I also tried this way and I'm still using it, but getLocale() and setLocale() ignore it. Also because Settings does not overwrite the values of the config files, but allows you to store them in the database and use those stored in the files only if they do not exist in the database.

I have seen that if I change the value in the "system\HTTP\IncomingRequest.php" file constructor:

from

PHP Code:
$this->validLocales $config->supportedLocales

to

PHP Code:
$this->validLocales service('settings')->get('App.supportedLocales'); 

then it works fine as I expect, but I would like to avoid modifying the files under system folder.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB