Bilingual Kirby

Bilingual Kirby

Maybe some of you have noticed, I am trying to write bilingual. So I wrote some helpers for that.

My last posts here could be read in english (surprise!), too. Although my written english is a bit rusty, I will keep translating my posts in the future and will try to translate the older ones step by step.

I am not that enthusiastic by the possibilites which come with Kirby, but it works, as you can see. I wrote a little snippet to switch languages and added a translation-detection I am using in my blog-header.

The problem is, Kirby will show the german text under a /en/-URL if there is no translated file available. I don't want that. You should not be able to click on the link for a translation which isn't there. So I modified the default-code for switching languages a little bit.

So it works better (for me)

First I try to figure out, which template is used by the current article. The templatename matches the first part of the filename. With that information I can look for a translation of the text. If a translation is available the language-switch-link is clickable, if not, the link is not clickable.

Have a look:

<?php foreach(c::get('lang.available') as $lang) : ?>
    <?php if($lang != c::get('lang.current')) : ?>
        <?php if($page->files()->find($page->template().'.'.$lang.'.md')) : ?>

To prevent manual modifications of the URL from "de" to "en" I am using a translation-detection in the header-file. If I would not do so, you could change "de" to "en" in the URL, but you would see the german text, if no translation is available. And I don't want you (and searchengines) to do so.

<?php
    $lang = c::get('lang.current');
    $other = ($lang == 'de') ? 'en' : 'de';
    $template = $page->template();
    if($page->files()->find($template.'.'.$lang.'.md') == NULL) {
        header ('HTTP/1.1 303 See Other');
        header('Location: '.$page->url($other));
    }
?>

First I look which language is choosen and see what its counterpart is. Then again the test if a translation is available. If not, I am redirecting to the german version (which changes the URL).

I am not sure, if the 303-header is really correct, but it semed the best solution for me. This whole solution only works, if you are only bilingual. If you would like to support more than two languages you need to modify it.

My snippets can, as always, be found at GitHub.

0
0
blog comments powered by Disqus