Constants
Moderator: General Moderators
Constants
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?
They're all going to be in the same file. I was thinking I could split them like this
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]
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;
}
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
It would be easier to split them into different files and
Code: Select all
include('languages/english/page1.php');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.
Personally, I'd define them all as constants in a single language specific file, then include it based on the users language preference.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Split them into separate language files and don't directly worry about where they are... write a class for that.
The class can then look for the correct language file and include it itself.
Code: Select all
$lang->paint("USERNAME_TAKEN", $user_lang);For one of my websites, I've made it multi-lingual by having a database-table for each phrase required in their respective language
This is resource intensive.
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`)
);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;
}