从字符串中临时删除 Google Translate API 的 HTML 以降低成本 [英] Temporary removal of HTML from string for Google Translate API to reduce cost

查看:37
本文介绍了从字符串中临时删除 Google Translate API 的 HTML 以降低成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用我们付费的 Google API 翻译一些细节.详细信息包含 HTML,Google 会按每个字符收费.我不想发送完整的内容,而只想发送英文文本,并删除 HTML.我可以使用 PHP 函数删除 HTML 标签和实体,但我必须在翻译后将英文内容放回 HTML 标签中才能正确显示.它还将包括 CSS.

I have to translate some details using a Google API which we're paying for. The details contain HTML, and Google charges for each character. I don't want to send the complete content, but only the English text instead, with the HTML removed. I can remove HTML tags and entities using PHP functions, but I have to place the English content back in the HTML tags after translation for proper display. It will also include CSS.

示例:

<strong>This is a test</strong><br /> &nbsp; <custom tag>This is a test</custom tag><br />

翻译成西班牙语后,我需要:

After translation to Spanish I need:

<strong>Translated content </strong><br /> &nbsp; <p>Translated content </p><br />

如何在不向 API 发送 HTML 的情况下保留 HTML 格式?

How can I preserve the HTML format with out sending HTML to the API?

推荐答案

哈哈,我也遇到了这个问题.但它已经有一段时间了...

Haha, I also had that problem. But it has been while ago...

我认为,有一个问题是——由于翻译性质——一些句子部分被交换了.所以我首先无法将标签放在同一位置.但是我认为有一种方法可以从翻译过程中获取一些元数据,您是否可以看到句子的哪个部分移动到了新位置以及内容是什么......我知道,我终于解决了.但我不记得怎么了:(

I think, there was a problem were - due to translation-nature - some sentenceparts were swaped. So I was not able to fit the tags in at the same position, first. But I think there was a way to get some metadata from the translationprocess, were you can see which part of the sentence have moved to a new position and what the content was... I know, I solved it finally. But I cant recall how :(

如果每个单词在翻译后再次出现在同一个位置,您可以先用空格或 htmltag 将所有单词分隔成一个数组,并记住每个 HTML 标签的位置,然后在翻译后重新应用...

If every word takes the same place again after translation, you could first separate all words by whitespace OR htmltag into an array and remember where each HTML-tag was and reapply that after translation...

这篇关于从字符串中临时删除 Google Translate API 的 HTML 以降低成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