URL of the other language

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?

Best,

Nedim

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.

With only two languages you can do it like this:

$languageCode = $kirby->defaultLanguage() ? 'de' : $kirby->defaultLanguage()->code();
echo $languageCode

This assumes that your default language is Turkish.

You can use like that:

<?php echo $page->url('tr'); ?>

But doesnā€™t $languageCode point to the code? I know it is not correct, but shouldnā€™t it be something like:

<?= $languageUrl = $kirby->defaultLanguage() ? 'de' : $kirby->defaultLanguage()->code(); echo $languageUrl ?>

It was supposed to be use with

<?php $page->url($languageCode) ?>

But then I only have one language. I want

<?php echo $page->url('tr'); ?>

on the German page and

<?php echo $page->url('de'); ?>

on the Turkish page :slight_smile:

So you can use in $kirby->languages() loop. This is only how it works.

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.

Turkish is default:

$languageCode = $kirby->defaultLanguage() ? 'de' : $kirby->defaultLanguage()->code();

German is default:

$languageCode = $kirby->defaultLanguage() ? 'tr' : $kirby->defaultLanguage()->code();

Sample piece of codes that support unlimited languages :slight_smile:

<?php foreach($kirby->languages()->not($kirby->language()) as $language): ?>
    <a href="<?php echo $page->url($language->code()); ?>">
        <?php echo $language->code(); ?>
    </a>
<?php endforeach; ?>

I already suggested that in another threadā€¦ :woman_shrugging: But with only two languages you donā€™t really need the loop.

As regard the second question:

You can check if a translation file exists with:

<?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.

Since I wanted to have the language code capitalized (i.e. ā€žTRā€œ or ā€žDEā€œ) I created new strings in the language php files:

In the German file:

ā€˜languagecodecapitalizedā€™ => ā€˜TRā€™,

In the Turkish file:

ā€˜languagecodecapitalizedā€™ => ā€šDEā€˜,

and that works perfect:

<span class="text-sans-serif ls font-weight-black fs-0">
<?php echo t('languagecodecapitalized') ?></span></a>`

I tried the same thing for the url code:

In the German file:

ā€˜languagecodecapitalizedā€™ => ā€štrā€˜,

In the Turkish file:

ā€˜languagecodecapitalizedā€™ => ā€šdeā€˜,

That does not work :frowning:

I am not good at php :frowning:

I tried:

<a class="fancynavbar-addon-item" href="
<?php $page->url(<?= $languageUrl = $kirby->defaultLanguage() ? 'trā€˜ : $kirby->defaultLanguage()->code(); echo $languageUrl ?> ) ?>">

And that breaks :frowning:

  1. You donā€™t need a language variable just to capitalize a string:

    $string = 'abc';
    echo Str::upper($string);
    

    will do the job.

  2. Issue

    <?php $languageCode = $kirby->defaultLanguage() ? 'tr' : $kirby->defaultLanguage()->code(); ?>
    
    <a class="fancynavbar-addon-item" href="<?= $page->url($languageCode)?>">...</a>
    

SUPER! That works!

Regarding:

<?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?

Best,

Nedim

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.

This is a typical page with the language selection:
https://nedimhazar.de/tigris-rebellen

This is a non typical page without a translation:
https://nedimhazar.de/singen-mit-zeki-mueren

On this page, the language selection is not displayed but is still there :slight_smile: with a ā€œ#ā€ link

As I said: the website is still in draft. I am trying to put it into a cms framework so that

  • updating static pages will be easier in the future
  • I will have blogs or news in the said languages

Best,

Nedim

1 Like

Dear texnixe,

incredible! But:

<?php $languageCode = $kirby->defaultLanguage() ? 'tr' : $kirby->defaultLanguage()->code(); ?> <a class="fancynavbar-addon-item" href="<?= $page->url($languageCode)?>

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. :slight_smile:

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.