Database demo, multi-language setup?

Thank you @texnixe for the Database demo :

I am using it and it works very well but I would like to use it in a multilanguage setup. What are the best practices for that ?

Something like that ?

<?php

class CommentsPage extends Kirby\Cms\Page
{
    public function children()
    {
        $comments = [];

        foreach (Db::select('comments') as $comment) {
            $comments[] = [
                'slug'     => $comment->slug(),
                'num'      => 0,
                'template' => 'comment',
                'model'    => 'comment',
                'content.en'  => [
                    'text'  => $comment->textEn(),
                    'user'  => $comment->user(),
                ],
                'content.fr'  => [
                    'text'  => $comment->textFr(),
                    'user'  => $comment->user(),
                ]
            ];
        }

        return Pages::factory($comments, $this);
    }
}  

Thank you.

Hey @tristantbg, I was going to update the guide with a multilanguage example but haven’t got round to it yet nor even made my own tests.

Doesn’t the code you posted above work?

I think the notation is different in multilang:

1 Like

@tristantbg Did you actually work it out? I wonder if it really makes sense to use different fields for different languages or whether or not it would make more sense to add a language code column in the database and have one entry per language. Otherwise we would end up with a lot of duplicate fields (not in this example with few fields but likely IRL). Also, with such a setup it is easily possible to add additional languages without having to change the structure of the tables.

IMO, that would make it easier to update fields in different languages without accidentally updating the wrong field.

What do others think would be the best approach for multi-language content? @distantnative? @bastianallgeier? @lukasbestle? Anyone ever done this before in a real project?

Having said that, it probably depends on the database structure you start with (if you don’t set it up for the purpose).

For comments the suggestion to use a separate language code column and only one column for the text is probably the way to go. Multiple columns for each language only make sense if you are planning to translate the comments between the languages – not very common for comments (for other types of content it may make sense though).

So it all depends on the use-case and also, as @texnixe said, on existing DB structures etc.

@tristantbg:
Think for https://en.wikipedia.org/wiki/Database_normalization of your database!

This is important, if you want to use your database for a long term! Or ask your database architect…

Thanks a lot @distantnative it works perfectly !

Actually I just need to get some data from a database and then I merge the data with what I have in Kirby’s content.
For example the title of the page is coming from the SQL DB but the images of the page are inside Kirby.

What would be the best approach to merge the data ? Here is how I did it in my Shopify plugin :

Check out this section of the guide: https://getkirby.com/docs/guide/virtual-pages/merging-virtual-and-local-content

1 Like

Thanks ! I didn’t know this inventory method !
:fire:

Thank you for the inventory tip, but now that I have a lot of page items the panel is now very slow… I guess it is a bit like the index() method, the more you have the slower you get.
I switched back to my hack of getting the specific txt text file directly and it goes fast again but it is not very clean… maybe you’ll have a better idea ?

        $kirbyPageRootFr = $this->root() . '/' . $artist->id_artist() . '_' . $slug . '/artist.fr.txt';
        $kirbyPageRootEn = $this->root() . '/' . $artist->id_artist() . '_' . $slug . '/artist.en.txt';
        $kirbyPageFr     = F::exists($kirbyPageRootFr) ? \Kirby\Data\Data::read($kirbyPageRootFr) : false;
        $kirbyPageEn     = F::exists($kirbyPageRootEn) ? \Kirby\Data\Data::read($kirbyPageRootEn) : false;
        $contentFr = [
            'id_artist' => $artist->id_artist(),
            'title' => $title,
            ];
        if($kirbyPageFr) $contentFr = ($contentFr + $kirbyPageFr);

        $contentEn = [
            'id_artist' => $artist->id_artist(),
            'title' => $title,
        ];
        if($kirbyPageEn) $contentEn = ($contentEn + $kirbyPageEn);

Whats the right way how to read single page content from database in multi language context?

Content from a database | Kirby CMS mentions readContent() but the kirby/ModelWithContent.php at 9ea05e38dbeb471657e3066651568fc29763ef06 · getkirby/kirby · GitHub function content() that seems to be responsible for this seems to not use readContent() at all if you are in multi language site and if i rewrite content() its just does wierd errors.

The content() expects some sort of Translation object.

In my case i don’t even care about translations i just want to show show some same data in all languages.

You would define the content and translations in the children() method of the parent page, see for example: Multilang page restrict structur field - #21 by texnixe