kirby-seo
kirby-seo copied to clipboard
Passing translated keys into computed description
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?
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
But why is $missions translated in the first case then? π€
Thank you for this great plugin.
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. π€
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 :
hope this helps!
Yes, since it's accessed in the panel it uses your account language and not the page language.
fyi I was also getting 'NULL' for $lang on the front end side.
yeah, it's a known issue
Good luck and thank you again for this great plugin.