Page 1 of 1

Constants

Posted: Sat Feb 24, 2007 10:36 am
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?

Posted: Sat Feb 24, 2007 10:51 am
by feyd
Splitting them into several files that are logical groupings. A function is useless really if you're using actual constants.

Posted: Sat Feb 24, 2007 11:15 am
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]

Posted: Sat Feb 24, 2007 11:48 am
by nickvd
It would be easier to split them into different files and

Code: Select all

include('languages/english/page1.php');

Posted: Sun Feb 25, 2007 2:45 am
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.

Posted: Sun Feb 25, 2007 3:46 am
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.

Posted: Sun Feb 25, 2007 5:13 am
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.

Posted: Sun Feb 25, 2007 10:06 am
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;
 }