Switching language does not switch locale

Switching language correctly switches URL and content but does not seem to switch locale.

This is my lang switcher echoed code in localhost:

<nav class="languages">
	<a href="http://localhost/~jaume/dev/femsa/es" hreflang="es">es</a> 
	<a href="http://localhost/~jaume/dev/femsa/en" hreflang="en">en</a>
</nav>

This is my config:

<?php 
return  [
	'hooks' => [
		'page.create:after' => function ($page) {
			buildPageTree($page);
		}
	],
	'languages' => true,
	'date.handler' => 'strftime',
	'panel' =>[
		'install' => false
	],
	'debug'  => true
];

And these are my language files

site/languages/es.php

<?php
return [
    'code' => 'es',
    'default' => true,
    'direction' => 'ltr',
    'locale' => 'es_MX',
    'name' => 'Español',
    'translations' => [
        'proxeventos' => 'PRÓXIMOS EVENTOS Y PROGRAMAS ',
        'artistasInvitados' => 'ARTISTAS invitados ',
        'curadoresInvitados' => 'CURADORES invitados ',
        'hoy' => 'hoy',
        'mañana' => 'mañana',
        'estaSemana' => 'esta semana',
        'proxSemana' => 'prox. week',
        'infoViaje' => 'información de viaje INFORMACIÓN DE VIAJE ',
        'sedes' => 'sedes ',
        'progPublico' => 'programa PÚBLICO ',
        'progPedagogico' => 'programa PEDAGÓGICO ',
        'aquiDescarga' => 'AQUÍ DESCARGA: ',
        'programa' => 'PROGRAMA',
        'convocatoria' => 'CONVOCATORIA',
        'fechasConfirmar' => 'Fechas por confirmar ',
        'sinEventos' => 'Sin eventos',
        'contacto' => 'CONTACTO',
        'siguenos' => 'SIGUENOS EN',
        'descargas' => 'DESCARGAS',
        'materialesPrensa' => 'Materiales de prensa',
        'avisoPrivacidad' => 'Aviso de privacidad',
        'suscribirse' => 'suscribirse'
    ],
    'url' => NULL
];

site/languages/en.php

<?php
return [
    'code' => 'en',
    'default' => false,
    'direction' => 'ltr',
    'locale' => 'en_US',
    'name' => 'English',
    'translations' => [
        'proxeventos' => 'UPCOMING PROGRAMS AND EVENTS ',
        'artistasInvitados' => 'guest ARTISTS ',
        'curadoresInvitados' => 'guest CURATORS ',
        'hoy' => 'today',
        'mañana' => 'tomorrow',
        'estaSemana' => 'this week',
        'proxSemana' => 'next week',
        'infoViaje' => 'trip information TRIP INFORMATION ',
        'sedes' => 'event locations ',
        'progPublico' => 'programa PÚBLICO ',
        'progPedagogico' => 'educational PROGRAM ',
        'aquiDescarga' => 'DOWNLOAD HERE: ',
        'programa' => 'PROGRAM',
        'convocatoria' => 'APPLICATION',
        'fechasConfirmar' => 'Dates to be confirmed ',
        'sinEventos' => 'No events',
        'contacto' => 'CONTACT',
        'siguenos' => 'FOLLOW US',
        'descargas' => 'DOWNLOADS',
        'materialesPrensa' => 'Press materials',
        'avisoPrivacidad' => 'Privacy notice',
        'suscribirse' => 'subscribe'
    ],
    'url' => NULL
];

When I click any of the links in the language switcher, URL and content switch accordingly but not the locale. The result of the following code:

   <?php echo setlocale(LC_ALL, 0); ?>

is…

   es_MX.utf8

…in both cases.

:thinking:

What may be going on ?

Thank you

Additionally, doing this:

        <?php setlocale(LC_ALL, 'en_US.utf8'); ?>
        <?php echo setlocale(LC_ALL, 0); ?>

…works… as in returns en_US.utf8 as locale on the second line

Furthermore the output of locale -a shows:

