ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Language: add default language in single-language sites

Open hdodov opened this issue 6 years ago • 5 comments

Currently, if you run:

$lang = kirby()->defaultLanguage();

...in a single-language site, you get null. That doesn't make sense. There's an implicit default language and that's English. Look at this:

image

I believe those 12 literals aren't good design. Those same things that users define in the languages folder under PHP files - Kirby can have one of those in its config folder and it can be used when there's nothing defined in languages. This would:

  • Allow Kirby to define metadata for its "implicit" default language just how users can. For example, locale - does Kirby use British English or American English?
  • Allow users to utilize language variables even in single-language sites. This may sound strange, but it's actually useful in practice. It's better to have static content in a single place, rather than in fields (which is time consuming) or as plain text in templates (which is less organized). If the site is not set as multilang in the config - Kirby simply uses the first file in languages
  • The Kirby core could reduce its source code since it wouldn't have to use "fallbacks" - it would simply have a language
  • Plugin authors will have a more consistent API - currently, you have to make checks for whether the site is multilingual in cases where that's unnecessary

In conclusion, my proposal is:

  • kirby()->defaultLanguage() returns
    • In a single-language site - the language in Kirby's config or the first language in the languages folder
    • In a multi-language site - works as it does now
  • kirby()->language() returns
    • In a single-language site - the same as defaultLanguage()
    • In a multi-language site - the currently viewed language (translation)
  • kirby()->multilang() returns whether the site is flagged as multi-language in config.php

hdodov avatar Sep 25 '19 13:09 hdodov

That sounds like we would force developers ti define a language even when they don’t want to care.

distantnative avatar Sep 25 '19 13:09 distantnative

@distantnative nope, I said:

Those same things that users define in the languages folder under PHP files - Kirby can have one of those in its config folder and it can be used when there's nothing defined in languages.

Kirby can simply provide a default language for developers who don't care. It already does that, actually - in the form of 12 occurrences of 'en' in the source code. My whole point is to improve that.

At the same time, if I want to have a single-language site with Deutch as the default language, I could simply create languages/de.php and that's it. If I decide to turn it into multi-language, I just flip the switch in config.php and add more languages.

hdodov avatar Sep 25 '19 13:09 hdodov

Why don’t you flip on multi-language with only one default language?

distantnative avatar Sep 25 '19 14:09 distantnative

For several reasons:

  • It doesn't make sense
  • It would be easier for plugin developers to not rely on users setting the multi-language option in their single-language sites
  • Your content files would end in .de.txt instead of .txt
  • Imagine a new developer joining the project and wondering why the site is flagged as multilingual when there's a single language

hdodov avatar Sep 25 '19 14:09 hdodov

I'm testing now and actually, you can define languages/de.php without setting languages => true in the config. You indeed set the "default" language of the site with that, but all your files end up with .de.txt. I don't think that has to happen. You still have just one language.

Docs say the following about the languages config setting:

Enables language definitions via the Panel

So that doesn't alter the way Kirby treats languages. I think that's good. But apparently, Kirby internally flips the multilang switch on whenever there's a file in site/languages. I don't think that's correct.

I think it should function like this:

  • No languages folder or nothing inside of it - Kirby uses its own default language definition for English from kirby/config
  • One file in languages - it is used as the default site language instead (nothing appended to txt files)
  • More than one file in languages - the site is now multilang and saving content appends the language locale to txt files as needed. Developers should add default => true to one of the languages or an error is thrown

This is way simpler. One file - one language. Many files - many languages.

hdodov avatar Sep 25 '19 14:09 hdodov