I am looking for a code which would create the url of a page in the other language.
I have a site with two languages and would like to link e.g. the German page to the Turkish page and vice versa.
I came across this pice of code:
<?= $site->language('tr')->url() ?>
which I would use with the page parameter:
<?= $page->language('tr')->url() ?>
But then how can I code it so that the ‘tr’ changes to ‘de’ when I’m on the Turkish page?
Might it be be possible to translate the string in the languages templates?
Follow-up question: If there is no translation, if the particular page is only in one language, I need to add an inline css “display:none”. Otherwise the layout breaks. Unfortunately I can’t change the design anymore.
Is there a way to code that?
I mean instead of directing to the error page. I have already seen such a possibility on the forum.
But you said you wanted it to swap, that is what my code example does. If you are on the German page, it shows “tr” and vice versa. You have to change “de” to “tr” though, if Turkish is NOT the default language.
<?php if ($page->translation()->exists()) : ?>
<!-- do something -->
<?php endif ?>
This will always return true for the default language (because the Panel always creates a content file in the default language). It will also return true if the non-default languages are only partly translated, for example, only the title, so that a content file in that language is created.
<?php if ($page->translation()->exists()) : ?>
<?php endif ?>
You say, the panel always creates a content file in the default language.
Momentarily, for my needs (inline css “display:none” for a none-existing translation) I can cope with things manually (i.e. create a different template) since there are 2 pages only in Turkish and four pages only in German.
However in the coming few months I will have different dynamic blogs in both languages, with multiple pages. Then it might be wiser to install Kirby in subfolders for each language for the blogs. Or what do you suggest?
No, you can use a toggle field and manually set this field to true or false depending on whether or not you regard is as translated or not, and then in your template check for that field value instead of if the translation exists.
I don’t understand this display: none thingy. Are you displaying these side by side?
The draft website is already online in flat html/css. For the robots it is a “nofollow, no index” site.
Have a look on the bottom right. If I take out the TR or DE the whole navigation breaks. Having spent days over days on the design; I don’t want to change it anymore.
works only once. I mean, you click from one language page to the other - that works. If you want to click back the same way using the language switch, it stays on the same page.
I’m still working locally. I will upload the Kirby version of my site sometime next week on to a test folder in the remote server. Perhaps it might be better if I bring up the issue again then.