function DrupalTranslator::processParameters

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()
  2. 10 core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()
  3. 11.x core/lib/Drupal/Core/Validation/DrupalTranslator.php \Drupal\Core\Validation\DrupalTranslator::processParameters()

Processes the parameters array for use with TranslatableMarkup.

2 calls to DrupalTranslator::processParameters()
DrupalTranslator::trans in core/lib/Drupal/Core/Validation/DrupalTranslator.php
Translates the given message.
DrupalTranslator::transChoice in core/lib/Drupal/Core/Validation/DrupalTranslator.php

File

core/lib/Drupal/Core/Validation/DrupalTranslator.php, line 75

Class

DrupalTranslator
Translates strings using Drupal's translation system.

Namespace

Drupal\Core\Validation

Code

protected function processParameters(array $parameters) {
    $return = [];
    foreach ($parameters as $key => $value) {
        // We allow the values in the parameters to be safe string objects. This
        // can be useful when we want to use parameter values that are
        // TranslatableMarkup.
        if ($value instanceof MarkupInterface) {
            $value = (string) $value;
        }
        if (is_object($value)) {
            // TranslatableMarkup does not work with objects being passed as
            // replacement strings.
        }
        elseif (strpos($key, '{{ ') === 0 && strrpos($key, ' }}') == strlen($key) - 3) {
            // Transform it into a Drupal pattern using the format %name.
            $key = '%' . substr($key, 3, strlen($key) - 6);
            $return[$key] = $value;
        }
        else {
            $return[$key] = $value;
        }
    }
    return $return;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.