Problem/Motivation

Need to be able to translate the banner using the User Interface translation for strings, at a minimum.

Since the user input for the cookie banner allows HTML formats, this is not possible. To be translatable they should be plain textareas. I see no other way to translate a cookie banner for different languages otherwise.

Even with a text format that only allows an anchor tag, you still cannot do something like this:

see our <a href='@policylink'>privacy policy here</a>.

But you can with a plain textarea field.

CommentFileSizeAuthor
#6 Screenshot 2022-06-03 at 15.48.17.png203.45 KBsvenryen

Comments

kevinquillen created an issue. See original summary.

kevinquillen’s picture

Issue summary: View changes
kevinquillen’s picture

I had to do the following:


/**
 * Implements hook_form_alter().
 */
function mymodule_eu_cookie_compliance_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'eu_cookie_compliance_config_form') {
    $config = \Drupal::config('eu_cookie_compliance.settings');
    $plain_message = $config->get('popup_info');
    $form['popup_message']['popup_info']['#type'] = 'textarea';
    $form['popup_message']['popup_info']['#default_value'] = $plain_message ?? 'Enter a message here';
    $form['popup_message']['popup_info']['#description'] = t("In order to make this field value translatable, HTML is not allowed. If you want to link a piece of text to the privacy policy link value located in this form, enter the anchor tag as @tag and it will be replaced on render.", ['@tag' => "<a href='@policylink'>(name of link)</a>"]);
  }
}

My custom theme .theme:

/**
 * Implements hook_preprocess_eu_cookie_compliance_popup_info().
 */
function mytheme_preprocess_eu_cookie_compliance_popup_info(&$variables) {
  $config = \Drupal::config('eu_cookie_compliance.settings');
  $variables['plain_message'] = $config->get('popup_info');
  $variables['popup_link'] = $config->get('popup_link') ?? '/';
}

Twig template (excerpt):

{% set banner_title = "Please review and accept our privacy policy" %}
{% set banner_message = plain_message|striptags('<a>') %}

<div role="alertdialog" aria-labelledby="popup-text" {{ attributes.addClass(classes) }}>
  <div class="popup-content info eu-cookie-compliance-content" >
    <div id="popup-text" class="eu-cookie-compliance-message" >
      <div class="privacybanner">
        <div class="coh-style-tertiary-section-title-alt privacybanner-row-1">
          <span class="">{{ banner_title|t }}</span>
        </div>
        <div class="privacybanner-row-2 coh-style-type-system-body"><p><span>{{ banner_message|t({'@policylink': popup_link}) }}</span></p></div>
      </div>
svenryen’s picture

Category: Bug report » Feature request

Thanks for the snippets. I'm classifying this as a "Feature request" since it seems to break existing sites if we change this value in the module form.

Just out of curiosity, I'm able to translate the html strings (although in plain text showing html tags), what version of Drupal are you using? Has there been a core change somewhere that prevents translation of markup fields?

kevinquillen’s picture

What is 'in plain text'? I can't even select plain text format on the settings form. Any attempt at translating this is an exception:

InvalidArgumentException: $string ("<p>By clicking accept, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. For more information on the information we collect, see our <a href="@policylink">privacy policy here</a>.</p>") must be a string.

If I grant plain text as usable with CKEditor, then you get an editor chrome and its just weird. Even if I do, I can't select plain text. So how do you select plain text?

svenryen’s picture

Issue summary: View changes
StatusFileSize
new203.45 KB

On the translation page I have a field that is shown as a textarea with html tags included. Upon changing the value and saving, I don't get any error message or exception.

For example, I can open this page: admin/config/system/eu-cookie-compliance/settings/translate/en/add

And there I have this field (among others):

kevinquillen’s picture

That page just gives a 404. How do you get that route? I don't see any mention of 'translate' or 'translation' in the codebase. This is Drupal 9.

svenryen’s picture

You have enabled the Configuration Translation module? Normally if you have the "en" language added and the module is enabled, you should get this path.

What languages are set up for your site?

svenryen’s picture

Status: Active » Postponed (maintainer needs more info)
svenryen’s picture

Status: Postponed (maintainer needs more info) » Fixed

We have a better solution for translations in 2.0.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.