Jump to content

[SOLVED] PHP Multi-Language code help


worldcomingtoanend

Recommended Posts

I am a newbie in PHP and not yet expert enough to write complex code.  however i can read and understand php code. my question is: I hv created a website in german and hv also manually  translated the site into english. How do i create a PHP code such that if a user clicks on a button they get transferred to the equivalent page in another language.  I hv an idea that the code might be something like the one i hv created below. please help me if u can.  thanks so much 

<?php
If (isset ($_POST[txtclick])) {
$button = $_POST[txtclick];
$current_page=$_POST[currentpage];
If (( $current_page==GN) &&  ($button==checked)) {
     $current_page== EN;
}else if (( $current_page==EN) &&  ($button==checked)) {
     $current_page== EN;
}
}else{
  Break;
}
?>      

Link to comment
Share on other sites

You apparently have a field called "txtclick" (although you are referencing the field incorrectly - the field name should be in quote marks). And you are assuming the value indicates that the user want's to change their language from the current page. That would work, but I think it would make more sense to pass a value to explicitly set the language. So, on the German pages pass a value of "en" and on the English pages pass a value of "de" (using the standard lanugage abbreviations).

 

I am also assuming that you have parrallel pages for each language as opposed to one page which uses different content based on the language. It would be helpful to know how you named the pages in order to show how you would change the page. But, generally speaking I would put code at the top of the page to do the following

 

1. Check if the "change Language" option was POSTED.

2. If yes, then get the current URL

3. Modify the current URL to the other language equivalent

4. Do a header redirect to the new URL

Link to comment
Share on other sites

You apparently have a field called "txtclick" (although you are referencing the field incorrectly - the field name should be in quote marks). And you are assuming the value indicates that the user want's to change their language from the current page. That would work, but I think it would make more sense to pass a value to explicitly set the language. So, on the German pages pass a value of "en" and on the English pages pass a value of "de" (using the standard lanugage abbreviations).

 

I am also assuming that you have parrallel pages for each language as opposed to one page which uses different content based on the language. It would be helpful to know how you named the pages in order to show how you would change the page. But, generally speaking I would put code at the top of the page to do the following

 

1. Check if the "change Language" option was POSTED.

2. If yes, then get the current URL

3. Modify the current URL to the other language equivalent

4. Do a header redirect to the new URL

 

thank you so much for your response and also giving me a clue. my pages are exactly the same, same content, same everything.  I will try to follow the psuedocode u hv given and will work flat out tonight and will come back tomorrow if i fail.

 

Link to comment
Share on other sites

ok I am back after spending an entire night struggling with php.  Now after a thorough  scrutiny of my work I am realising that the best thing will be for me to come up with a script that if a user clicks on their preferred language they will be redirected to the parallel translated page. I still have an idea of how the script should be like but i am failing to come up with something to the dot.  below is the code which is not perfect but which conveys an idea of what i need to have.

<?php
$current_url=$_POST['clickedbutton'];
$next_url=$current_page.html;
if ($current_url == $next_url) {
header ("$next_url");
}else {
exit;
}
?>

  I have 2 languages,and they are all translated and their respective folders are ready with all the information.  Currently if a user clicks on their preferred language it takes them to the start of the site instead of just translating the current page.  I hv created multilanguage sites before with php & mysql but currently i hv no mysql and the authorities around seem to hv an attitude with mysql.  Please help if u can.  thanks in advance

Link to comment
Share on other sites

Show two examples of the URL for a page, the URL to the English version and the URL to the German version.

I have 2 folders(english & german) with complete translated text, etc.  I need to create the website in such a way that if a user clicks on their preferred language even if they r deep within the page, the translation will simply translate that current page and not take the user to the start page of their preferred language like what I currently have.  Yesterday somebody gave me a clue with 4 points which are:

1. Check if the "change Language" option was POSTED.

2. If yes, then get the current URL

3. Modify the current URL to the other language equivalent

4. Do a header redirect to the new URL

following that I came up with the following codes and I am stuck on number 3 which I need help on.


1. Check if the "change Language" option was POSTED.
<a href="index.html?lang=en"><img src="images/en.png" /></a>
<a href="index.html?lang=de"><img src="images/de.png" /></a>

2. If yes, then get the current URL
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

3. Modify the current URL to the other language equivalent
I am stuck here.  please help me how can i modify the currentURL to the other equivalent.  I have hundreds of pages and i am clueless really.

