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
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


Comments on this post will automatically be closed three months from the original post date.



RSS Feed



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.
String overrides
Just beautiful.Needs to be in core no matter what!
Damn!
Good Job dude...
Kudos!
One of those small-but-so-handy modules.
Thank you!
wow
wow that is the coolest module i have ever come across. thanks to the author and for spreading the word about it.
O HAI! Iz imitatin teh lo-cal mod00lz! An not eated dem!
Im in ur dr00ple 5, imitatin ur localz, overraidin ur stringz, srlsy! wtf!
kthxbai!
Love it
BRILLIANT!
thank you
That concludes this comment.
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?
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,
I am looking to do something
I am looking to do something very similar.
I have submitted a feature request to the sting overrides project
http://drupal.org/node/541154
Not sure how to add this functionality,
Maybe the lullabots will lend a helping hand on this one!
Thanks