Does site()->isLanguageDefault(); exist?

Does this function exist in the core somewhere? Check if the current language is the default one?

function isLanguageDefault() {
  if( site()->language()->code() != '' ) {
    if( site()->language()->code() === site()->defaultLanguage()->code() ) {
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
}

I feel like a function like that should exist.

Looking at the cheat sheet:
https://getkirby.com/docs/cheatsheet#site
https://getkirby.com/docs/cheatsheet#language

  • $site->language() returns the current language.
  • $language->default() returns true if $language is the default language.

So you could rewrite your function as:

function isLanguageDefault() {
  return site()->language()->default();
}

or just use $site->language()->default() without writing a function, of course.

2 Likes

How could I miss that one!? That’s so much shorter! I don’t always find the stuff I need in the docs. Maybe it’s the docs, maybe it’s me…

Thanks!

There’s a reason for that. :wink:

Kirby’s API style prefers simple nouns or verbs over verbose names. The upside is brevity, and often simplicity. The cost is — at least sometimes — the need to learn and remember what the short names don’t tell (cognitive load).

With longer and more explicit names, you might have found it easier to figure out that you needed to check (careful, this is not real Kirby code):

// This is ugly, but I know what it means
$site->getCurrentDisplayLanguageObject()->isDefaultLanguage();

While shorter names can be confusing:

// I could read this as "get a the general-purpose 'language' utility,
// and ask it what the 'default' language is (from the configuration)".
$site->language()->default();

Sometimes there might be balanced solutions:

// Okay I think I still get this one
$site->currentLanguage()->isDefault();

But good API design is hard and I’m not saying this last example is necessarily better that what Kirby used. :wink:

You are right. To analyze the what I did.

1

I visited the Cheatsheet and then the #language hash.

2

I looked into the functions, even default. Inside it it’s a loop to get each language. I don’t needed a loop, I just needed a function to know if the current language was the default one or not.

I also thought that default was returning the language code and not true or false.

3

Then I visited #site and language. I did not find anything about default language there.

Summery

  1. I could combine those docs to figure out how to use it but I did not see that.
  2. I also overlooked the default function as I thought it was returning the default language code.

Solutions

You have mention many of them already, I know…

  1. More docs to cover everything.
  2. Rename default to ìsDefault.
  3. Way to see everything about language, without jumping around. Maybe have different tags, like “language” and “site”.
  4. Some documentation tool, something like Grav API including references to or inline examples.
  5. A better search in a better place.

Just in case you missed that, but the docs actually tell you what each method returns:

Yes, I know. I overlooked it because it was inside a loop and I was looking for:

site()->language()->default();