4. Do a header redirect to the new URL
<?php
header("Location: http://mygerman site/");
exit();
?>

i could hv easily done this with mysql but i cant becoz of circumstances beyond my control. thank you for helping me.

Link to comment
Share on other sites

OK, create a single page to change the language for any of your pages (say changelanguage.php) using the following code:

<?php

//Get the URL of the referrer page
$last_page  = $_SERVER['HTTP_REFERER'];

//Set the value for the new language - default is german
if (isset($_GET['lang']) && ($_GET['lang']=='en' || $_GET['lang']=='de'))
{
    $new_lang  = $_GET['lang'];
}
else
{
    $new_lang  = 'de';
}

//Set the value of the old language
$old_lang = ($new_lang=='en') ? 'de' : 'en';

//Replace the language with the new language in the referrer URL
$new_page = str_replace("/{$old_lang}/", "/{$new_lang}/", $last_page);

//Redirect to new page
header('Location: '.$new_page);

?>

 

Then just create your links to change the language for english and german like this on all your pages

<a href="http://mysite.com/changelanguage.php?lang=de">Change to German</a>
<a href="http://mysite.com/changelanguage.php?lang=en">Change to English</a>

Link to comment
Share on other sites

The above is giving me an error that says

The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
    *   This problem can sometimes be caused by disabling or refusing to accept
          cookies.

 

anyway since its  late here i will try to go to my room and study your code and see if i will find a solution.  will check in tomorrow morning.  thanks again.

Link to comment
Share on other sites

Change the last line in that code to echo the $new_page value. Then see if the value makes sense. I did some basic testing and it worked fine for me.

 

In fact I went back just now tested it both in IE & FF in my XAMPP environment (on Windowz) and then tested again in both IE & FF on my Linux host. No problems.

 

Here is the full code from my test files:

 

changelanguage.php

placed at root of site folder: http://www.mysite.com/changelanguage.php)

<?php

//Get the URL of the referrer page
$last_page  = $_SERVER['HTTP_REFERER'];

//Set the value for the new language - default is german
if (isset($_GET['lang']) && ($_GET['lang']=='en' || $_GET['lang']=='de'))
{
    $new_lang  = $_GET['lang'];
}
else
{
    $new_lang  = 'de';
}

//Set the value of the old language
$old_lang = ($new_lang=='en') ? 'de' : 'en';

//Replace the language with the new language in the referrer URL
$new_page = str_replace("/{$old_lang}/", "/{$new_lang}/", $last_page);

//Redirect to new page
header('Location: '.$new_page);

?>

 

English test file:

Placed at http://www.mysite.com/en/test.htm

<html>
<body>
<h1>English</h1>
<a href="http://www.mysite.com/changelanguage.php?lang=de">Change to German</a><br>
<a href="http://www.mysite.com/changelanguage.php?lang=en">Change to English</a>
</body>
</html>

 

German test file:

Placed at http://www.mysite.com/de/test.htm

<html>
<body>
<h1>German</h1>
<a href="http://www.mysite.com/changelanguage.php?lang=de">Change to German</a><br>
<a href="http://www.mysite.com/changelanguage.php?lang=en">Change to English</a>
</body>
</html>

Link to comment
Share on other sites

mjdamato!!!  honestly I dont know what I can do for u.  Last night i scrutinized your code and I noticed that the error i kept seeing was because of my faulty url Iink.  I was writing this /firstfolder/secondfolder/changelanguage.php  instead of http://www.mysite.com/firstfolder/secondfolder/changelanguage.php.  I also got an answer from your code on why i was getting confused about grabbing the next url.  I noticed that I need to have the same exact folders in both directories so that it can be easy for me to specify the next url.  Thanks again you are indeed a guru...

 

Link to comment
Share on other sites

I noticed that I need to have the same exact folders in both directories so that it can be easy for me to specify the next url.

 

That is precisely why I asked to see the URLs for an English vs German page. If there is not going to be some programatic way of determining the corresponding page from one language to another they you will have to build a list of every page and it's corresponding page in the other language. Much easier to just build the site in a format that it can be done programatically.

 

Also, with only two languages to support, having two copies of the site is easy enough to manage. But, another method of accomplishing this - which would be a must if supporting several lanaguages - would be to build all your pages with placeholders for the text and have separate pages that hold the appropriate text for each placeholder based upon the laguage. Then when the page loads it will detemine the correct language, load the appropriate language and then replace the placeholders with the actual text. This way you have only 1 page and would just need to manage the language files.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.