C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
es_AR.utf8
es_BO.utf8
es_CL.utf8
es_CO.utf8
es_CR.utf8
es_CU
es_CU.utf8
es_DO.utf8
es_EC.utf8
es_ES.utf8
es_GT.utf8
es_HN.utf8
es_MX.utf8
es_NI.utf8
es_PA.utf8
es_PE.utf8
es_PR.utf8
es_PY.utf8
es_SV.utf8
es_US.utf8
es_UY.utf8
es_VE.utf8
POSIX

So I tried to change the locales in the language files to en_US.utf8 and es_MX.utf8 respectively.

But that did not solve the problem, I still get es_MX.utf8 in both language urls.

Additionally I tried the same site in the remote server, with the same results, which leads me to think there is a config problem ?

The server’s locale -a relevant codes:

C
POSIX
...
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
...
es_MX
es_MX.iso88591
es_MX.utf8

Thank you

Looks like you din’t create the locales via the Panel? Because the Panel adds an array

  'locale' => [
        'en_US'
    ],

And then the locale turns up correctly, at least in my test.

Hmmm…

If I change the locale via the panel it writes it this way:

'locale' => [
    6 => 'es_MX.utf8'
],

'locale' => [
    6 => 'en_US.utf8'
],

Why is that so ? I recall finding that 6 and removing it.

Nevertheless even with that the situation is not resolved.

Is there any other language related config stored anywhere ?

Thank you @texnixe

Hm, yes, I remember there was something with this 6 thingy, but I don’t get it when I create languages via the Panel. Which Kirby version do you use? Wonder if that has either changed recently or if that is server-specific.

  • 3.3.1

I did the test with 3.3.2, but as I said, I don’t know if that makes a difference. But apart from getting the wrong locale when outputting it via phpsetlocale(), what is the real problem you are facing?

I cannot translate dates with strftime because, I assume, the locale is wrong

And adding the locale as array so that it looks like in my example above doesn’t change anything? Or setting LC_ALL explicitly in the array?

What I am trying to achieve is changing the date function to strftime in config, and auto-translating dates site-wide. How would I manually specify locale depending on the language the site is on ?

Sorry, I misread your last comment.

Using:

'locale' => [
    'en_US.utf8'
],

and

'locale' => [
     'es_MX.utf8'
],

while having

'languages' => true,
'date.handler' => 'strftime', 

In config.php

And doing this in the template

        <?= setlocale(LC_ALL, 0); ?>
        <?= strftime('%e %b') ?>

Results in

es_MX.utf8
23 ene 

…both in /en and /es

As mentioned content switches correctly, tho.

Let me try setting LC_ALL explicitly.

@texnixe, doing this:

'locale' => [
    LC_ALL  => 'es_MX.utf8'
],

and

'locale' => [
    LC_ALL  => 'en_US.utf8'
], 

…does not change anything…

And without utf8?

Nope, without utf8 neither.

I’ve just cloned starterkit * 3.3.3

Went into config, added

'languages' => true

Created primary and secondary language through panel, they are created like this:

en.php

<?php

    return [
        'code' => 'en',
        'default' => true,
        'direction' => 'ltr',
        'locale' => [
            6 => 'en_US.utf8'
        ],
        'name' => 'English',
        'translations' => [

        ],
        'url' => NULL

es.php

<?php

return [
    'code' => 'es',
    'default' => false,
    'direction' => 'ltr',
    'locale' => [
        6 => 'es_MX.utf8'
    ],
    'name' => 'Español',
    'translations' => [

    ],
    'url' => NULL

Added this code to the homepage

<?= setlocale(LC_ALL, 0); ?>
<?= strftime('%e %b') ?>

And it works. As in changing the language bit of the url (/en or /es) does show its right locale and the right output for strftime.

Then I went and added this to the config:

'date.handler' => 'strftime',

…still working.

Then I tried to add content to the secondary language, and still it works.

This would suggest again that some config is wrong right ?

Thanks

@texnixe
After many tests I discovered that this was ONLY happening in the homepage :man_facepalming:

And after a few more tests I discovered I had setlocale(LC_ALL, "es_MX.utf8") in a plugin’s index !!!

So, there was no problem with the language settings. It was working site-wide, but not where this plugin was being loaded.

I knew it was some hidden stupid thing like this, argh…

Thank you very much