Forum Moderators: coopster

Message Too Old, No Replies

Content switching

         

simon5

4:39 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



I'm very new (and I mean very new) to PHP.

I'm working on a bilingual site. Users can change the language by clicking text links which then switches stylesheets (using PHP- stylesheet displays one div while the other div is not displayed eg display: none;). I don't really want to do it this way as I want to keep the stylesheets for presentation only. Is there a way of switching div's using PHP or another method which will produce the same result.

ergophobe

8:34 pm on Jan 11, 2005 (gmt 0)

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



Simon,

Welcome to WebmasterWorld. I think you'll find the new user guide and welcome quite useful

[webmasterworld.com...]

Also, please read the forum charter (link at the upper left of each page).

As for your question, there are many ways to approach it. The problems I see with your current method are
- you are uploading lots of content that isn't getting used
- I think maintenance will be a nightmare
- adding a third language would be a ture nightmare

There are a few common ways to build bilingual sites, some of which depend on the operating system and some of which do not. To get you started, here is one method that would work with any server setup that supports PHP.

First, set up your script so that you print interface text by using constants, like so:

define('HOME', 'Home');
define('PAGE2', 'My Second Page');

Then in your navigation area

<li><a href="http://example.com/"><? echo HOME;?></a></li>
<li><a href="http://example.com/page2.php"><? echo PAGE2;?></a></li>

Anytime you have text in your interface, you use a constant that is assigned a value.

Now, to make this bilingual, at the top of your page, you set a language. We can talk about ways to do that in a moment.

$lang = 'en';

Now, I put my constants in a directory that is named for the language

/www <= root dir
/www/langs/en <= lang dir

Within each language directory are a set of files that always have the same content and the same name, but the content is translated into different languages.

At the top of my script, I include the language file for the navigation interface

include("/path/to/language/files/" . $lang . "/nav_text.php");

So in /www/en/ I have the file that defines the constants HOME and PAGE2 as above. However, in /www/fr/ I have a file that defines them as

define('HOME', 'Accueil');
define('PAGE2', 'Ma Deuxième Page');

Now if I set the language equal to French,

$lang = 'fr';

and include the language files

include("/path/to/language/files/" . $lang . "/nav_text.php");

My navigation is now in French.

I can do the same thing for text presented as images.

I can also do the same thing with content if I either draw the content out of a database (probably a bit hard for you at this point) or just mirror the directory tree within the /langs/ directory.

In that case, I get to the content section and I have the page in English and in French.

in page2.php I check to see if a French version exists, and if so, that's what I give them.

if (file_exists('/path/to/language/files/' . $lang . '/page2.php')
{
include('/path/to/language/files/' . $lang . '/page2.php')
}
else
{
include('/path/to/language/files/' . $default_lang . '/page2.php')

}

Was that thoroughly confusing? I hope not.

Tom

mcibor

8:55 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is:

<?PHP
if($_GET["lang"] == "us") include("us.html");
elseif($_GET["lang"] == "pl") include("pl.html");
else include("uk.html");
?>

or

<?PHP
if(isset($_GET["lang"])) $lang = $_GET["lang"];
else $lang = "uk";

switch $lang
{
case "us" your_code_us;
case "pl" your_code_pl;
case "no" your_code_norway;
default your_code_uk

}
?>

Hope it helps you somehow.
Michal Cibor

jshpro2

8:58 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Just make a folder called languages and set up the structure like this:

languages/
english.php
spanish.php

Then in english.php put:
define("hello_txt", "hello");
and in spanish.php put:
define("hello_txt", "hola");

Then just do:


<?
$username='jshpro2';
$lang='eng';

switch ($lang) {
case 'span':
include_once('languages/spanish.php');
break;
default:
include_once('languages/english.php');
break;
}

echo (hello_txt.' '.$username);
?>

ergophobe

12:22 am on Jan 13, 2005 (gmt 0)

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



Like jshpro2 said.

Actually, that's what I was trying to say, but in a more roundabout way.

Does anybody know the other method I'm talking about, which uses a *nix app to provide multilingual support? I was playing around with one script that used it, but I can't remember it at all now.

ergophobe

9:50 pm on Jan 13, 2005 (gmt 0)

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



For the record, the gnu package I was thinking of is gettext. I was just drawing a blank on the name. It's pretty slick for internationalization.

[gnu.org...]

[us3.php.net...]

coopster

10:13 pm on Jan 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You may also want to have a look at Apache Content Negotiation [httpd.apache.org], which is ideal for multi-language sites.