Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New language:variables root #6212

Draft
wants to merge 6 commits into
base: v5/develop
Choose a base branch
from

Conversation

afbora
Copy link
Member

@afbora afbora commented Jan 26, 2024

This PR …

This was a more complex problem than I expected (always πŸ™ˆ). Because the system should be able to read, update and delete language variables both from the language file and from a custom root. It involves a breakage change, but I wanted to remove the code complexity by creating a separate LanguageVariables object.

Fixes

Features

  • New language:variables root
$kirby = new Kirby([
    'roots' => [
        'language:variables' => __DIR__ . '/site/variables'
    ]
]);

Breaking changes

  • Language::translations() deprecated, use Language::variables() instead
  • $language->translations() returns LanguageVariables object instead array. Use $language->translations()->toArray() or $language->variables()->toArray().

Ready?

  • Unit tests for fixed bug/feature
  • In-code documentation (wherever needed)
  • Tests and checks all pass

For review team

@afbora afbora added needs: discussion πŸ—£ Requires further discussion to proceed type: enhancement ✨ Suggests an enhancement; improves Kirby type: feature πŸŽ‰ Adds a feature (requests β†’ feedback.getkirby.com) needs: tests πŸ§ͺ Requires missing tests labels Jan 26, 2024
@afbora afbora requested a review from a team January 26, 2024 14:07
@afbora afbora self-assigned this Jan 26, 2024
Copy link
Member

@lukasbestle lukasbestle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great solution to move the logic to a separate class. πŸ‘
A few things I noticed while skimming through the changes:

src/Cms/Language.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
@lukasbestle lukasbestle removed the needs: discussion πŸ—£ Requires further discussion to proceed label Jan 27, 2024
@afbora afbora mentioned this pull request Jan 27, 2024
5 tasks
@distantnative distantnative added the needs: changes πŸ” Implement any requested changes to proceed label Feb 3, 2024
@tobimori
Copy link
Contributor

tobimori commented Feb 6, 2024

Excited for this, thanks! Will this include the possibility to add translations (e.g. for the panel) even if the language does not have a content language assigned to it? Some of my pages are German only, but should still have English translations for the panel.

@afbora
Copy link
Member Author

afbora commented Feb 10, 2024

@tobimori In your case; multilingual is not enabled for the frontend, but you want to add translations (for example for blueprint labels) for users in different languages using the panel. Do I understand correctly?

@tobimori
Copy link
Contributor

tobimori commented Feb 10, 2024

@tobimori In your case; multilingual is not enabled for the frontend, but you want to add translations (for example for blueprint labels) for users in different languages using the panel. Do I understand correctly?

Multilingual is enabled, but the languages in the panel might differ from the languages in the frontend.
Let's say I have de and en in the frontend, but I want to add a french translation for my field labels etc. as well.

Currently, I have a translations directory and load yaml files from a plugin. (https://github.com/tobimori/kirby-baukasten/blob/main/site/plugins/project-extended/index.php#L20)

@afbora
Copy link
Member Author

afbora commented Feb 10, 2024

If there is no FR language (so fr.php language file), unfortunately it will not load custom FR translations. It will only load the translation files for existing languages from a custom root.

@tobimori
Copy link
Contributor

😦

@afbora afbora added this to the 5.0.0 milestone Mar 29, 2024
@distantnative distantnative force-pushed the lab/afbora/new-translations-root branch from 7f4acb1 to 178fa70 Compare July 22, 2024 15:33
Copy link
Member

@distantnative distantnative left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some first comments. Let me know if you think I misunderstood parts of the code :)

