Constants

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Constants

Post by Echilon »

I have several hundred constants which define strings used to translate my PHP script into different languages. I'm wondering which would be the best way to include them: define them all or split them into a function, which accepts a parameter. On every page, pass a parameter to the function which will load just the constants needed for that page. Would the overhead to store and call the function actually end up making the script slower? Which would be quicker/require less resources?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Splitting them into several files that are logical groupings. A function is useless really if you're using actual constants.
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Post by Echilon »

They're all going to be in the same file. I was thinking I could split them like this

Code: Select all

function getconstants($type) {
    switch($type) {
    case "page1":
        define('const1','some text');
        define('const2','some other text');
        break;
    case "page2":
        define('const3','some more text');
        define('const4','yet more text');
        break;
}
Then call the function from the page, so from page 1, I'd call getconstants('page1'); which would save declaring constants that I'm not going to use.[/syntax]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

It would be easier to split them into different files and

Code: Select all

include('languages/english/page1.php');
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Post by Echilon »

It's alot easier for me to define them as constants. I really wanted to know whether the overhead I'd need to call a function would be greater than the memory that would be used to just define all constants at the start of the script.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I see a problem with breaking them up by page. I would imagine that a lot of the text will be reused (like "name", "email address", "submit form", etc). If you've defined all the constants for a particular page you'll end up duplicating ones that are used across many pages.

Personally, I'd define them all as constants in a single language specific file, then include it based on the users language preference.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Split them into separate language files and don't directly worry about where they are... write a class for that.

Code: Select all

$lang->paint("USERNAME_TAKEN", $user_lang);
The class can then look for the correct language file and include it itself.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

For one of my websites, I've made it multi-lingual by having a database-table for each phrase required in their respective language

Code: Select all

CREATE TABLE `ls` (
  `StringID` bigint(20) NOT NULL auto_increment,
  `English` varchar(255) NOT NULL default '',
  `Hindi` text NOT NULL,
  `Malayalam` text NOT NULL,
  `German` text NOT NULL,
  PRIMARY KEY  (`StringID`),
  UNIQUE KEY `English` (`English`)
);
This is resource intensive.

Code: Select all

<?php $Language_Words = GetLanguageStrings('Hindi', array('Heading', 'Description', 'Some Title', 'Something Else')); ?>

<h1><?php echo $Language_Words['Heading']; ?></h1>
<h2><?php echo $Language_Words['Some Title']; ?></h2>
.
.
.

/*
_______________________________________________________________________________________________________________
 Function GetLanguageStrings()
 Paramters :
           $Language : Which Language to translate to
           $Phrases  : An array of English words to be looked up in the db for corresponding Target Language
                       Words
 Return the Translatede Language Words of specifed English words in an array with key being the the English
        Word and the value being the translated word. If target is english then key and value will be same
_______________________________________________________________________________________________________________
*/
function GetLanguageStrings($Language, $Phrases)
 {
        $Language_Words = array();

        if ($Language != "English")
         {
                $Lang_SQL = "SELECT `English`, `$Language` FROM `ls` WHERE ";
                for ($i=0; $i<count($Phrases); $i++)
                 {
                        $Lang_SQL .= "`English` = '".$Phrases[$i]."' OR ";
                 }
                $Lang_SQL = substr($Lang_SQL, 0, -4);

                $Lang_Res = @mysql_query($Lang_SQL);
                while ($Lang_Row = @mysql_fetch_assoc($Lang_Res))
                 {
                        if ($Lang_Row[$Language] != "")
                         $Language_Words[$Lang_Row['English']] = $Lang_Row[$Language];
                        else # If the Target Language Word is null or empty then use the English word instead
                         $Language_Words[$Lang_Row['English']] = $Lang_Row['English'];
                 }
         }
        else # return all english words - return the input array as values too
         {
                for ($i=0; $i<count($Phrases); $i++)
                 {
                        $Language_Words[$Phrases[$i]] = $Phrases[$i];
                 }
         }

        return $Language_Words;
 }
Post Reply