I’m trying to successfully convert parts of my website from one language to another with an api from aperitum.org, but the conversion is bad.
I begin by having an array of variables representing longer sentences with HTML code. Then I aim to have the longer sentences converted to foreign languages. and each language eventually will have its own html file which is the json version of the data for that language. Then in any php script, I should be able to get the right text when calling gettextinlanguage(“por”,”variable”) for example. Sometimes I receive the right results and sometimes I don’t.
I did notice that the results are very bad if I use new lines as part of my delimiter.
I tried using HTML comments for my delimiter but that didn’t help much.
I checked the PHP error logs and they did not help me.
Is my language converter that bad or am I doing something wrong?
And I cannot feed it JSON directly because it doesn’t accept JSON.
Code is shown below for you to play with:
function gettextinlanguage($lang,$txt){
$res=json_decode(file_get_contents(langfol()."/".$lang.".htm"),true);
return $res[$txt];
}
function langfol(){
return "/path/to/languagefiles";
}
function loadwords(){
//our words.
$words=array(
"Ball"=>'This <b ID="English">ball</p> is bouncy',
"Apple"=>'Apples <i ID="England">are</i> red'
);
}
function buildbaseconv(){
//build our english data in JSON format
$fol=langfol();$w=loadwords();
$ww='';
foreach ($w as $n=>$v){
$ww.=$v."<!--delim1-->";
}
$x=fopen($fol."/eng.htm","w");
fwrite($x,rawlangtojson($ww));
fclose($x);
}
function rawlangfromjson($fi){
$d=json_decode($fi,true);
if (!is_array($d)){
echo "ERROR: No Arrayn";
return;
}
$ww='';
foreach ($d as $naa=>$vaa){
$ww.=$vaa."<!--delim1-->";
}
return $ww;
}
function rawlangtojson($fi){
if (strpos($fi,"<!--delim1-->")===false){
echo "ERROR: data delimiter not received!";return;
}
$valz=explode("<!--delim1-->",$fi);
$c=0;$w=loadwords('',0);
foreach ($w as $naa=>$vaa){
if (!isset($valz[$c])){
echo "ERROR: Value offset ".$c." not foundn";
break;
}
$wds[$naa]=$valz[$c];$c++;
}
return json_encode($wds,true);
}
function test(){
buildbaseconv();
$fol=langfol();
//do language conversion
$sourcelang='eng';
$destlang='por';
//our language files are JSON encoded but
//the remote server cant handle JSON so we have to
//convert it to values with delimiters
$data=rawlangfromjson(file_get_contents($fol."/".$sourcelang.".htm"));
//Make the file acceptable for the remote translator
$f=fopen($fol."/Lfr.htm","w");
fwrite($f,$data);
fclose($f);
langexec($fol,$sourcelang,$destlang);
//Convert the result back to JSON
$data=rawlangtojson(file_get_contents($fol."/Lto.htm"));
//and save it as its own language file
$f=fopen($fol."/".$destlang.".htm","w");
fwrite($f,$data);
fclose($f);
}
function langexec($fol,$fr,$to){
//actual language converter
system("curl -sk --form 'file=@".$fol."/Lfr.htm' 'https://www.apertium.org/apy/translateDoc?markUnknown=no&langpair=".$fr."|".$to."' > ".$fol."/Lto.htm");
}
I tried changing the delimiter