0

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 Array\n";
        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 found\n";
            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

2
  • 4
    I don’t know where to begin really. At a quick glance, using echo for errors is asking for problems, I’d just throw an exception probably. Similarly, just using return; isn’t helpful either. Also, are you aware of existing file formats for translations such as PO/MO and XLIFF, as well systems such as gettext? Commented Jun 18, 2024 at 4:01
  • "I don’t know where to begin really." That generally indicates that a question is "Needs More Focus". "Is my language converter that bad" -- Opinion-based. Commented Jul 6, 2024 at 14:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.