I have a simple language switch, copied from here. When im on the homepage and switch from german to english, english is not shown anymore. The homepage is translated and the file has all needed permissions.
The strangest thing: I have two switches for the language (I know its bad, but the design is made like that) and the second language switch works (meaning even when english is selected, it gets marked as “active” and is still showing up).
However there is no difference between them as they use the same snippet and when I remove the first language switch so there is just one, this one will also remove the english li element when i switch to english. PS: No Dom manipulation is happening, so i guess it’s a problem within php.
Here is my language switch snippet:
<nav class="languages hide--<?= $view ?>" id="languages">
<ul>
<?php foreach($kirby->languages() as $language): ?>
<li class="<?php e($kirby->language() == $language, 'active') ?>">
<a href="<?= $page->url($language->code()) ?>"><?= html($language->name()) ?></a>
</li>
<?php endforeach ?>
</ul>
</nav>
Is the li really missing in the source? What does the hide--<?= $view ?>
styling do?
the hide--
class hides the hole navigation depending on mobile or desktop environment. As i said, its nothing too beautiful.
Yes, the english <li>
element is completely gone in the source. It’s not just hidden by any display:none
code, it’s not shown in the DOM at all, when i look at it with the dev console.
Is the site online? I’d say it has nothing to do with the PHP code, but sounds more like a JS thingy.
Yes, Here you’ll find (link removed) a test where stripped out all other stuff (no JS in here as you see).
What does your language configuration look like? And what is the Kirby version you are using?
It’s Kirby Version 3.1.1, i think it’s the newest, right? Before this update, it didn’t work as well, though.
config.php:
<?php
return [
'debug' => true,
'languages' => true,
'languages.detect' => false,
'markdown' => [
'extra' => true
]
];
de.php:
<?php
return array (
'code' => 'de',
'default' => true,
'direction' => 'ltr',
'locale' => 'de_DE',
'name' => 'Deutsch',
'url' => '/',
);
en.php:
<?php
return array (
'code' => 'en',
'default' => false,
'direction' => 'ltr',
'locale' => 'en_US',
'name' => 'English',
'url' => 'en'
);
(/en
didn’t work as well)
The latest release is 3.1.2, so maybe you could update first and then test again.
Some the second language doesn’t seem to be recognized.
What happens if you do a
dump($kirby->languages());
Your URL for English should definitely be /en
with the slash.
Updated to 3.1.2, changed to “/en” again, nothing changes.
dump($kirby->languages());
gives an interesting output thought - even when changing to english and just the english li
element is shown, this gives:
Kirby\Cms\Languages Object
(
[0] => de
[1] => en
)
Ok, stripped out every php output, and this is the problem here, what removes the english entry when changing to english:
<?= $page->url($language->code()) ?>
I just tested this again with a fresh Starterkit and still can’t reproduce your issue.
Have you verified this with a fresh Starterkit?
I played a lot arround and noticed that the problem occurs on my side, when you delete the following line in the starterkit from the header.php:
<?= css(['assets/css/index.css', '@auto']) ?>
That sounds even more weird, because all that line does is add the stylesheet. If I remove that line, my language switcher is still there but no styles.
Yeah it’s very strange to me as well… So what i am doing:
- Unpack starter kit
- add the following files to
site/languages/
:
de.php:
<?php
return [
'code' => 'de',
'default' => true,
'direction' => 'ltr',
'locale' => 'de_DE',
'name' => 'Deutsch',
'url' => '/',
];
en.php:
<?php
return [
'code' => 'en',
'default' => false,
'direction' => 'ltr',
'locale' => 'en_US',
'name' => 'English',
'url' => '/en'
];
- Add the following lines to the
header.php
snippet, right under the body
tag:
...
<body>
<nav class="languages" id="languages">
<ul>
<?php foreach($kirby->languages() as $language): ?>
<li class="<?php e($kirby->language() == $language, 'active') ?>">
<a href="<?= $page->url($language->code()) ?>"><?= html($language->name()) ?></a>
</li>
<?php endforeach ?>
</ul>
</nav>
<div class="page">
...
- Remove this line from header.php:
<?= css(['assets/css/index.css']) ?>
PS: Using LAMP stack.
PPS: This problem happens on the /home page and the starter example /photography page, at the subpage /photography/animals it works actually.
If you add your languages manually like that (instead of through the Panel), the text files in your content folder are not modified with the language extension. Maybe that’s the problem?
1 Like
Nope. The problem occurs both when I remove these files from my existing project and add them by admin panel and when I start over again from starter kit and add languages by admin panel… 
Sounds as if there’s something wrong with your configuration. As I said I can’t for the life of me reproduce this.
Can you reproduce it using this (link removed) kirby installation?
Yes, and I also found that it happens when the language switch is located like in your test before the navigation while it doesn’t happen with the language switch located after the navigation where I put it in my test.
But I have no idea what’s happening here.
Okay, I’m happy that it’s not because im too stupid to make the configurations… 
So… will we treat that as a bug? As i said, besides the navigation, the css() function also seems to fix that, when it’s before the languages foreach loop…