需要使用谷歌翻译API翻译xml文件内容

I need to know how to use Google translate languages API to translate the content of XML file from English to another language using PHP , Data in XML file shown as following:

<log>
    <titles>
        <logfile>Log File</logfile>
        <clearlog>Clear Log File</clearlog>
    </titles>
</log>  

So i need to pass the file to Google API and return new data between tags in other specific language like in Arabic:

<log>
    <titles>
        <logfile>ملف اللوج</logfile>
        <clearlog>مسح محتويات الملف</clearlog>
    </titles>
</log>  

I hope that will be clear..

Thanks

You can use GTranslate which is a wrapper for Google Translate API to ease your work. Here is a sample code:

require_once ("include/GTranslate.php");

// original input xml
$orig_xml = "YOUR XML HERE";

// translate
$gt = new Gtranslate; // google translate object
$orig_text = array(); // original text
$trans_text = array(); // translated text

// extract text between tags
$split_tags_pattern = '|<[^>]+>([^<>]+)<\/[^>]+>|';
$tags = array();

preg_match_all($split_tags_pattern, $orig_xml, $tags, PREG_SET_ORDER);
/*
* preg_match_all returns:
*
* array() {
*   array () {
*       <xml tag>text</xml>,
*       text
*   },
*   ...
* }
*/

// translate each tag's inner text
foreach ($tags as $tag) {
    $text = trim($tag[1]);
    if ($text != '') {
        $orig_text[] = trim($tag[0]);
        $trans_text[] = str_replace($text, $gt->en_to_ar($text), $tag[0]); // en_to_ar method is used to translate from english to arabic.
    }
}

// replace original tag's inner text with translated ones
$trans_xml = str_replace($orig_text, $trans_text, $orig_xml);

note that Google limits API requests per day for each IP, so for a large XML with many tags this may not work, alternatively you can pass the whole XML (with tags) to Google Translate (there is a 100000 characters limit in this too, but that would be enough for many things) and then extract translated inner texts from the translation result.

mkTranslation can translates .txt file , .string files and .xml files into multiple languages.

Install: $pip install mkTranslation

Usage:

$translate -p  ./ios.strings  -d 'en'  # translation file
$translate -p ./android.xml -d 'ja'  # translation file
$translate  'mkTranslate supports multilingual translations' -d 'pt'   # translation text

demo:

from  
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- tab -->
    <string name="network_error">网络不可用,点击屏幕重试</string>
    <string name="scan_qr_code_warn">将二维码放入框内,即可自动扫描</string>
    <string name="album_text">相册</string>
</resources>
to  
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- tab -->
    <string name="network_error">Network is not available, click screen to try again</string>
    <string name="scan_qr_code_warn">Put the QR code into the box and you can scan it automatically.</string>
    <string name="album_text">Album</string>
</resources>

The default translation is google translation, you can specify other translation channels

Code:https://github.com/mythkiven/mkTranslate