Fallback in multi-language Site


#1

Hello there,

Maybe in my other post my problem is going under.

My multi-language setup works so far, my content structure:

product-xyz/product.de.txt (default)
product-xyz/product.en.txt
product-xyz/product.DE-de.txt
product-xyz/product.DE-en.txt
product-xyz/product.UK-en.txt

My problem so far:
If I’m on my second domain, which is for the German Market (*.de) should always use the following languages:

product.DE-de.txt
product.DE-en.txt

If there is no .DE-.txt it should fall back to *.de.txt or *.en.txt

On the third domain (UK market), it should use the following rule, if something doesn’t exist:
product.UK-en.txt > product.DE-en.txt > product.de.txt

I was trying to use a controller (site.php) for this:

$languages_code = array_column( c::get('languages'), 'code' );
$languages_pos  = array_search( $site->language()->code(), $languages_code );
$language_info  = c::get('languages')[$languages_pos];

if (!$page->content()->exists()) {
    if ($page->content($language_info['inherit'])->exists()) {
        $content = $page->content($language_info['inherit']);
    } else if ($page->content($language_info['backup'])->exists()) {
        $content = $page->content($language_info['backup']);
    }
} else {
    $content = $page->content();
}

I can pass $content to my template, but I have to re-write/re-organize every template/snippet so far what I try to avoid.

Is there any elegant solution for multiple fallbacks in a multi-language site?
Please let me know if you need more information about any part of my code.

Cheers,
frank


#2

What are you using in your templates to fetch the correct version? Passing $content doesn’t seem to be the best way to handle this, but I’d rather pass the correct language code and then in your template, use

$page->content($langCode)->title()

etc.

The only other option I see would be to use page models that overwrite the page methods you use in your templates, but that would probably result in as much work.


#3

I used the snippet from my first post (controller\site.php).
And in my templates for example:

<title><?= $content->get('Metatitle'); ?></title>

But your code should be an easier implementation. I will try this during the day.
Thanks.


#4

The downside of the default controller is that you can’t use specific controllers for your templates, which may or may not be necessary. But instead of putting the logic into your site.php controller, you could wrap your language logic into a function that you call in each controller, be it site.php (as your fallback controller) or a specific controller where needed.