简体   繁体   English

暂时从字符串中删除HTML以用于Google Translate API,以降低成本

[英]Temporary removal of HTML from string for Google Translate API to reduce cost

I have to translate some details using a Google API which we're paying for. 我必须使用我们要付费的Google API来翻译一些细节。 The details contain HTML, and Google charges for each character. 详细信息包含HTML,Google对每个字符收费。 I don't want to send the complete content, but only the English text instead, with the HTML removed. 我不想发送完整的内容,而只发送英文文本,而删除了HTML。 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. 我可以使用PHP函数删除HTML标记和实体,但是我必须在翻译后将英语内容重新放置在HTML标记中才能正确显示。 It will also include CSS. 它还将包括CSS。

Example: 例:

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

How can I preserve the HTML format with out sending HTML to the API? 如何在不发送HTML到API的情况下保留HTML格式?

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 :( 但我不记得如何:(

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... 如果翻译后每个单词都再次占据相同的位置,则可以先将所有单词用空格或htmltag分隔成一个数组,并记住每个HTML标签的位置,然后在翻译后重新应用。

Add google translate service to your website and add notes witch words not to translate. 将google翻译服务添加到您的网站,并添加注释注释不翻译的单词。

https://translate.google.com/manager/ https://translate.google.com/manager/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM