Drupal Droppings: Fix that string!

This is so obvious that I am going to blog it anyway just in case it helps somebody else!

We are using the node_limit module and we do not like the message that it displays when you exceed the limit of comments for your account.

We wanted to change that message and soon deep meaningful discussions about form_alter hooks, nodeapi hooks, $SESSION variables etc followed blah blah yadda yadda you know what I mean if you do any serious hacking in a crowd during the day sort of thing.

After a few minutes, we noticed a comment in the source code for the t() function which suddenly made us all go very quiet, here’s the top part of the t() function which got us all excited and humbled by Drupal-s magnificence…

<?php
function t($string, $args = array(), $langcode = NULL) {
  global $language;
  static $custom_strings;
 
  $langcode = isset($langcode) ? $langcode : $language->language;
 
  // First, check for an array of customized strings. If present, use the array
  // *instead of* database lookups. This is a high performance way to provide a
  // handful of string replacements. See settings.php for examples.
  // Cache the $custom_strings variable to improve performance.
  if (!isset($custom_strings[$langcode])) {
    $custom_strings[$langcode] = variable_get('locale_custom_strings_'. $langcode, array());
  }

So we steamed of over to the settings.php file and lo and behold, right there at the bottom of the file, a place not oft visited during the day we found this, clearly commented but also commented out,

/**
 * String overrides:
 *
 * To override specific strings on your site with or without enabling locale
 * module, add an entry to this list. This functionality allows you to change
 * a small number of your site's default English language interface strings.
 *
 * Remove the leading hash signs to enable.
 */
# $conf['locale_custom_strings_en'] = array(
#   'forum'      => 'Discussion board',
#   '@count min' => '@count minutes',
# );

For example, lets say there is a module ‘M’ that detected a condition and does this :-

drupal_set_message(t("Your input sucks, do it again you idiot!"));

This might have been funny at the time but if you wanted to change that to something different without touching the code, which may not even be available for modification for many reasons, all you have to do is modify the $conf variable like so :-

$conf['locale_custom_strings_en'] = array(
  "Your input sucks, do it again you idiot!" =>
      "Please check your input and try again."
);

So, all we had to do was add a new entry, the key being the existing string and the value being what we wanted to say instead.
Works like a charm!
:)

Published: June 21st, 2010 at 18:43
Categories: Drupal, PHP