# Set current language of virtual page

**URL:** <https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 11, 2019, 4:39pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636 "2019-12-11T16:39:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvan](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/silvan/32/8097_2.png) [@silvan](https://forum.getkirby.com/u/silvan)\
**Post date:** [December 11, 2019, 4:39pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/1 "2019-12-11T16:39:03Z")

</div>

Is there a way to set the current language when I return a virtual page (using `new Page()`) in a route? Right now all content in the template is returned in the default language.

By the way: I struggle a lot with getting a multi language site to work. I wish there was a ‘language starter kit’—or does something like that exist?

As far as I understand it, as soon as you start using custom routes, a lot needs to be hand-coded in order for the languages to work properly.

For example do I not understand the concept of the “language scope”, especially when the `'language'` of the route is set to wildcard / asterisk. In my eyes it does nothing but handing the current language as a parameter to the `'action'` method. I would have hoped that it would set the current language for all kirby methods like `page()`, just as it happens in a template.

As always: 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:** [December 12, 2019, 6:03am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/2 "2019-12-12T06:03:17Z")

</div>

How do you create the virtual page, could you please post all relevant code?

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [December 12, 2019, 7:05am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/3 "2019-12-12T07:05:23Z")

</div>

I got it working, but I remember having wrestled through a lot as well.

The gist is you need a `translations` key in the `$options`-array you feed in the `new Page($options)`. This one contains another array with the slugs of the translated versions of your current virtual page like this:

```auto
$translations = [
    "nl" => "mijn-eerste-blog-post",
    "en" => "my-first-blog-post"
]

```

* * *

To set the language, I have this at the top of the function that handles this route:

```auto
site()->visit(site()->homepage(), $lang);

```

Where `$lang` is coming in as parameter from the route.

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [December 12, 2019, 7:11am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/4 "2019-12-12T07:11:29Z")

</div>

I don’t know if this is the _"best way_" to cope with this (as it dates from the beta iirc), but it works for me.

---

<div class="post-metadata">

**Author:** ![silvan](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/silvan/32/8097_2.png) [@silvan](https://forum.getkirby.com/u/silvan)\
**Post date:** [December 12, 2019, 9:04am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/5 "2019-12-12T09:04:54Z")

</div>

This is what I use:

```
[
  'pattern' => ['sammlungen/(:any)',
                'collections/(:any)'],
  'language' => '*',
  'action' => function($language, $title) {
    $slug = 'projekte/sammlungen/' . $title;
    $collection = page($slug);

    if ($collection) {
      $title = $collection->content($language)->title();
      $selection = $collection->content($language)->selection();

      // return virtual page based on template
      return new Page([
        'slug' => $slug . '/' . $index,
        'template' => 'project',
        'content' => [
          'title' => $title,
          'selection' => $selection
        ]
      ]);
    };

    return false;
  }
]
```

---

<div class="post-metadata">

**Author:** ![silvan](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/silvan/32/8097_2.png) [@silvan](https://forum.getkirby.com/u/silvan)\
**Post date:** [December 12, 2019, 9:09am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/6 "2019-12-12T09:09:20Z")

</div>

I was aware of the `translations` key, but I couldn’t find out what I need to put in there (except a “translations array”). I thought it’s where to put translated content, when you create a page in order to save it to a file afterwards. Thank you for pointing that out! (Something that I wish was stated somewhere in the docs.)

> To set the language, I have this at the top of the function that handles this route:
> 
> `site()->visit(site()->homepage(), $lang);`

Good to know—I thought `visit()` was only useful when being returned by the route action.

Thank you @bvdputte! I will try and see if this works, but it looks promising.

---

<div class="post-metadata">

**Author:** ![silvan](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/silvan/32/8097_2.png) [@silvan](https://forum.getkirby.com/u/silvan)\
**Post date:** [December 12, 2019, 9:24am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/7 "2019-12-12T09:24:26Z")

</div>

Oh I think I found the “right” solution. I don’t return the virtual page (created by new `Page()`) directly but put it into a variable (`$virtual`). Using `site()->visit($virtual, $language)`works!

Of course you could also create the `new Page()` directly as an argument for the `visit()` method (kind of wrap the `new Page()` code with it).

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [December 12, 2019, 10:43am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/8 "2019-12-12T10:43:56Z")

</div>

Glad you worked it out 👍

---

<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:** [December 12, 2019, 11:07am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/9 "2019-12-12T11:07:42Z")

</div>

I think we need some more documentation for this…

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [December 12, 2019, 12:06pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/10 "2019-12-12T12:06:37Z")

</div>

@texnixe, yes that might be handy. Especially for the multilingual stuff 😉

---

<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:** [December 12, 2019, 12:07pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/11 "2019-12-12T12:07:53Z")

</div>

> [@bvdputte](#):
>
> Especially for the multilingual stuff

That’s what I meant.

> <https://github.com/getkirby/getkirby.com/issues/780>
>
> https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/8

---

<div class="post-metadata">

**Author:** ![Turqueso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/turqueso/32/8058_2.png) [@Turqueso](https://forum.getkirby.com/u/Turqueso)\
**Post date:** [January 14, 2020, 6:40pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/12 "2020-01-14T18:40:17Z")

</div>

The docs for [simple virtual page](https://getkirby.com/docs/guide/virtual-pages/simple-virtual-page) have an example for this.

But i’m wondering if they can have different slugs? I could make 2 virtual pages but they would not be related to each other

---

<div class="post-metadata">

**Author:** ![silvan](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/silvan/32/8097_2.png) [@silvan](https://forum.getkirby.com/u/silvan)\
**Post date:** [January 15, 2020, 9:58am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/13 "2020-01-15T09:58:43Z")

</div>

Yes they can. You put them in the `translations` key of the (virtual) page. As an example:

```
'translations' => [
  'en' => [
    'code' => 'en',
    'slug' => 'slug1'
  ],
  'de' => [
    'code' => 'de',
    'slug' => 'slug2'
  ]
]
```

---

<div class="post-metadata">

**Author:** ![Turqueso](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/turqueso/32/8058_2.png) [@Turqueso](https://forum.getkirby.com/u/Turqueso)\
**Post date:** [January 15, 2020, 7:55pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/14 "2020-01-15T19:55:51Z")

</div>

that works! I think we need more docs on multi language sites

---

<div class="post-metadata">

**Author:** ![squareclouds](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/squareclouds/32/15827_2.png) [@squareclouds](https://forum.getkirby.com/u/squareclouds)\
**Post date:** [May 26, 2024, 9:18am UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/15 "2024-05-26T09:18:37Z")

</div>

@texnixe what is the status of this? i was having problems regarding this today but i got it working now with the visit() function. on saw on github that yesterday @distantnative was active in the issue.

@bvdputte BTW, where do you have the visit() function from?? xD it is not documented anywhere that i could find.

> **[$site](https://getkirby.com/docs/reference/objects/cms/site)**
>
> The $site object is the root element for any site with pages. It represents the main content folder with its site.txt.

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [May 26, 2024, 12:34pm UTC](https://forum.getkirby.com/t/set-current-language-of-virtual-page/16636/16 "2024-05-26T12:34:04Z")

</div>

I got it from here [Routes | Kirby CMS](https://getkirby.com/docs/guide/routing#multi-language-setup)
