Current language in field method

In a custom field method, how do I get the current language of the content in a multilingual setup?


returns the current language.

Okay, I have to revisit this question. I first thought using kirby()->language() was the correct answer but I now encountered the same issue as when posting for the first time. And the context is a bit different:

Given an install with two languages, German and English, kirby()->language() will always return the current language context. So if I’m visiting or any subpages it will be German, if I’m visiting it will be English. Let’s call this the site language. What I’m looking for is the content language.

What do I mean by saying content language? Think of a German page named texte, there are two situations:

  1. the page has already been translated to English,
  2. the page has not been translated.

In the first case there are two content files on the server, and the frontend displays either the German or the English version depending of the path. But in the second case, there is only a German content file. Still, the page will be accessible under and under – displaying the German content both times. So while two valid language paths exist, there is actually only on language in the background. And even it the site language is English, the content will be German.

So, is there a method to know this content language?

I’m looking for some kind page method like $page->language() in comparison to the global method $kirby->language().

It’s not as easy as that, because what if someone has translated the title and there is no content apart from the title in the presumably translated version? It would make more sense to add a field that marks a page as translated.

I will revisit your question later.

Yeah, that’s true. I’m actually not sure if I think it’s a good idea to copy the primary content when only translating the title – but that’s another topic.

For my use case, knowing if a content file for the current language exists or not is the essential part.

you can check if a translation for a page exists like this:

if ($page->translation(kirby()->language()->code())->exists()) {
  echo 'The ' . $kirby->language()->name() . ' translation exists';

Note that the content file for the default language is always created by the Panel.

This method can also be used to filter by language:

As I already mentioned above, this doesn’t help to determine if a page was really translated. Maybe the page has a translation file but no content or untranslated content. Therefore marking a page as translated explicitly is the best choice in my opinion and then additionally use such a field as additional filter.