kirby-seo icon indicating copy to clipboard operation
kirby-seo copied to clipboard

Passing translated keys into computed description

Open ImaCrea opened this issue 1 year ago β€’ 6 comments
trafficstars

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 aΜ€ 16 32 25

But why is $missions translated in the first case then? πŸ€”

Thank you for this great plugin.

ImaCrea avatar Nov 19 '24 15:11 ImaCrea

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 avatar Nov 19 '24 16:11 ImaCrea

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 avatar Nov 19 '24 16:11 ImaCrea

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

tobimori avatar Nov 19 '24 17:11 tobimori

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

ImaCrea avatar Nov 19 '24 18:11 ImaCrea

yeah, it's a known issue

tobimori avatar Nov 19 '24 19:11 tobimori

Good luck and thank you again for this great plugin.

ImaCrea avatar Nov 19 '24 19:11 ImaCrea