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

Passing translated keys into computed description #123

Open
ImaCrea opened this issue Nov 19, 2024 · 6 comments
Open

Passing translated keys into computed description #123

ImaCrea opened this issue Nov 19, 2024 · 6 comments
Milestone

Comments

@ImaCrea
Copy link

ImaCrea commented Nov 19, 2024

Hi,

I'm trying to set some computed content for description but I'm having difficulties with using a translated string into it.

Here's my code :

<?php
// site/models/template.php

use Kirby\Cms\Page;

class ReferencePage extends Page
{
	public function metaDefaults(string $lang = null): array
	{
        $content = $this->content($lang);
        $missions = "";
        foreach ($travaux = $content->travaux()->toPages() as $travail) {
            $missions=$missions." - ".$travail->title();
        }
        $description = t('seo.filmBy',null,$lang).$content->refWho().$missions;

		return [

			// you can use field names (from blueprint)
			'metaDescription' => $description,

			// or any meta tag
			'og:image' => "{$content->artwork()->toFile()->url()}",
			"og:image:width" => 600,
			"og:image:height" => 1230,

		];
	}
}

The key 'seo.filmBy' should return "Un film de " in FR and "Directed by " in EN. But I'm always getting the FR value (default language) and not the EN value on english version. What's weird is content is translated, meaning $lang is passed well right?

seo

It's as if $lang was not returning the lang properly into t(). 🤔 I've tried different things without success, any hint would be super helpful 🙏

I wonder how can I try to debug this. For some reasons I don't understand, if I try this to see what's returns $lang :

<?php
// site/models/template.php

use Kirby\Cms\Page;

class ReferencePage extends Page
{
	public function metaDefaults(string $lang = null): array
	{
        $content = $this->content($lang);
        $missions = "";
        foreach ($travaux = $content->travaux()->toPages() as $travail) {
            $missions=$missions." - ".$travail->title();
        }
        $description = gettype($lang); // TEST

		return [

			// you can use field names (from blueprint)
			'metaDescription' => $description,

			// or any meta tag
			'og:image' => "{$content->artwork()->toFile()->url()}",
			"og:image:width" => 600,
			"og:image:height" => 1230,

		];
	}
}

I see $lang is then NULL
Capture d’écran 2024-11-19 à 16 32 25

But why is $missions translated in the first case then? 🤔

Thank you for this great plugin.

@ImaCrea
Copy link
Author

ImaCrea commented Nov 19, 2024

Alright. I tried with $content = $this->content(); instead of $content = $this->content($lang); and I get the same result in the same case.

Then I checked into how metaDefaults is instantiated and added some debug there

public function __construct(Page $page, ?string $lang = null)
	{
		$this->page = $page;
		$this->lang = $lang;

		if (method_exists($this->page, 'metaDefaults')) {
			$this->metaDefaults = $this->page->metaDefaults($this->lang);
		}
	}

And my understanding is that it's always called with $lang as NULL which should explain what happens I guess. 🤔

@ImaCrea
Copy link
Author

ImaCrea commented Nov 19, 2024

And this is how I fixed /site/plugins/kirby-seo/classes/Meta.php :

public function __construct(Page $page, ?string $lang = null)
	{
		$this->page = $page;
		$this->lang = kirby()->language()->code(); // EDITED

		//site()->logger()->debug(kirby()->language()->code());

		if (method_exists($this->page, 'metaDefaults')) {
			$this->metaDefaults = $this->page->metaDefaults($this->lang);
		}
	}

Not sure if it's 100% clean as I'm not a php expert but it works. Lang code is sent over and translation is done :

seo2

hope this helps!

@ImaCrea ImaCrea changed the title Computed description and multilang Passing translated keys into computed description Nov 19, 2024
@tobimori
Copy link
Owner

Yes, since it's accessed in the panel it uses your account language and not the page language.

@tobimori tobimori added this to the 2.0.0 milestone Nov 19, 2024
@ImaCrea
Copy link
Author

ImaCrea commented Nov 19, 2024

fyi I was also getting 'NULL' for $lang on the front end side.

@tobimori
Copy link
Owner

yeah, it's a known issue

@ImaCrea
Copy link
Author

ImaCrea commented Nov 19, 2024

Good luck and thank you again for this great plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants