Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/AnalyticCookiesCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public function google(string $id, bool $anonymizeIp = true): static
$group->name(static::GOOGLE_ANALYTICS)
->cookie(fn(Cookie $cookie) => $cookie->name('_ga')
->duration(2 * 365 * 24 * 60)
->description(__('cookieConsent::cookies.defaults._ga'))
->setAttribute('description', fn() => $this->translate('defaults._ga'))
)
->cookie(fn(Cookie $cookie) => $cookie->name('_ga_' . strtoupper($key))
->duration(2 * 365 * 24 * 60)
->description(__('cookieConsent::cookies.defaults._ga_ID'))
->setAttribute('description', fn() => $this->translate('defaults._ga_ID'))
)
->cookie(fn(Cookie $cookie) => $cookie->name('_gid')
->duration(24 * 60)
->description(__('cookieConsent::cookies.defaults._gid'))
->setAttribute('description', fn() => $this->translate('defaults._gid'))
)
->cookie(fn(Cookie $cookie) => $cookie->name('_gat')
->duration(1)
->description(__('cookieConsent::cookies.defaults._gat'))
->setAttribute('description', fn() => $this->translate('defaults._gat'))
)
->accepted(fn(Consent $consent) => $consent
->script('<script async src="https://www.googletagmanager.com/gtag/js?id=' . $id . '"></script>')
Expand Down
16 changes: 15 additions & 1 deletion src/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Whitecube\LaravelCookieConsent\Concerns;

use Closure;

trait HasAttributes
{
/**
Expand Down Expand Up @@ -51,9 +53,21 @@ public function setAttribute(string $attribute, mixed $value): void

/**
* Get a specific attribute's value.
*
* The closures are only executed when the view accesses the attributes,
* ensuring that the current locale has already been set and translations
* are rendered correctly.
*
* This lazy evaluation approach is necessary because translations depend
* on the active locale at render time. If the closures were executed earlier
* (e.g., when the attribute was assigned), they might use the wrong locale,
* causing incorrect or untranslated text to appear in the output.
*
*/
public function getAttribute(string $attribute): mixed
{
return $this->attributes[$attribute] ?? null;
$value = $this->attributes[$attribute] ?? null;

return $value instanceof Closure ? $value() : $value;
}
}
4 changes: 2 additions & 2 deletions src/CookiesCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CookiesCategory
public function __construct(string $key)
{
$this->key = $key;
$this->setAttribute('title', $this->translate('categories.' . $key . '.title', ucfirst($key)));
$this->setAttribute('description', $this->translate('categories.' . $key . '.description'));
$this->setAttribute('title', fn() => $this->translate('categories.' . $key . '.title', ucfirst($key)));
$this->setAttribute('description', fn() => $this->translate('categories.' . $key . '.description'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/EssentialCookiesCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function consent(): static
return $this->cookie(function(Cookie $cookie) {
$cookie->name(Config::get('cookieconsent.cookie.name'))
->duration(Config::get('cookieconsent.cookie.duration'))
->description(__('cookieConsent::cookies.defaults.consent'));
->setAttribute('description', fn() => $this->translate('defaults.consent'));
});
}

Expand All @@ -26,7 +26,7 @@ public function session(): static
return $this->cookie(function(Cookie $cookie) {
$cookie->name(Config::get('session.cookie'))
->duration(Config::get('session.lifetime'))
->description(__('cookieConsent::cookies.defaults.session'));
->setAttribute('description', fn() => $this->translate('defaults.session'));
});
}

Expand All @@ -38,7 +38,7 @@ public function csrf(): static
return $this->cookie(function(Cookie $cookie) {
$cookie->name('XSRF-TOKEN')
->duration(Config::get('session.lifetime'))
->description(__('cookieConsent::cookies.defaults.csrf'));
->setAttribute('description', fn() => $this->translate('defaults.csrf'));
});
}
}