Jump to content

code help


JR

Recommended Posts

I dont know if there are ANY function in PHP and/or MySQL that would right away translate each word into a different one. They can only do so much as character encoding support but not translation, correct me if am wrong.

 

From what I know, especially sites built with MVC, they create a separate view for transalation or the phrase itself is stored in an array in a way that it will fetch for a certain index that will match the selected language.

 

Also, one thing to understand about direct translation is bad since different languages has different grammatical rules.

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575789
Share on other sites

I think the amount of text translation depends on how you take this task...

 

If you want to add to your translations, or change all text in the website, a database would be recommended.

 

However an array is better if its just odd words.

 

1. you could use a session, or a cookie (i recommend sessions).

2. using the country ID you could then do something like...

 

(this is only any example.)

<?php
$lang =array(
  'gb' => array( #english
   'english','german'
  ),
  'de' => array( #german
    'englisch','deutsch'
  )
);

# $_SESSION['country'] is gb
?>
choose a country:
- <?=$lang[$_SESSION['country']][0]?> <!-- english -->
- <?=$lang[$_SESSION['country']][1]?> <!-- german -->

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575813
Share on other sites

I do this. First, I create three files. One that holds the content, and one for each language. ex:

 

index.php

index_en.php

index_fr.php

 

At the start of index.php, I use the following code:

 

$language = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : "en"; // replace 'en' with 'fr' if you want the default language to be french
require_once("index_{$language}.php");

 

in index_en.php, I would have this code:

 

define("_LONDON_", "London");

 

and in index_fr.php I would have this code:

 

define("_LONDON_", "Londres");

 

And finally, anywhere that I wanted to display the city name in index.php, I would have this code:

 

echo _LONDON_;

 

You will then have to create a link that switches the value of the user's cookie from French to English and back.

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575824
Share on other sites

The majority of sites I do are bilingual (I'm in Japan), and as such, I've tried a million different methods. Trust me when I tell you that the one above is one of the easiest to maintain, though it does take some time to set up.

 

It also makes it extremely easy to add more languages in the future - you just copy the language sheets, change the definitions, and enable your cookie switching mechanism to deal with an extra cookie type.

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575832
Share on other sites

OK, I'll give you my database version. use MySQL.

 

database.php

<?
$host ="localhost";
$user ="UserName";
$pass ="PasswordHere";
$data ="databaseHere";
$connect =mysql_connect($host, $user, $pass);
$database =mysql_select_db($data, $connect);
if(!$connect||!$database)die("Database cannot connect...");
?>

 

ajax.js

function xml_request(){
var nr;
if(window.XMLHttpRequest){
	nr =new XMLHttpRequest();
}else if(window.ActiveXObject){
	try{
		nr =new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			nr =new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			nr =null;
		}
	}
}
return nr;
}
var xml =xml_request();
function changeLang(langId){
var d =new Date();
xml.open('get','ajax.php?'+d.getTime()+'&i='+langId);
xml.onreadystatechange =function(){
	if(xml.readyState ==4 && xml.status ==200){
		window.location.reload();
	}
};
xml.send(null);
};

 

ajax.php

<?
session_start();
$_SESSION['CountryId'] =$_GET['i'];
?>

 

index.php

<?
ob_start();
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript" src="ajax.js"></script>
<?
include_once 'database.php';
$lang_id =((!$_SESSION['CountryId'])?'gb':$_SESSION['CountryId']);
$sql =mysql_query("select * from `website_text` where `lang_id` ='$lang_id'")or die(mysql_error());
$lang =mysql_fetch_assoc($sql);
?>
<table width="100%" height="100%" cellspacing="0" cellpadding="0">
<tbody>
 <tr>
<td align="center">
 <form name="logonCode" method="post" action="index.php">
	  <input type="hidden" name="ref" value="<?=$_SERVER['PHP_SELF']?>" />
	  <table width="400" cellspacing="0" cellpadding="1">
	   <tbody>
	    <tr>
	     <td width="400" height="20" colspan="2">
		 <select onChange="changeLang(this.value)">
		 <option selected>-- Choose</option>
		 <option value="gb">English</option>
		 <option value="de">German</option>
		 </select>
		 </td>
	    </tr>
		<tr>
	     <td width="400" height="20" colspan="2"></td>
	    </tr>
	    <tr>
	     <td width="400" height="20" colspan="2" align="center">Logon</td>
	    </tr>
	    <tr>
	     <td width="100" height="20"><strong><?=$lang['t_username']?></strong></td>
		 <td width="300" height="20"><input type="text" name="logon_user" value="<?=$_POST['logon_user']?>" class="textinput" /></td>
	    </tr>
		<tr>
	     <td width="100" height="20"><strong><?=$lang['t_password']?></strong></td>
		 <td width="300" height="20"><input type="password" name="logon_pass" class="textinput" /></td>
	    </tr>
		<tr>
	     <td width="100" height="20"></td>
		 <td width="300" height="20"><input type="submit" name="logon_go" value="Go" class="button" style="width:50px;" /></td>
	    </tr>
		<tr>
	     <td width="400" height="20" colspan="2"><a href="#"><?=$lang['t_link_1']?></a></td>
	    </tr>
	   </tbody>
	  </table>
	 </form>
  </td>
 </tr>
</tbody>
</table>
</body>
</html>
<?
ob_end_flush();
?>

 

MySQL Query (install database)

CREATE TABLE IF NOT EXISTS `website_text` (
 `text_id` int(11) NOT NULL auto_increment,
 `lang_id` varchar(5) NOT NULL,
 `t_username` varchar(50) NOT NULL,
 `t_password` varchar(50) NOT NULL,
 `t_link_1` varchar(150) NOT NULL,
 PRIMARY KEY  (`text_id`),
 UNIQUE KEY `lang_id` (`lang_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `website_text` (`text_id`, `lang_id`, `t_username`, `t_password`, `t_link_1`) VALUES
(1, 'gb', 'Username:', 'Password:', 'Forgotten Password?'),
(2, 'de', 'Username:', 'Passwortt:', 'Passwortt Vergessen?');

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575837
Share on other sites


$string_that_was_translated = str_replace("English Word","French Word", $string_to_be_translated);

 

Simple as they come.

Hi Waynewx,

Thank you I have managed to get your code working but please could you give me the same code but I need to add more variables I need to chnge more than one word.

Link to comment
https://forums.phpfreaks.com/topic/112160-code-help/#findComment-575849
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.