Language : why?

#1

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

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

Shouldn’t it be an auto value ?

0 Likes

#2

How does your default layout output the HTML tag?

0 Likes

#3

!DOCTYPE html
html lang=“en”

0 Likes

#4

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

0 Likes

#5

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

0 Likes

#6

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?

0 Likes

#7

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

0 Likes

#8

What does your layout file look like?

0 Likes

#9
<!DOCTYPE html>
"<html>"
"<head>"
<?= $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', 'https://fonts.googleapis.com/css?family=Roboto:100,300,400']) ?>
<?php
	echo $this->Html->script("//code.jquery.com/jquery-1.11.3.min.js") . "\n";
	echo $this->Html->script("//code.jquery.com/ui/1.11.4/jquery-ui.min.js") . "\n";
	echo $this->Html->script("https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js") . "\n";
	echo $this->Html->script("https://unpkg.com/imagesloaded@4/imagesloaded.pkgd.min.js") . "\n";
?>
<?= $this->fetch('meta') ?>
<?= $this->fetch('css') ?>
<?= $this->fetch('script') . "\n"; ?>
<?= $this->element('analytics') . "\n"; ?>
"</head>"
"<body>"
"<!-- content here -->"
"</body>"
"<!-- some js here -->"
"</html>"
0 Likes

#10

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.

0 Likes

#11

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

0 Likes

#12

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)

0 Likes

#13

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

0 Likes

#14

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

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

0 Likes

#15

Easily handled with substr

0 Likes

#16

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

0 Likes

#17

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.

0 Likes

#18

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.

0 Likes

#19

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

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

Hope it helps other people.

0 Likes

#20

You can also set the default to fr instead of fr_FR

0 Likes