zephir / kirby-cookieconsent
Kirby cookieconsent 插件
Requires
README
一个用于在 Kirby 中实现 cookieconsent 的插件。
- 使用开源 cookieconsent 库
- 提供 5 种 cookie 类别的默认翻译
- 多语言支持(EN/DE/FR)
- 可定制
目录
1. 安装
建议使用 Composer 进行安装。
1.1 Composer
composer require zephir/kirby-cookieconsent
1.2 下载
下载并将此存储库复制到 /site/plugins/kirby-cookieconsent
。
1.3 Git 子模块
git submodule add https://github.com/zephir/kirby-cookieconsent.git site/plugins/kirby-cookieconsent
2. 设置
将 snippet('cookieconsentCss')
添加到您的页眉,将 snippet('cookieconsentJs')
添加到您的页脚。
3. 选项
3.1 可用选项
3.2 默认值
'zephir.cookieconsent' => [ 'cdn' => false, 'revision' => 1, 'root' => 'document.body', 'autoClearCookies' => true, // Only works when the categories have an autoClear array 'autoShow' => true, 'hideFromBots' => true, 'disablePageInteraction' => false, 'lazyHtmlGeneration' => true, 'guiOptions' => [ 'consentModal' => [ 'layout' => 'box', 'position' => 'bottom right', 'flipButtons' => false, 'equalWeightButtons' => true ], 'preferencesModal' => [ 'layout' => 'box', // 'position' => 'left', // only relevant with the "bar" layout 'flipButtons' => false, 'equalWeightButtons' => true ] ], 'categories' => [ 'necessary' => [ 'enabled' => true, 'readOnly' => true ], 'measurement' => [], 'functionality' => [], 'experience' => [], 'marketing' => [] ], 'translations' => [ 'de' => require_once(__DIR__ . '/translations/de.php'), 'en' => require_once(__DIR__ . '/translations/en.php'), 'fr' => require_once(__DIR__ . '/translations/fr.php') ], 'language' => [ 'locale' => 'en', 'direction' => 'ltr' ] ]
3.3 预定义 cookie 类别
每个 cookie 类别都可以用于控制某些脚本。要了解如何管理脚本,请参阅 CookieConsent 文档。
由本插件预定义的类别
预定义表示每个类别都包含所有语言的翻译。
要启用/禁用类别,您可以使用插件的 categories
选项。
'zephir.cookieconsent' => [ 'categories' => [ 'necessary' => [ 'enabled' => true, // marks the category as enabled by default 'readOnly' => true ], 'measurement' => [], 'functionality' => [], 'experience' => [], 'marketing' => [] ], ]
空数组定义了具有默认选项的类别。您可以在数组中传递额外的选项,如
enabled
或readOnly
。有关这些选项的更多信息,请参阅 CookieConsent 文档。
3.4 删除类别
要删除一个类别,您必须将其值设置为 false
。
'zephir.cookieconsent' => [ 'categories' => [ 'necessary' => [ 'enabled' => true, 'readOnly' => true ], 'measurement' => [], 'functionality' => false, // will not be shown 'experience' => false, // will not be shown 'marketing' => [] ], ]
4. 语言
如果您有一个单语言设置(kirby 选项 languages
未设置为 true
),您将必须定义插件的 language
选项。对于多语言设置,您不需要做任何事情,插件会自动使用 Kirby 语言或回退到翻译数组中的第一个翻译。
'zephir.cookieconsent' => [ 'language' => [ 'locale' => 'de', // The translation to use, default is en 'direction' => 'ltr' // Either ltr or rtl, default is ltr ] ]
5. 翻译
翻译是插件的核心部分。默认情况下,我们为 EN、DE、FR 以及在 3.3 预定义 cookie 类别 中提到的类别提供翻译。要自定义或覆盖默认翻译,您将需要使用 translations
选项。
5.1 覆盖特定翻译
对于此示例,我们正在覆盖一个字符串并在 EN 翻译的章节部分中添加一个新类别。
- 在您的项目中创建一个新的文件夹。对于此示例,我们将其称为
cc-translations
。 - 在该文件夹中,创建一个新的文件
en.php
。
<?php // site/cc-translations/en.php return array_replace_recursive( // We assume the plugin was installed as suggested in the installation section of the readme. require_once(__DIR__ . '/../plugins/kirby-cookieconsent/translations/en.php'), [ 'consentModal' => [ 'title' => 'Lorem ipsum dolor!', // Override consentModal title ], "preferencesModal" => [ "sections" => [ [ // Add a new category in sections "title" => "Custom scripts", "description" => "Diese Cookies helfen uns, Ihnen personalisierte Werbung oder Marketinginhalte bereitzustellen und deren Leistung zu messen.", "linkedCategory" => "custom-scripts", ], ] ] ] );
- 现在您需要在
config.php
中引入(或包含)此文件。因为我们想看到新的类别custom-scripts
,我们还需要更新categories
选项。
'zephir.cookieconsent' => [ 'categories' => [ 'custom-scripts' => [] ], 'translations' => [ 'en' => require_once(__DIR__ . '/../cc-translations/en.php') ] ]
您可以通过复制默认文件并调整值来覆盖整个翻译文件。您也可以包含默认翻译,并手动修改数组,通过修改、添加或删除条目来更改它。
6. 事件
您可以在 CookieConsent 文档 中找到所有可用的事件。
CookieConsent 对象通过 window 对象(
window.CookieConsent
)提供。
许可协议
MIT