I have a website that I am thinking of making into both english and spanish content. I don’t have a database and all the text for the pages can be hard coded as the content won’t change. The pages in spanish can end in “s”. For example index.php?id=main.htm would be english and index.php?id=mains.htm would be in spanish – however I also want index.php?id=main&lang=spanish to return the main page in spanish
A little background:
The content for the “main window” of the website is generated based on a variable in the url – index.php?id=main. This then calls main.htm and includes that content. The code all adds the .htm to the end of the website so when I write a link it looks like this:
[code]
<a href=”index.php?id=main”>Main page</a> or
<a href=”index.php?id=mains”>Main page in spanish</a>
**Before I go on, let me tell you that I know very little about php, so the following “code” that I am going to write is most likely very wrong.
So what I was thinking is having a link at the top of the page where you can click to change to spanish. The code for that link would be something like this
[code]
<a href=”index.php?<?php echo $id ?> s”>Read this page in Spanish</a>
Obviously, once you are on a spanish page, you expect any link you click on to be the spanish version and not the english version, therefore all the links on the page should have something in them such as:
[code]
<a href=”index.php?id=main<?php if $lang=spanish echo ‘s’ ?>”>Read this page in Spanish</a>
How would something like that work?
Are there better ways? – I am sure there are.
Warning: Failed opening '$Pistachios125gr' for inclusion (include_path='.:/usr/share/pear') in /var/www/html/SDP/pistachio.htm on line 32
[/quote]
[code=php]
if(isset($_COOKIE['yourcookiename']) && $_COOKIE['yourcookiename'] == 'spanish') {
header("Location: http://yoursitename.com/spanish/yourspanishpage.php");
exit;
}
[/code]
[code=php]
if(!isset($_COOKIE['yourcookiename'])) {
// setting it for 2147483647 makes it stick for an extremly long time
setcookie("yourcookiename", "spanish", time() + 2147483647);
}
[/code]
<i>
</i><img src="images/
<?PHP
<i> </i>if(isset($_GET['lang'])){
$lang = $_GET['lang'];}
if $lang=="spanish"
{
echo "tops.jpg"
}
else
{
echo "top.jpg"
}
?>
">
[code=php]<img src="images/<?PHP
if(isset($_GET['lang'])){
$lang = $_GET['lang'];
}
if($lang == "spanish"){
echo 'tops.jpg';
}else{
echo 'top.jpg';
}
?>">[/code]
[code=php]
if(isset($_COOKIE['yourcookie']) && $_COOKIE['yourcookie'] == 'spanish') {
echo '<a href="yourenglishversion">Opinión nuestra versión inglesa</a>'; // I hope that is correct I am not too good with spanish
} else {
echo '<a href="yourspanishversion">View our Spanish Version</a>';
}
[/code]
[code=php]
<?php
$language = get_language();
if($language == 'english'){
$display = "<a href="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=spanish" xml:lang="es">Español</a><br />
<p>Lots of text in english here...</p>'; // You could use $display = file_get_contents('english_text.php'); here!
}elseif($language == 'spanish'){
$display = "<a href="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=english" xml:lang="en">English</a><br />
<p>Mucho texto en Español aquí...</p>'; // You could use $display = file_get_contents('spanish_text.php'); here!
}else{
exit;
}
echo <<< HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="resource-type" content="document" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="distribution" content="global" />
<style type="text/css">
/*<![CDATA[*/
/*]]>*/
</style>
<title>Blah blah</title>
</head>
<body>
$display
</body>
</html>
HTML;
function get_language()
{
// Make global variables more digestable
$language = $_GET['language'];
$cookie = $_COOKIE['language'];
// Check browser first language
$first_browser_language = substr($HTTP_ACCEPT_LANGUAGE,0,2);
//Validate $_GET['language']
if (empty ($language)) {
}elseif ($language == 'english') {
}elseif ($language == 'spanish') {
}else{ $language = 'english';
}
// Check for desired language in the GET variable
if (isset ($_GET['language'])) {
setcookie ('language', "$language", time()+100000000, '/', '', 0);
return($language);
// If the GET variable was empty check for desired language in the COOKIE variable
// select page to send depending on the language in the COOKIE variable (if any)
// If the COOKIE variable is empty do not return a page to the browser
}elseif
(isset ($_COOKIE['language'])) {
// ----- Validate $_COOKIE['language']
if (empty ($cookie)) {
}elseif ($cookie == english) {
}elseif ($cookie == spanish) {
}else{ $cookie = english;
}
return($cookie);
// If neither of the above give a positive result set language according to browser preference
}else{
// ----- Check if English is first prefered browser language.
if ($first_browser_language == en) {
return(english);
// ----- Check if Spanish is first prefered browser language.
}elseif ($first_browser_language == es) {
return(spanish);
// ----- Check if Spanish is a preferered language (first, second, third, etc...). If yes send spanish.html
}elseif(strstr($HTTP_ACCEPT_LANGUAGE,"es")) {
return(spanish);
}else{
return(english);
}
}
} // End function
?>
[/code]
[code=php]
// ----- Check if English is first prefered browser language.
if ($first_browser_language == en) {
return(english);
// ----- Check if Spanish is first prefered browser language.
}elseif ($first_browser_language == es) {
return(spanish);
// ----- Check if Spanish is a preferered language (first, second, third, etc...). If yes send spanish.html
}elseif(strstr($HTTP_ACCEPT_LANGUAGE,"es")) {
return(spanish);
}else{
return(english);
}
[/code]
Wherever you have the link to change it to spanish you can have it like this
[code=php]
[/QUOTE]
if(isset($_COOKIE['yourcookie']) && $_COOKIE['yourcookie'] == 'spanish') {
echo '<a href="yourenglishversion">Opinión nuestra versión inglesa</a>'; // I hope that is correct I am not too good with spanish
} else {
echo '<a href="yourspanishversion">View our Spanish Version</a>';
}
[/code]
So you would still have to make the seperate language pages?[/QUOTE]Well the data has to be stored in some form, (database, separate files, same files, etc) but you want it somewhere non public so it doesn't get accessed out of context.
Yes I did take a look at your site.I used something very similar to that function I posted. It defaulted to English either your browser is set to English or it doesn't have English or Spanish as one of its prefered languages.
Mine defaulted to english, but I could switch to spanish, and back to english. What code did you use for that switch.
That is the part I am confused about.[/QUOTE]
Yes, I understand that. Could you post the code that you use to "force" the change to spanish from english or vice-versa.First I already have posted the code. Go back and read the thread again. Also id=main is a total useless and unnecessary query string. Use proper URLs without query strings if it is possible.
Is it as simple as creating a link like index.php?id=main&language=spanish[/QUOTE]
[CODE]<?php
$language = get_language();
if($language == 'english'){
$display = "<a href="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=spanish" xml:lang="es">Español</a><br />
$display = file_get_contents('english.php');
}elseif($language == 'spanish'){
$display = "<a href="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=english" xml:lang="en">English</a><br />
$display = file_get_contents('spanish.php');
}else{
exit;
}[/CODE]
$display = file_get_contents('english.php');
[code=php]<?php
$language = get_language();
if($language == 'english'){
$display = file_get_contents('english.php');
}elseif($language == 'spanish'){
$display = file_get_contents('spanish.php');
}else{
exit;
}
[/code]
[code=php]<?php
$language = get_language();
if($language == 'english'){
include(english.html);
}elseif($language == 'spanish'){
include(spanish.html);
}else{
exit;
}
function get_language()
{
// Make global variables more digestable
$language = $_GET['language'];
$cookie = $_COOKIE['language'];
// Check browser first language
$first_browser_language = substr($HTTP_ACCEPT_LANGUAGE,0,2);
//Validate $_GET['language']
if (empty ($language)) {
}elseif ($language == 'english') {
}elseif ($language == 'spanish') {
}else{ $language = 'english';
}
// Check for desired language in the GET variable
if (isset ($_GET['language'])) {
setcookie ('language', "$language", time()+100000000, '/', '', 0);
return($language);
// If the GET variable was empty check for desired language in the COOKIE variable
// select page to send depending on the language in the COOKIE variable (if any)
// If the COOKIE variable is empty do not return a page to the browser
}elseif
(isset ($_COOKIE['language'])) {
// ----- Validate $_COOKIE['language']
if (empty ($cookie)) {
}elseif ($cookie == english) {
}elseif ($cookie == spanish) {
}else{ $cookie = english;
}
return($cookie);
// If neither of the above give a positive result set language according to browser preference
}else{
// ----- Check if English is first prefered browser language.
if ($first_browser_language == en) {
return(english);
// ----- Check if Spanish is first prefered browser language.
}elseif ($first_browser_language == es) {
return(spanish);
// ----- Check if Spanish is a preferered language (first, second, third, etc...). If yes send spanish.html
}elseif(strstr($HTTP_ACCEPT_LANGUAGE,"es")) {
return(spanish);
}else{
return(english);
}
}
} // End function
?>[/code]
<i>
</i><a href="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=english" xml:lang="es">English</a>
<i>
</i>
<?PHP
<a href="http://" .$_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'].'?language=english" xml:lang="es">English</a>
?>
[code=php]<a href="http://<?php print $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; ?>?language=english" xml:lang="en">English</a>
<a href="http://<?php print $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; ?>?language=spanish" xml:lang="es">Español</a>[/code]
[code=php]<?php
$description ='';
$keywords ='';
$stylesheet ='@import url( css/default.css );';
$title ='Page title';
//Any Javascript for the page head section here
$javascript ='';
//Get top of the page
require('../templates/template.php'); //Path and filename of template
top_of_page($description, $keywords, $stylesheet, $title, $javascript);
echo <<<HTML
<!-- Content here -->
HTML;
//Get bottom of the page
bottom_of_page();
?> [/code]
[code=php]<?php
### HTML for top portion of page ###
function top_of_page($description, $keywords, $stylesheet, $title, $javascript)
{if (!empty($stylesheet)) {
$stylesheet = '<style type="text/css">
/*<![CDATA[*/
' . $stylesheet . '
/*]]>*/
</style>';
}
if (!empty($javascript)) {
$javascript = '<script type="text/javascript">
/*<![CDATA[*/
' . $javascript . '
/*]]>*/
</script>';
}
echo <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="resource-type" content="document" />
<meta name="description" content="$description" />
<meta name="keywords" content="$keywords" />
<meta name="distribution" content="global" />
$stylesheet
$javascript
<title>$title</title>
</head>
<body>
END;
}
function bottom_of_page()
{ echo <<<END
</body>
</html>
END;
}
?>[/code]
why are they so bad? ?[/QUOTE]They are bad because they iinflate html and make page difficult to access for the handicaped. Also external CSS is cached for use in subsequent pages.
0.1.9 — BETA 5.5