Log in

View Full Version : multi-language site + slideshow



madu
12-24-2010, 09:37 AM
Hi, i'm constructing a multi-language site. in the top of the page there are links to change the language. by using echo
eg:
<title><?php echo $lang['PAGE_TITLE'];?></title>the variables send to a php (say common.php) file. then this php file calls another 3 php files (say lang.en.php, lang.de.php & lang.es.php) which contain the content of different 3 languages.
eg:in lang.en.php
$lang['PAGE_TITLE'] = 'Testing Page';there is no issue with changing the content.

but there is a slideshow. for different 3 languages there should be different slideshow images.
since in this site there is no database it was hard to me.
if you can tell me how can i call the common.php and how the define the path to different images in lang.en.php, lang.de.php & lang.es.php , it would be grate help. hope you can understand my problem. all replies are very welcome.
thank you.

Schmoopy
12-24-2010, 03:11 PM
Can you post the code you have for populating the slideshow images?

It would be good to see how the files are laid out and when you're calling them on the page.

You want to create arrays depending on the language the user's chosen, and then in the language files, have something like:

lang.es.php:


$lang['slideshow'] = array('imagen1.jpg', 'imagen2.jpg');


lang.en.php:


$lang['slideshow'] = array('image1.jpg', 'image2.jpg');


common.php:


foreach($lang['slideshow'] as $image) {
echo '<img src="' . $image . '" alt="' . $image . '" />';
}


I realise that the HTML above isn't what you'd be using for the slideshow, but the principle is the same. Whether you need to populate a JavaScript array or anything similar, you can just loop through that slideshow array and pull out the values you need.

Depending on how your script's currently set up though, the above code may need altering, but if you can post the code it will be easier to see what needs to be done.

madu
12-24-2010, 06:00 PM
thanx for reply me....

this is the slide show,

<div class="rap">
<img src="extra/slide1.jpg" width="860" height="270" />
<img src="extra/slide2.jpg" width="860" height="270" />
<img src="extra/slide3.jpg" width="860" height="270" />
<img src="extra/slide4.jpg" width="860" height="270" />
</div>

this is the common.php

session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;

case 'sin':
$lang_file = 'lang.de.php';
break;

case 'tam':
$lang_file = 'lang.es.php';
break;

default:
$lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
i have, what are the language files in the common.php.

and this is the lang.en.php

<?php
/*
------------------
Language: English
------------------
*/

$lang = array();

$lang['PAGE_TITLE'] = 'Testing Page';

// Menu

$lang['Menu_Home'] = 'Home';

.......................
................................

hope now you have clear idea. please help me...
tahnk you very much

Schmoopy
12-24-2010, 06:29 PM
Change your language files to look like:

lang.en.php


<?php
/*
------------------
Language: English
------------------
*/

// Define the images you want for this language in the array below
$images = array('slide1.jpg', 'slide2.jpg', 'slide3.jpg', 'slide4.jpg');

$lang = array();

$lang['PAGE_TITLE'] = 'Testing Page';

// Menu

$lang['Menu_Home'] = 'Home';

$lang['slideshow'] = $images;
?>


Replace the current HTML you have for outputting the slideshow with:



<div class="rap">
<?php
foreach($lang['slideshow'] as $image) {
?>
<img src="extra/<?php echo $image; ?>" width="860" height="270" />
<?php
}
?>
</div>


You don't need to make any changes to common.php for this to work.
Obviously you need to change the $images variable in each of your language files with whatever images you want.

Good luck!

madu
12-25-2010, 04:59 AM
thank you Schmoopy. but still there is something in the code.
when i change these modifications there is an error called ": Invalid argument supplied for foreach() in C:\wamp\www\Test\index.php on line "
then i change the code

<div class="rap">
<?php
foreach($lang as $image) {
?>
<img src="extra/<?php echo $image['slideshow']; ?>" width="860" height="270" />
<?php
}
?>
</div>

then there is no error and no out put also.

the lang.en.php placed in folder called language. and images are placed in folder called images.
then this the ang.en.php

<?php
/*
------------------
Language: English
------------------
*/


// Define the images you want for this language in the array below
$images = array('../images/slide1.jpg', '../images/slide2.jpg', '../images/slide1.jpg', '../images/slide2.jpg');

$lang = array();

// slideshow
$lang['slideshow'] = $images;

$lang['PAGE_TITLE'] = 'Testing Page';

// Menu

$lang['Menu_Home'] = 'Home';
$lang['Menu_About_us'] = 'About Us'
..............
....................
but still no image shown in index page. what has happened to this code. what is the error? your help much needed.
thanx a lot.

Schmoopy
12-25-2010, 11:11 AM
Ok here's how it should look. Your foreach statement should be looping through $lang['slideshow'], not just $lang.

Here's a version that combines all the code into one file, so you can see how it should flow:


<?php
// START COMMON.PHP

session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;

case 'sin':
$lang_file = 'lang.de.php';
break;

case 'tam':
$lang_file = 'lang.es.php';
break;

default:
$lang_file = 'lang.en.php';

}
// Commented out the line below because we don't need the include, hard coded in below
//include_once 'languages/'.$lang_file;

// START LANG.EN.PHP

/*
------------------
Language: English
------------------
*/

// Define the images you want for this language in the array below
$images = array('slide1.jpg', 'slide2.jpg', 'slide3.jpg', 'slide4.jpg');

$lang = array();

$lang['PAGE_TITLE'] = 'Testing Page';

// Menu

$lang['Menu_Home'] = 'Home';

$lang['slideshow'] = $images;

// END LANG.EN.PHP

// START COMMON.PHP
?>
<div class="rap">
<?php
foreach($lang['slideshow'] as $image) {
?>
<img src="extra/<?php echo $image; ?>" width="860" height="270" />
<?php
}
?>
</div>

