# Database demo, multi-language setup?

**URL:** <https://forum.getkirby.com/t/database-demo-multi-language-setup/17419>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 26, 2020, 11:55am UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419 "2020-02-26T11:55:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tristantbg](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tristantbg/32/9690_2.png) [@tristantbg](https://forum.getkirby.com/u/tristantbg)\
**Post date:** [February 26, 2020, 11:55am UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/1 "2020-02-26T11:55:35Z")

</div>

Thank you @texnixe for the Database demo :

> **[Content from a database](https://getkirby.com/docs/guide/virtual-pages/content-from-database)**
>
> Create pages from a database and edit, add or delete entries in the Panel

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 26, 2020, 2:34pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/2 "2020-02-26T14:34:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 26, 2020, 3:18pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/3 "2020-02-26T15:18:45Z")

</div>

I think the notation is different in multilang:

> <https://github.com/getkirby/kirby/blob/master/tests/Cms/Pages/PageTranslationsTest.php#L34-L50>

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 27, 2020, 9:28am UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/4 "2020-02-27T09:28:26Z")

</div>

@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).

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 27, 2020, 7:05pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/5 "2020-02-27T19:05:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon77445132](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@anon77445132](https://forum.getkirby.com/u/anon77445132)\
**Post date:** [February 27, 2020, 7:47pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/6 "2020-02-27T19:47:00Z")

</div>

@tristantbg:  
Think for [https://en.wikipedia.org/wiki/Database\_normalization](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…

---

<div class="post-metadata">

**Author:** ![tristantbg](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tristantbg/32/9690_2.png) [@tristantbg](https://forum.getkirby.com/u/tristantbg)\
**Post date:** [March 12, 2020, 5:06pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/7 "2020-03-12T17:06:37Z")

</div>

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 :

> <https://github.com/tristantbg/kirby-shopify/blob/master/src/models/shopify.products.php#L49>

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 12, 2020, 5:12pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/8 "2020-03-12T17:12:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tristantbg](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tristantbg/32/9690_2.png) [@tristantbg](https://forum.getkirby.com/u/tristantbg)\
**Post date:** [March 12, 2020, 5:28pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/9 "2020-03-12T17:28:13Z")

</div>

Thanks ! I didn’t know this inventory method !  
🔥

---

<div class="post-metadata">

**Author:** ![tristantbg](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tristantbg/32/9690_2.png) [@tristantbg](https://forum.getkirby.com/u/tristantbg)\
**Post date:** [March 17, 2020, 4:48pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/10 "2020-03-17T16:48:30Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![krisa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/krisa/32/3415_2.png) [@krisa](https://forum.getkirby.com/u/krisa)\
**Post date:** [March 7, 2022, 1:39pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/11 "2022-03-07T13:39:42Z")

</div>

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

[Content from a database | Kirby CMS](https://getkirby.com/docs/guide/virtual-pages/content-from-database#create-a-model-for-the-comment-child-page) mentions `readContent()` but the [kirby/ModelWithContent.php at 9ea05e38dbeb471657e3066651568fc29763ef06 · getkirby/kirby · GitHub](https://github.com/getkirby/kirby/blob/9ea05e38dbeb471657e3066651568fc29763ef06/src/Cms/ModelWithContent.php) 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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 7, 2022, 7:59pm UTC](https://forum.getkirby.com/t/database-demo-multi-language-setup/17419/12 "2022-03-07T19:59:53Z")

</div>

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](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/21)
