Forum Moderators: coopster
I was thinking of a session variable language, and call the text output with a function that receives the messageid and language id and so returns the text.
Is it wise to store all these messages in mysql or inside the function (if messageid=1 etc)? Does it make a difference in speed if the function handles all or if the message content is received by mysql query?
thanks,
Marco
I would only use the DB method if you have small number, they will be frequently and dynamically updated by non-coder site admins and you can grab most of them in one go.
Otherwise, you can have language files with phrases set in constants as in
/site/languages/en/includes/header.php has
define('NOT_A_CONSTANT', "This is not a constant");
/site/languages/fr/includes/header.php has
define('NOT_A_CONSTANT', "Ceci n'est pas une constante");
Then in
/site/includes/header.php
include ("/site/languages/" . $current_language . "/includes/header.php");
echo "<h1>" . NOT_A_CONSTANT . "</h1>";
More sophisticated than either of these is to use gettext which is designed specifically for localization. I've played with this, but havent' really used it. If multi-language versions were really important to me, though, I would give this a very serious look.
[gnu.org...]
[us3.php.net...]
I'll drop a cookie to set the site language. Then in my script I'll do something like:
if ($_COOKIE['sitelanguage']) {
$language = $_COOKIE['sitelanguage'];}
else {
$language='english';}
Then I set up a language array so I call several different language parameters:
if ($language == 'english') {
$lang['$language'] = array ('directory' => "$language", 'speech' => "Language", 'desc' => "English"); }
Next I'll use a language directory called language on my server, put a directory in for each language and call a set of includes based on that language:
include('language/'.$lang['$language'][directory].'/texts.php');
In this case, in language/english/texts.php, I have a series of statements like:
<?php
DEFINE("_HOME","Home");
DEFINE("_YES", "Yes");
DEFINE("_NO", "No");
DEFINE("_SEARCH", "Search");
DEFINE("_FIRST_NAME", "First Name");
DEFINE("_THANK_YOU","Thank you for your comment. This will be "
."reviewed and published in the next 24-48 hours.");
?>
I can then have any language properly displayed on the fly by using the codes I've defined instead. eg:
<a href="http://somesite.com/"><?php echo _HOME;?></a>
Hope it helps,
Burner
I would guess the solution of using constants and including the appropriate file based on the language variable that is in the session (or whereever) would be fastest and simplest. It's the system used by a variety of CMS and shopping carts and so on.
gettext is the preferred method of localization for GNU projects, but it's definitely more complicated and depends on having a box with gettext installed.