<?php
// END COMMON.PHP
?>


I have this running on my local server and it outputs:



<div class="rap">
<img src="extra/slide1.jpg" width="860" height="270" />
<img src="extra/slide2.jpg" width="860" height="270" />
<img src="extra/slide3.jpg" width="860" height="270" />
<img src="extra/slide4.jpg" width="860" height="270" />
</div>


Let me know when you get this running.

madu
12-25-2010, 12:26 PM
YES! it's working!!! :) thank you.....
when combining common.php & lang.en.php like your last post, it was showing 2 slide shows. then i remove

<div class="rap">
<?php
foreach($lang['slideshow'] as $image) {
?>
<img src="extra/<?php echo $image; ?>" width="860" height="270" />
<?php
}
?>
</div> from your new combine code. after it was working.

but combining common.php & lang.en.php, how to define lang.de.php & lang.es.php for other 2 languages ???

if we can use again

include_once 'languages/'.$lang_file; and use difference 3 php files for 3 languages it would be fine and nice.

please help me Schmoopy. thnx again for your kindly help. :)

Schmoopy
12-26-2010, 01:38 AM
To get it working for all 3 files, and additional language files if needed, do the following:


common.php:



<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;

case 'sin':
$lang_file = 'lang.de.php';
break;

case 'tam':
$lang_file = 'lang.es.php';
break;

default:
$lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>
<div class="rap">
<?php
foreach($lang['slideshow'] as $image) {
?>
<img src="extra/<?php echo $image; ?>" width="860" height="270" />
<?php
}
?>
</div>


lang.en.php:



<?php
/*
------------------
Language: English
------------------
*/


// Define the images you want for this language in the array below
$images = array('../images/slide1.jpg', '../images/slide2.jpg', '../images/slide1.jpg', '../images/slide2.jpg');

$lang = array();

// slideshow
$lang['slideshow'] = $images;

$lang['PAGE_TITLE'] = 'Testing Page';

// Menu

$lang['Menu_Home'] = 'Home';
$lang['Menu_About_us'] = 'About Us';

?>


lang.de.php:



<?php
/*
------------------
Language: Dutch
------------------
*/


// Define the images you want for this language in the array below
$images = array('../images/dutch1.jpg', '../images/dutch2.jpg', '../images/dutch3.jpg', '../images/dutch4.jpg');

$lang = array();

// slideshow
$lang['slideshow'] = $images;

$lang['PAGE_TITLE'] = 'Danish Page';

// Menu

$lang['Menu_Home'] = 'Home'; // (whatever "Home" is in Dutch)
$lang['Menu_About_us'] = 'About Us'; // (whatever "About Us" is in Dutch)

?>


lang.es.php:



<?php
/*
------------------
Language: Spanish
------------------
*/


// Define the images you want for this language in the array below
$images = array('../images/diapositiva1.jpg', '../images/diapositiva2.jpg', '../images/diapositiva3.jpg', '../images/diapositiva4.jpg');

$lang = array();

// slideshow
$lang['slideshow'] = $images;

$lang['PAGE_TITLE'] = 'Pagina de Prueba';

// Menu

$lang['Menu_Home'] = 'Inicio';
$lang['Menu_About_us'] = 'Quiénes somos';

?>


That should do it for you.

Let me know how you get on.

madu
12-26-2010, 03:39 AM
YES! YES!! YES!!! it's working. thank you Schmoopy. thank you very much. :)

Schmoopy
12-26-2010, 03:45 AM
Glad it worked for you.

Good luck with your site! :)

M rosi
12-26-2010, 03:49 AM
would you please tell me how can run the css background images through a php file ?
background:url(content-bg.png);

Schmoopy
12-26-2010, 04:01 AM
How do you mean? Variable background images?

If that is what you mean, you can use something like this:

style.php:


<?php
header("Content-type: text/css");

$image = 'content-bg.png';
?>

#element{
background:url(<?php echo $image; ?>);
}


HTML:


<head>
<link rel="stylesheet" href="style.php" type="text/css">
</head>


If this isn't what you meant, please try and explain in more detail.

M rosi
12-26-2010, 04:16 AM
no dear i mean, if there is a style.css file with some bg images. then how can call that bg images from a php file ?

style.css

body {
font-family:Arial, Helvetica, sans-serif, A12Yasarath;
font-size:0.7em;
}

h1 {
position:absolute;
top:9px;
}

section#content {
background:url(content-bg.png) left top no-repeat;
border-right:1px solid #e2e4ea;
border-left:1px solid #e2e4ea;
}

footer{
height:99px;
background:url(footer-bg.png) left top no-repeat;
}

as an example the bg images(highlighted in red color), i want call from a php file.
is it possible ?

Schmoopy
12-26-2010, 02:32 PM
You can't get the property of a style via PHP. Unless the stylesheet has PHP variables in it.

You can however do it with JavaScript.

If you're using JQuery, you can do:



var bg = $('#content').css('background');
// bg now contains whatever background property you assigned to the content element.


If you're not using JQuery, ask in the JavaScript forum about this (not sure how you'd do this is native JavaScript, probably fairly similar - element.style.background or something)

djr33
12-26-2010, 09:32 PM
You cannot do anything dynamic in an external .css file. You could use Javascript to alter it.

Or here are two options that might be easier:
1. Don't use an external .css file. Just put it in <style> tags in the head section. Then you can generate the SRC/HREFs using PHP, with echo just like outputting text.
2. You can actually setup a dynamic ".css" file using PHP. In fact, it would be something like "mycss.php" and you would output .css text with a header that says it is a CSS file. Then use that (even though it isn't .css) in your head section like a regular external .css file.