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

发布于 2024-12-19 04:32:31 字数 566 浏览 2 评论 0原文

我需要知道如何使用 Google 翻译语言 API 使用 PHP 将 XML 文件的内容从英语翻译成另一种语言,XML 文件中的数据如下所示:

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

所以我需要将文件传递给 Google API 并在标签之间返回新数据用其他特定语言(例如阿拉伯语):

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

我希望能清楚地表达出来..

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-26 04:32:31

您可以使用 GTranslate 这是 Google Translate API 的包装器,可以简化您的工作。这是一个示例代码:

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);

请注意,Google 限制每个 IP 每天的 API 请求,因此对于具有许多标签的大型 XML 来说,这可能不起作用,或者您可以将整个 XML(带标签)传递给 Google Translate(有 100000字符数也有限制,但这对于很多事情来说已经足够了),然后从翻译结果中提取翻译后的内部文本。

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.

怪我鬧 2024-12-26 04:32:31

mkTranslation 可以将 .txt 文件、.string 文件和 .xml 文件翻译成多种语言。

安装:$pip install mkTranslation

使用方法:

$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>

默认翻译为google翻译,可以指定其他翻译渠道

代码:https://github.com/mythkiven/mkTranslate

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文