Language : why?

cakephp 3.x
I was surprised that my sites html pages language was :
html lang=“en”
Although ‘defaultLocale’ is set to :

Can it be considered some type of oversight or did I miss something ?

Shouldn’t it be an auto value ?

How does your default layout output the HTML tag?

html lang=“en”

Well then, not really much of a mystery as to why that’s ending up in your HTML, is it?

Oh ! We did not understand each other.
My html tag is written :
but ends up as
html lang=“en”

So, your layout just has “<html>” in it? Are you looking at the results with “view source” (or, equivalently, some command line tool like GET or wget or curl), or with developer tools?

Yes "<html>" only.
Looking ar the results with “view source” and developer tools.

What does your layout file look like?

<!DOCTYPE html>
<?= $this->Html->charset() . "\n" ?>
<?= $this->element('description') . "\n" ?>
<?= $this->Html->meta('viewport', 'width=device-width, initial-scale=1.0') . "\n" ?>
<?= $this->Html->meta('icon') . "\n" ?>
<?= $this->Html->meta('format-detection', 'telephone=no') . "\n" ?>
<?= $this->Html->css(['style', 'menu', 'medias', ',300,400']) ?>
	echo $this->Html->script("//") . "\n";
	echo $this->Html->script("//") . "\n";
	echo $this->Html->script("") . "\n";
	echo $this->Html->script("") . "\n";
<?= $this->fetch('meta') ?>
<?= $this->fetch('css') ?>
<?= $this->fetch('script') . "\n"; ?>
<?= $this->element('analytics') . "\n"; ?>
"<!-- content here -->"
"<!-- some js here -->"

Ok, well, CakePHP doesn’t alter/change the html unless you specifically have code for it (i.e. an echo). So, it must be your browser, network or server that’s adding it.

OK. Thank you for taking time to answer.
So I have to hard code the “lang” tag.

Pretty much :slight_smile:
Or build a little Html helper that you can call that outputs it for you (so you can change it based on the language of the user if applicable as well)

Using <html lang="<?= Configure::read('App.defaultLocale') ?>"> in your layout should work, I’d think.

Works after adding <?php use Cake\Core\Configure; ?> of course

but prints :
instead of :
Not sure if it’s right. According to W3C it should be “fr”.

Easily handled with substr

Of course.
But not too clean :wink:
And would it work with all languages ?

I believe so. The naming standard should mean that your language code is either xx or xx_YY. In either case, the first two characters would be what you want.

Alternatively what one could do (but I haven’t tested this yet) is get the locale in the AppController, substr it there and pass it to the template view.
This would minimize the PHP code needed in the template.

Well, to sum up the subject, I can do one of the following :

  • substr the defaultLocale in the html tag
  • create a helper that does the same
  • make an element that does the same

Thank you all of you who helped and Zuluru for the base idea.

Hope it helps other people.

You can also set the default to fr instead of fr_FR