Get updates and news:
Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box? Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.

Replace any string in Drupal (5/6), without Locale module

Drupal versions: 5.x, 6.x

During a recent Lullboat podcast we mentioned a new feature in Drupal 6 that allows you replace any string in Drupal without using Locale module (thanks Moshe!). Faithful podcast listener Rob Loach took it upon himself to write the String Overrides module that lets you accomplish this task with ease in Drupal 6.

While Lullabots were lamenting the fact that this feature was never in Drupal 5, we realized that it actually could be accomplished. An hour after undertaking the project, the patch was submitted to the String Overrides queue and committed shortly afterwards.

String Overrides accomplishes this in Drupal 5 by imitating the Locale module, and without hitting the database (since variables are cached and pulled up on every request anyway). Since this essentially a hack for Drupal 5, you cannot combine it with Locale module or languages other than English. Here's the key code that makes this possible:

<?php
/**
* Fake the locale function.
*/
if (!function_exists('locale')) {
  function
locale_supported_languages($reset = FALSE, $getall = FALSE) {
    return array(
     
'name' => array('xx' => 'String Overrides'),
     
'formula' => array('xx' => ''),
    );
  }

  function
locale($string) {
    static
$strings = array();
   
$strings = variable_get('locale_custom_strings_en', array());
    if (
$strings[$string]) {
        return
$strings[$string];
    }
    else {
        return
$string;
    }
  }
}
?>

Basically String Overrides pretends that it is Locale module and does the translation without hitting the database. Because it uses the same variable format as the Drupal 6 core feature, when the site upgrades to Drupal 6 the hack is no longer necessary and the site becomes compatible with Locale module again. For anyone needing to change just a few words on their Drupal site, String Overrides is a great solution.

In Action

stringoverrides1.png

stringoverrides2.png

Comments

nice one

thats wicked clever. very nice work.

Awesome!

Thank You Thank You Thank You!!!! No more core/module hacking needed to alter a simple line of text.

Josh

Excellent Timing!!!

Just last night, I turned off the computer frustrated at how hard it was turning out to be to change "Comments" to be called "Reviews". I was debating between the Locale module route, versus using the Node comments module, versus setting up a CCK/Viewfield solution. I just tested the String Overrides module and changed everything I needed in about 30 seconds. Thank you!

Clever indeed!

That's such a cool idea.

nice little hack. I always

nice little hack. I always thought the locale module + custom language was overkill to just change a few textsnippets.

Thank You!!

I was using the locale module with a custom language and almost considered disabling it to improve response times. Your module does everything I needed, with a lot less overhead!

Good

I just tested the String Overrides module and changed everything I needed in about 30 seconds. Thank you! merde

Where does this code go? In

Where does this code go? In a template.php file? Thanks very much.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h2> <h3>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options