mrtn
1
I am using the following code within <head>
to set a few hreflang links:
<?php foreach($kirby->languages() as $language): ?>
<link
rel="alternate"
hreflang="<?php echo html($language->locale(LC_ALL)); ?>"
href="<?= $page->url($language->code()); ?>" >
<?php endforeach; ?>
When I validate my site through the W3C Validator, this throws an error:
Bad value en_us
for attribute hreflang
on element link
: The language subtag en_us
is not a valid language subtag.
For my understanding the hreflang
attribute has to be a ISO 639-1 compliant value.
Is there a way to achieve this?
stffr
2
I think it should be “en-US” instead of “en_us”
mrtn
3
Is there a way to return the correct value directly or do I have to convert the returned values from $language->locale(LC_ALL)
myself?
Do you need the explicit region? The hreflang attributes accepts both simple languade codes like de
, en
etc. as well as en-gb
, en-us
etc.
For the simple code, you can simply use $language->code()
.
2 Likes
mrtn
5
This seems to be a great solution, thanks!
I built a slightly different solution that seems to work at least for the language my site supports:
$languageCode = strtolower(str_replace("_", "-", $language->locale(LC_ALL)));
All the best, Martin