src/Cms/Language.php Outdated Show resolved Hide resolved
if ($this->translations()->root() !== null) {
$this->translations()->save($data['translations'] ?? []);
$data['translations'] = [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about instead of unsetting it here, we do an if-else clause: The if part writes it to the dedicated root, the else part adds it to the $data array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If custom root is already set (root !== null), it empties the data.translations array and writes to the custom root. If custom root is not set, it saves the data array normally as before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant:

		$data = [
			...$existingData,
			'code'         => $this->code(),
			'default'      => $this->isDefault(),
			'direction'    => $this->direction(),
			'locale'       => Locale::export($this->locale()),
			'name'         => $this->name(),
			'url'          => $this->url,
		];

		ksort($data);

		// save translations to the custom root and remove translations
		// to prevent duplication write into the language file
		if ($this->translations()->root() !== null) {
			$this->translations()->save();
		} else {
			$data['translations'] = $this->translations()->toArray();
		}

(not passing anything to $this->translations()->save() here based on the other suggestion I make below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to empty to $data['translations'] index if custom root defined. Because after few lines later will run Data::write() and same translation strings to be written to language translations index. $data['translations'] = [] prevents it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we don't add it to $data in the first place, we wouldn't need to unset it, right? Or could $existingData also have a translations key?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, $existingData mean /site/languages/*.php data and currently languages have translations key.

@@ -571,7 +580,7 @@ public function update(array|null $props = null): static
$updated = $this->clone($props);

if (isset($props['translations']) === true) {
$updated->translations = $props['translations'];
$updated->translations = new LanguageTranslations($updated, $props['translations']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? I thought translations property is protected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already protected: protected array $translations?

src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
src/Cms/LanguageTranslations.php Outdated Show resolved Hide resolved
@afbora afbora changed the title [v5] New translations root New translations root Sep 1, 2024
@afbora afbora force-pushed the lab/afbora/new-translations-root branch 6 times, most recently from c3bc647 to a18e0ef Compare September 13, 2024 20:32
@afbora afbora force-pushed the lab/afbora/new-translations-root branch from a18e0ef to b1b372f Compare September 13, 2024 20:35
@afbora
Copy link
Member Author

afbora commented Sep 13, 2024

@distantnative I did some changes based on your reviews and done some improvements. Would be nice if you take a look when you're available before writing unit tests and last touch πŸ™Œ

src/Cms/Core.php Outdated
'sessions' => fn (array $roots) => $roots['site'] . '/sessions',
'snippets' => fn (array $roots) => $roots['site'] . '/snippets',
'templates' => fn (array $roots) => $roots['site'] . '/templates',
'translations' => null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: I don't have a good idea yet, but wondering if it's too confusing to have i18n:translations and translations? Maybe languages:translations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bastianallgeier What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was our last decision for the language variable permissions? I wonder if we maybe should go the same route here with naming as for permissions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now to be languageVariables.create|update|delete.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could go for language:variables maybe

I know that it totally clashes with the translations naming everywhere. But I think it makes sense to fully commit to one naming convention here and language variables are at least not giving us any more headaches with the entire content translations vs. ui translations monster.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe even i18n:variables then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure for this PR, @bastianallgeier ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that it belongs to the language and thus the language namespace feels better to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point there is that so far we don't have a language namespace anyways, we are creating a new one. And what if language is the i18n namespace?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still about the root and I think we are mixing them up here. The i18n roots are all in our kirby folder to store interface translations and slug rules. The language:variables root would be a custom project root inside of site.

if ($this->translations()->root() !== null) {
$this->translations()->save($data['translations'] ?? []);
$data['translations'] = [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant:

		$data = [
			...$existingData,
			'code'         => $this->code(),
			'default'      => $this->isDefault(),
			'direction'    => $this->direction(),
			'locale'       => Locale::export($this->locale()),
			'name'         => $this->name(),
			'url'          => $this->url,
		];

		ksort($data);

		// save translations to the custom root and remove translations
		// to prevent duplication write into the language file
		if ($this->translations()->root() !== null) {
			$this->translations()->save();
		} else {
			$data['translations'] = $this->translations()->toArray();
		}

(not passing anything to $this->translations()->save() here based on the other suggestion I make below)

Comment on lines 72 to 74
public function save(array $translations = []): static
{
$this->data = $translations;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function save(array $translations = []): static
{
$this->data = $translations;
public function save(array|null $translations = null): static
{
if ($translations !== null) {
$this->update($translations);
}

@afbora afbora changed the title New translations root New language:variables root Sep 16, 2024
@distantnative distantnative removed the needs: changes πŸ” Implement any requested changes to proceed label Sep 19, 2024
src/Cms/Language.php Outdated Show resolved Hide resolved
src/Cms/Language.php Outdated Show resolved Hide resolved
src/Cms/Language.php Outdated Show resolved Hide resolved
Co-authored-by: Bastian Allgeier <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: tests πŸ§ͺ Requires missing tests type: enhancement ✨ Suggests an enhancement; improves Kirby type: feature πŸŽ‰ Adds a feature (requests β†’ feedback.getkirby.com)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External language variables setup no longer works since K4's language views
5 participants