Search code examples
phpencodingmachine-translation

Encoding error in PHP with Google Translate


I have some text which I need to translate into Japanese.

Translate text, webpage, or document

Google Translate returns the string as:

翻訳テキスト、 Webページ、またはドキュメント

When I am writing that text to a file (ja.po), it looks like:

–|–óƒeƒLƒXƒgA Webƒy[ƒWA‚Ü‚½‚̓hƒLƒ…ƒƒ“ƒg

What would be the error?

I am using poeditor.exe to view the file. I am not using any encoding function at present.

for(/*extracting from the trnslated string array*/)
{
    $pattern = "/msgid \"".preg_quote($id, '/')."\"(\r?\n)msgstr \"\"/";
    $string = str_replace('"', '\"', $string);
    $replacement = "msgid \"$id\"\nmsgstr \"". $string . "\"";
    $res = preg_replace($pattern, $replacement, $con);
    $con = $res;
}
file_put_contents("ja.po", $con);

Solution

  • How are you getting the string from Google and how are you writing it? Somewhere along the way it gets converted from UTF8 to latin1 I suppose. If you can figure out where it happens, use mb_convert_encoding to fix it.

    BTW, the translated Japanese is pretty bad. ;)