PHP 使用谷歌翻译翻译 HTML 页面
下午,
这个问题有点棘手,我有一个网站,需要将页面翻译成法语,我正在使用谷歌翻译 API 进行翻译。我正在做的是使用 PHP 将 HTML 模板加载到字符串中,然后需要翻译页面上的文本副本,然后保存为例如:
fr_master_header.html
de_master_header.html
我遇到的问题是识别页面上的文本副本,忽略 HTML,将副本替换在确切的位置,然后使用 HTML 标签和翻译后的文本副本保存文件。
提前干杯。
Afternoon,
Bit of a tricky question this one, I have a website and need to translate the pages into french, I am using google translate API to do the translation. What I am doing is loading in the HTML templates into a string using PHP and then need to translate the text copy on the pages and then save as for example:
fr_master_header.html
de_master_header.html
The problem I have is identifying the text copy on the page ignoring the HTML, replacing the copy in the extact place and then saving the file with the HTML tags and trasnlated text copy.
Cheers in advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在编写应用程序之前考虑翻译您的应用程序将节省大量时间/金钱。您也许可以返回并修复 HTML,但这将是严重的受虐行为。如果您在添加语言支持后,使用 Goolge Ajax Language API 效果会更好已经构建了该应用程序。
我通过对每一段文本使用变量来解决这个问题。然后我检查用户想要什么语言,并包含相应的语言文件。我的模板系统负责放置这些变量的位置。这些语言文件看起来像这样:
无论语言如何,变量名称始终相同。这是非常可扩展的,我可以支持谷歌翻译的尽可能多的语言,而无需进行任何修改。
Thinking about translating your application before you write it is going to save a massive amount of time/money. You might be able to go back and fix the HTML, but this is going to be seriously masochistic. You are far better off with the Goolge Ajax Language API if you are adding language support after you've built the app.
I took care of this problem by using variables for every piece of text. Then I check what the language the user wants and I include the corresponding language file. My template system takes care of where to place these variables. These language files look something like this:
The variable name is always going to be the same regardless of the language. This is very scalable, I can support as many languages as Google will translate without making any modifications.