by Nate Haug on February 2, 2008 // Short URL

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

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

Nate Haug

Senior Drupal Architect

Want Nate Haug to speak at your event? Contact us with the details and we’ll be in touch soon.

Comments

Joshua_cohen

Awesome!

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

Josh

Reply

Matt V.

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!

Reply

Herb

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!

Reply

merde

Good

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

Reply

spunkz25

Damn!

Good Job dude...

Reply

will hall

wow

wow that is the coolest module i have ever come across. thanks to the author and for spreading the word about it.

Reply

fallecs

Love it

BRILLIANT!

thank you

That concludes this comment.

Reply

JeniferTucker

Using stringoverride for translating language

Is it possible to use stringoverride to change a menu from "About us" to "Sobre nosotros"? I can translate menu items, but not the main menu block heading. Have tried just about everything and unable to solve this problem. I've got Drupal 6 installed with Locale, menutranslation all plugged in, but still no joy :-(

Example

About us
* Who we are
* How to find us

swtich to Spanish and the menu stays the same with the items changing

About us
* Quienes somos
* Cómo llegar

Anyone have any advice?

Reply

azhar

need help for string override

Hi,

I have a local menu task called products i want to check

if ($node->category =='restaurant'){
print 'Restaurant';
}else{
Print 'Products';
}

i want to use this if in string override can any one tell me how can i do this ....

Thanks,

Reply