Translation and config.php Bug?

Hello,

In an option page, I’ve set a select field to let the user choose the language by default between ‘en’ and ‘fr’.

I get this value in my config file to set the default language.

This is the code in the config.php file but I’ve got a blank page on the front.

$language = 'en';
$languages = array();


$language = site()->generalOptionslanguage()->value();
//$language = 'fr';


switch ( $language ) {
  case 'en':
    $languages = array(
      array(
        'code'    => 'en',
        'name'    => 'English',
        'default' => true,
        'locale'  => 'en_EN',
        'url'     => '/',
      ),
      array(
        'code'    => 'fr',
        'name'    => 'French',
        'default' => false,
        'locale'  => 'fr_FR',
        'url'     => '/fr',
      ),
    );
    break;

  case 'fr':
    $languages = array(
      array(
        'code'    => 'en',
        'name'    => 'English',
        'default' => false,
        'locale'  => 'en_EN',
        'url'     => '/en',
      ),
      array(
        'code'    => 'fr',
        'name'    => 'French',
        'default' => true,
        'locale'  => 'fr_FR',
        'url'     => '/',
      ),
    );
    break;
  
}

c::set( 'languages' , $languages );

If I comment the line:

$language = site()->generalOptionslanguage()->value();

… and replace it by:

$language = 'fr';

It works fine.

I’ve var_dump the line “site()->generalOptionslanguage()->value();” and it return ‘fr’ or ‘en’ as expected.

Is it a bug?

Hm, I think you need to set the languages first. You could then try to change the default language depending on the value of the site setting.

Edit: I’m not so sure this is intended usage. If in doubt, you could file an issue on GitHub.

It seems that this code $language = site()->generalOptionslanguage()->value(); only gets the value from the default language. So you would have to define the default language first.

The multi-language features are only available after the configuration, extensions, plugins and the router have been loaded.

I tried to define the default language first and then get the site option value:

$default_language = array(
				array(
			        'code'    => 'fr',
			        'name'    => 'French',
			        'default' => true,
			        'locale'  => 'fr_FR',
			        'url'     => '/',
				),
				array(
					'code'    => 'en',
					'name'    => 'English',
					'locale'  => 'en_EN',
					'url'     => '/en',
				),
			      
			);

c::set( 'languages' , $default_language );


$default_language = kirby()->site()->generalOptionslanguage()->value()';



switch ( $default_language ) {

  case 'en':
    $languages = array(
			      array(
			        'code'    => 'en',
			        'name'    => 'English',
			        'default' => true,
			        'locale'  => 'en_EN',
			        'url'     => '/',
			      ),
			      array(
			        'code'    => 'fr',
			        'name'    => 'French',
			        'locale'  => 'fr_FR',
			        'url'     => '/fr',
			      ),
			    );
  break;

  case 'fr':
    array(
				array(
			        'code'    => 'fr',
			        'name'    => 'French',
			        'default' => true,
			        'locale'  => 'fr_FR',
			        'url'     => '/',
				),
				array(
					'code'    => 'en',
					'name'    => 'English',
					'locale'  => 'en_EN',
					'url'     => '/en',
				),
			      
			);
  break;
  
}


c::set( 'languages' , $languages );

But it doesn’t work.

Well, if you think about it, switching can only work if you define “en” as generalOptionsLanguage in the French site.txt file and vice versa. That’s because the value is taken from the default language.

Edit: I think what I would try to do is to create a dashboard widget that saves the language configuration to an external file and then read that file in the config.

But the safer way would be to give the users a preconfigured installation instead if trying to automate it. After all, you don’t know in advance, how many and which languages they need, which locales of a given language they want to use etc.