Forum Moderators: coopster

Message Too Old, No Replies

Keeping texts outside php files

to make multi language later

         

breezeman

2:34 pm on Jun 25, 2005 (gmt 0)

10+ Year Member



I want to keep all messages etc outside php to create a multilanguage site later. What is the best way to do this?

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

ergophobe

5:03 pm on Jun 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Several ways to do it and I have done it using both a DB and files with constants or variables.

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...]

Burner

6:03 pm on Jun 25, 2005 (gmt 0)

10+ Year Member



I'll share a trick I use for you.

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

breezeman

12:08 am on Jun 26, 2005 (gmt 0)

10+ Year Member



Thanks for the help.

I think I have about 60 messages.
On one hand it sounds like a waste of memory to load them into an array or session variable for every user, on the other hand it would be an extra mysql query for every page.
Should speed and memory be a concern here or doesn't it matter a lot?

ergophobe

12:14 am on Jun 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With 60 messages, I doubt it makes a big difference whether you use a DB, a text file, or the gettext method. You have to do an extra something for each page.

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.

breezeman

8:06 am on Jun 27, 2005 (gmt 0)

10+ Year Member



Thanks, I am using the DEFINE constants and I must say it works great. I think I am going to do this also for one language sites,I find it keeps the code much cleaner and texts are much easier to update.