JR Posted June 27, 2008 Share Posted June 27, 2008 I have a travel agents website I am translating into French. I need to alter my destinations which are entered in the database in English to display in French What specific code would I need to alter mysql field $destination For example London needs to become Londres England needs to become Angleterre Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 I will do the translation. I need the code to alter London to Londres only on the page it is displayed Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 You would need some kind of dictionary, or an array which has the original words and then the translated words, this could also work with two fields in a database. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 You would need some kind of dictionary, or an array which has the original words and then the translated words, this could also work with two fields in a database. I agree, though this might be costly. Alternatively, aside from database, you may also use xml or array. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 I think it is a code for array I need. All I want to be able to do is me manually input Londres instead of London so it only displays as Londres on the French pages. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 27, 2008 Share Posted June 27, 2008 Then an array would work, but you would need some fail safe way of searching the pages for each particular word in the array and then replacing it with the translated work. Take a look at http://uk2.php.net/manual/en/function.preg-replace.php and regular expressions. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 I have looked at this but this is hard. Was hoping someone could just give me easy code with my variables Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 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 --> Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 It is just an array rather than database. DO I add this code at the top of the page or where the destination is displayed on the page please? Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 the array must be defined before you try and implement it on the page... Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 Hi, what do you mean defined? Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 Isnt there a way I can just do an array to change London to Londres etc... Quote Link to comment Share on other sites More sharing options...
haku Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 Thanks but I was really looking for something simple I could do on the page! Quote Link to comment Share on other sites More sharing options...
waynew Posted June 27, 2008 Share Posted June 27, 2008 $string_that_was_translated = str_replace("English Word","French Word", $string_to_be_translated); Simple as they come. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 $string_that_was_translated = str_replace("English Word","French Word", $string_to_be_translated); Simple as they come. Hi Waynewex, this sounds great, in the $string_that_was_translated do I put $destination sorry if I seem thick! Quote Link to comment Share on other sites More sharing options...
haku Posted June 27, 2008 Share Posted June 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
waynew Posted June 27, 2008 Share Posted June 27, 2008 Yea, just put destination there. My example was pretty generic. Mess around with that and have a look at http://ie2.php.net/str_replace Hope that helps. Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 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?'); Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 $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. Quote Link to comment Share on other sites More sharing options...
JR Posted June 27, 2008 Author Share Posted June 27, 2008 $string_that_was_translated = str_replace("English Word","French Word", $string_to_be_translated); Can someone please tell em what I would do if I wanted to add more variables to this code? ie another english word to be altered into another french word Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.