backdrop-contrib / examples

Examples for Developers
GNU General Public License v2.0
7 stars 9 forks source link

Do not concatenate a translatable string with a non-translated string when it is possible to use a t()-placeholder #60

Closed avpaderno closed 1 year ago

avpaderno commented 1 year ago

The batch_example.install contains the following code.

// Don't want them in the output.
unset($sandbox_status['messages']);
$sandbox['messages'][] = t('$sandbox=') . print_r($sandbox_status, TRUE);

Concatenating a translatable string with a non-translated string should be avoided, when it is possible to use a placeholder in the translatable string, which is exactly what the other line in the same file does.

return t('The batch_example demonstration update did what it was supposed to do: @message', array('@message' => $final_message));

We do not include in the translatable strings HTML markup. For example, the following code is fine.

  $form['info'] = array(
    '#markup' => '<div>' . t("This example does a simplest possible autocomplete by username. You'll need a few users on your system for it to make sense.") . '</div>',
  );

Avoiding to add HTML markup in translatable strings does not mean the following code is not fine, though.

$intro_message .= '<p>' . t('<a href="@url">Reload this page</a> to see cache in action.', array('@url' => request_uri())) . ' ';

We do not want beginner developers think it is fine to concatenate strings instead of using t()-placeholders. Code similar to t('$sandbox=') . print_r($sandbox_status, TRUE) should be avoided.