Skip to main content

Devbit

How to translate the Maxlength Countdown message label globally

When enabling the soft or hard character limit for a field using the Maxlength module, the module writes a default label into the field widget configuration. Because the label string is written directly into configuration, the fallback string logic is never used. As a result, the label cannot be…
A Drupal backend screenshot showing the "Count down message" field

Example configuration created by enabling Maxlength:

# ...
content:
  field_copyright:
    type: text_textarea
    weight: 3
    region: content
    settings:
      rows: 2
      placeholder: ''
    third_party_settings:
      allowed_formats:
        hide_help: '1'
        hide_guidelines: '1'
      maxlength:
        maxlength_js: null
        maxlength_js_label: 'Content limited to @limit characters, remaining: <strong>@remaining</strong>'
        maxlength_js_enforce: false
# ...

However, the module already contains fallback logic for this label in maxlength.module: 

$maxlength_js_label = !empty($thirdPartySettings['maxlength']['maxlength_js_label']) ? $thirdPartySettings['maxlength']['maxlength_js_label'] : t('Content limited to @limit characters, remaining: <strong>@remaining</strong>');
$maxlength_js = $thirdPartySettings['maxlength']['maxlength_js'];

Working configuration: empty the label

# ...
content:
  field_copyright:
    type: text_textarea
    weight: 3
    region: content
    settings:
      rows: 2
      placeholder: ''
    third_party_settings:
      allowed_formats:
        hide_help: '1'
        hide_guidelines: '1'
      maxlength:
        maxlength_js: null
        maxlength_js_label: ''
        maxlength_js_enforce: false
# ...

With the label empty, the module falls back to its internal t() string, which can then be translated normally via the interface translation system.

Issue:

When enabling the soft or hard character limit for a field using the Maxlength module, the module writes a default label into the field widget configuration. Because the label string is written directly into configuration, the fallback string logic is never used. As a result, the label cannot be translated through the Interface Translation system, since Drupal treats it as configuration rather than a translatable interface string.

Solution:

To allow translating the countdown string via the Drupal interface translation, the label must be removed from the configuration so the module fallback is used instead.

  • There is alrady a fix for this problem for the maxlength module on drupal.org.
Profilfoto Lothar Neumann

Lothar Ferreira Neumann

Junior Drupal Entwickler

Ihre Drupal-Website zukunftssicher machen

Wir prüfen Ihr System und zeigen, was Sie für AI, Wartung und Upgrades brauchen.