Multilingual cookiespolicy plugin

Bonjour ♪

Sorry to ask…
I would like a talent to look, for the glory of the cookiespolicy plug of Grav, the code below and correct the js script below (from cookiespolicy.html.twig file of the plugin) if possible.
Thanks in advance.

    document.addEventListener('DOMContentLoaded', function(event) {
        cookieChoices.showCookieConsent{{ cookiespolicy_type|capitalize }}('{{ cookiespolicy_message }}_{{ langswitcher.current }}','{{ cookiespolicy_btn_close}}_{{ langswitcher.current }}','{{ cookiespolicy_btn_privacy }}_{{ langswitcher.current }}','{{ cookiespolicy_url }}_{{ langswitcher.current }}');

I am trying to change the following strings of file cookiespolicy.yaml:

  • message
  • btn_close
  • btn_privacy
  • url

(say for instance my current language in page would be japanese: ‘ja’ > ‘langswitcher.current’ would be = ‘ja’)

  • message``_ja > message_ja
  • btn_close``_ja > btn_close_ja
  • btn_privacy``_ja > btn_privacy_ja
  • url``_ja > urlstring
    etc …

Thank you in advance for support ☆

Hi @tidivoit,

you are learning quite fast. Impressive! First of all when you finished making changes on the Cookies Policy plugin, it is best you open an issue here , too (if anyone has the same problems, it can use your solution :-)). Ok but now. AFAIK structure your cookiespolicy.yaml like

en: …
fr: …
en: …
fr: …
en: …
fr: …
en: …
fr: …

then in your Twig `cookiespolicy.html.twig` write something like

which is slightly different than your approach. I haven't tested it so if any error occours track it down e.g. print the contents of a variable in Twig via `{{ dump(variable) }}`. If you have the debugger enabled you will see then the contents of this variable ;-)

Hope it makes sense.

P.S.: Your idea can work, too, but in order to use something like `message_fr` you need to pass the values to Twig meaning changing, which means more effort than my approach above.

Thanks very much for help !!!
How beautiful … I am trying it immediatly and see ☆
(thanks also for the compliment ♪ but in fact … not at all.
What I can say is that I did not made a mistake choosing ‘Grav’: the more I get into it, the more I like it…

Glad to here! :smiley:

as result, so far, I get the following error:

addslashes() expects parameter 1 to be string, array given

\grav\user\plugins\cookiespolicy\cookiespolicy.php line 57

line 57 is:

$twig->twig_vars[‘cookiespolicy_message’] = addslashes($this->config->get(‘plugins.cookiespolicy.message’));

this is how is litterally structured cookiespolicy.yaml now:

enabled: true
type: bar
   fr: "En continuant à naviguer sur ce site, vous acceptez l'utilisation des cookies afin de réaliser des statistiques d'audiences et vous proposer des services et une offre adapés à vos centres d'intérêts."
   ja: '日本語'
   en: 'We use cookies to help us give you the best experience when using our website. Unless you change your settings, we will assume that you agree for us to do this. However, you can change your settings at any time according with your browser.'
   fr: 'En savoir plus'
   ja: '詳細'
   en: 'Close'
   fr: 'Fermer' 
   ja: '閉じる'
   en: 'Learn more'
url: ''

I have to say I could not get what you tried to explain to me with {{ dump(variable) }}: where should I put it ? in base.html.twig ? In replace of other element ? … Sorry …

Right now, I think that error is that there is nothing in front of message … but I don’t really understand … In advance, thanks again for your support … ☆

Hi @tidivoit,

argh! You are right. I haven’t noticed Maybe we do it differently. Revert the changes in the yaml, create a languages.yaml file in folder of cookies policy plugin with the contents

         MESSAGE: ...
         PRIVACY: ...
         CLOSE: ...

         MESSAGE: ...
         PRIVACY: ...
         CLOSE: ...  

and use this Twig template snippet

    document.addEventListener('DOMContentLoaded', function(event) {
        cookieChoices.showCookieConsent{{ cookiespolicy_type|capitalize }}('{{ 'PLUGINS.COOKIES_POLICY.MESSAGE'|t }}','{{ 'PLUGINS.COOKIES_POLICY.CLOSE'|t }}','{{ 'PLUGINS.COOKIES_POLICY.PRIVACY'|t }}','{{ cookiespolicy_url }}');

instead (this approach uses Gravs multi-language core features described here

Concerning dump'ing variables this is maybe overkill for the moment. Usually you can put it into your twig file and see the result in the console (see here But here it is a little bit more difficult I guess to print out something for the debug console…

Thank you for taking time !!! ☆ I try then give immediate feed back ♪

feed back ♪

  • languages.yaml in cookiespolicy plug in folder … created
  • cookiespolicy.yaml reverted to … its original form
  • cookiespolicy.html.twig changed to … the exemple in reference.

no event error anymore but
on reload … nothing happens
I mean no black bar on top of screen

Probably the new variables do not fit the previous structure …

Hm, ok then. I’ll test it asic and give you feedback (something in around 5h).

☆ Thanks to you
Whenever you want
♪ I keep triyng while

Ok, one last try. Did you add

{% include 'partials/cookiespolicy.html.twig' %}

just before </body> in your theme as described in the docs? Usually it’s templates/partials/base.html.twig you have to edit.

Yes of course (it has not moved so far)

Strange. Can you add

{% include 'partials/cookiespolicy.html.twig' ignore missing %}

just before </body> in templates/partials/base.html.twig instead and extract, copy and paste the following archive into user/plugins . Then please make sure you delete any file user/config/plugins/cookiespolicy.yaml, close your browser and try again. The reason for the latter is, that the plugin stores a cookie. Maybe you have seen the message once and clicked on it and due to the cookie this message doesn’t reappear.


Thank you very much for your support

Result right now is as previously:
I mean
on reload … nothing happens ☆
no black bar on top of screen
Probably the new variables do not fit the previous structure …

I precise that I have cleared the cache of grav with command line, closed browser and forced cache clearing also

Hm, that’s very strange. The modified cookiespolicy plugin I provided you with the link is working for me and is showing up correctly… I don’t know, but deleting the folder and re-installing CookiesPolicy plugin; does it then showing up to you?

Screen_en Screen_fr Screen_ja

at first on load:
☆ the bar appeared in japanese (yes sir!) on an english adressed page:
I could not believe my eyes ! … ♪
As there was absolutly no reasons …

I immediatly moved to another adress to check how this would be …
and then
everything turned systematically to english
(see screen_en and screen_ja photos here)

Impossible to get japanese back !!!

Also, impossible to have the bar on french pages
(see screen_fr.png here)

May be it’s an question of default language, as the default language of my site is french.

I am sure we are gooing to find a solution … I would like to thank you again for your help …

Sorry I’m a little bit confused and don’t know whether you re-installed CookiesPolicy or not or use the changes I made… ???

I exactly followed the indications you kindly gave …
What other informations would you need?
What is extraordinary is that actually the fr page do not display the bar while before it did … !
Anyway, if you have any other suggestion …

@tidivoit Can you send me your user folder (either via gitter per private message, click on my image/profileor by sending me an emaill to sommerregen [at] benjamin-regler [dot] de ) [zipped] ?

I don’t need all of your data. Stripping it down to contain one page is enough. What’s more important is the rest (config, plugins, themes folder). Further you may exclude the accounts folder, too (since it contains personal informations).