Sorry if the title is not enough clear, I didn’t know how to write it better.
The situation is that I a have a cron job that sends mails to users. I have language files, and depending in the configuration of the user I want to send the mail in his language.
But I don’t know how to do this.
Each language file have constants definitions like:
en.php define(‘DATE’,’Date’); define(‘TIME’,’Time’);
es.php define(‘DATE’,’Fecha’); define(‘TIME’,’Hora’);
And I need to display the correct labels depending the user language, but I’m in a while loop:
while ($row = mysql_fetch_array($res)) {
if ($row['lang'] == en) //load the english labels
}
So I think I can’t use something like "include_once" in each iteration.
The problem is that you use PHP Constants for it so once they are set, you can’t really change them within the script. Try to rewrite your constants and all references of them into variables.
Let’s say that your language files are named like this
lang.code.phpand the script that will send the email is insendemail.php. You can do something like this:You can then reference terms in your language files using their keys in the
$termsarray. Of course you’d have to create validation checks for the language files, directory traversals, etc. But that’s one way to implement it.If you can’t rewrite your code then I suggest putting all of the email contents in one file and use
file_get_contents()to fetch the email content via HTTP. Inside that file, you’d have a conditional which loads the language file you need.Let’s say that the file that will generate the email contents is called
emailcontent.php, you can do something like this:Of course you need to add some security checks to prevent local file inclusion, access to emailcontent.php, etc. But this is the gist of it. However this has a big overhead in terms of latency and repeated compilation of your PHP code. If there was any other way to clear the global score for each email, then that is the better way to go. But this is the only way I can think of if you can’t rewrite your code.