Author Topic: Language translation in PHP  (Read 10028 times)

reactosguy

  • Member
  • **
  • Posts: 269
  • Kudos: 2
    • Microsoft Sucks !!!
Language translation in PHP
« on: 3 August 2011, 20:17 »
So, I figured to translate words, preg_replace would do.

The idea of the script I'm trying to create is that you can insert it into an HTML page and if anybody wants to translate it, they can append ?language=(two-letter language code) to the URL and whatever is inside the dictionary will be translated.

It works with one word, but I want to make it work with multiple words, and loops aren't really helping.

Code: (PHP) [Select]
<?php
$cxn 
mysqli_connect("localhost","[hidden]","[hidden]","cgi");
$sql "SELECT * FROM dictionary";
$result mysqli_query($cxn,$sql);
$row mysqli_fetch_assoc($result);
$max mysqli_num_rows($result);
$i 0;
for(
$i=0;$i>$max;++$i)
{
$string[$i] = $rows['first_en'].$rows['restofword_en'].$rows['last_en'];
echo $string[$i];
}
if(
$_GET['language'] == $row['tolanguage'])
{
$firste $row['first_en'];
$laste $row['last_en'];
$reste $row['restofword_en'];
$firstn $row['first'];
$lastn $row['last'];
$restn $row['restofword'];
$regex "#^$firste($reste)$laste$#e";
$replacement "('{$firstn}{$restn}{$lastn}')";
for($i2=0;$i2>$max;++$i2)
{
$output[$i2] = preg_replace($regex,$replacement,$string[$i2]);
echo $output[$i2];
}
}
else
{
echo $string;
}
?>


Works great with one row like this:

Code: (PHP) [Select]
<?php
$cxn 
mysqli_connect("localhost","[hidden]","[hidden]","cgi");
$sql "SELECT * FROM dictionary";
$result mysqli_query($cxn,$sql);
$row mysqli_fetch_assoc($result);
$string $rows['first_en'].$rows['restofword_en'].$rows['last_en'];
if(
$_GET['language'] == $row['tolanguage'])
{
$firste $row['first_en'];
$laste $row['last_en'];
$reste $row['restofword_en'];
$firstn $row['first'];
$lastn $row['last'];
$restn $row['restofword'];
$regex "#^$firste($reste)$laste$#e";
$replacement "('{$firstn}{$restn}{$lastn}')";
$output preg_replace($regex,$replacement,$string);
echo $output;
}
else
{
echo $string;
}
?>


Is there any way to loop preg_replace so that if I have multiple words all of them will be translated?

Attachments: Table.
« Last Edit: 3 August 2011, 20:22 by reactosguy »

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: Language translation in PHP
« Reply #1 on: 6 August 2011, 02:01 »
I'll be honest, I haven't look at the code. But if you have it figure out for one word, this is probably what you are looking for: http://php.net/manual/en/function.strtok.php

If you don't like tokenizing, you could use this, and loop over the index of the array you get: http://www.php.net/manual/en/function.explode.php

reactosguy

  • Member
  • **
  • Posts: 269
  • Kudos: 2
    • Microsoft Sucks !!!
Re: Language translation in PHP
« Reply #2 on: 3 September 2011, 03:03 »
I think I found a solution.

Code: (PHP) [Select]
<?php
$cxn 
mysqli_connect("localhost","[hidden]","[hidden]","cgi");
$sql "SELECT * FROM dictionary WHERE tolanguage = '$_GET[language]'";
$result mysqli_query($cxn,$sql);
$rows mysqli_fetch_assoc($result);

$data "Hello, I am a user.";

if(
$_GET['language'] == $rows['tolanguage'])
{
while($row mysqli_fetch_assoc($result))
{
$data str_replace($row['oldword'],$row['newword'],$data);
}
}

echo 
$data;

Lines 1-3: PHP fetches data from a MySQL database.

Line 4: The $rows variable is created. This variable contains the data from the database in an array.

Line 6: The $data variable is created to store a string.

Line 8: PHP asks MySQL to check for any rows where the tolanguage column and the language parameter match. If they do, the translation begins!

Line 10: A while loop is created.

Line 12: The words get replaced every time the while loop loops and the variable is different.

Line 16:  The $data variable is echoed.