我正在为我们的网络应用开发多语言支持.我们在 gettext 库.一切都出乎意料地简单,除了如何处理包含重要 HTML 标记的句子的问题.这是一个简单的例子:
I'm developing multi-language support for our web app. We're using Django's helpers around the gettext library. Everything has been surprisingly easy, except for the question of how to handle sentences that include significant HTML markup. Here's a simple example:
Please <a href="/login/">log in</a> to continue.
以下是我能想到的方法:
Here are the approaches I can think of:
更改链接以包含整个句子.不管在这种情况下改变是否是一个好主意,这个解决方案的问题在于,当两者在理想情况下独立时,UI 变得依赖于 i18n 的需求.
Change the link to include the whole sentence. Regardless of whether the change is a good idea in this case, the problem with this solution is that UI becomes dependent on the needs of i18n when the two are ideally independent.
将上面的整个字符串标记为翻译(包括格式).然后,翻译字符串也将直接包含 HTML.这样做的问题是更改 HTML 格式需要更改所有翻译.
Mark the whole string above for translation (formatting included). The translation strings would then also include the HTML directly. The problem with this is that changing the HTML formatting requires changing all the translation.
将多个翻译紧密耦合,然后使用字符串插值将它们组合起来.例如,短语请 %s 继续"和登录"可以分别标记为翻译,然后合并.登录"被本地化,然后包装在 HREF 中,然后插入到翻译的短语中,这使 %s 保持在翻译中以标记链接应该去哪里.这种方法使代码复杂化并破坏了翻译字符串的独立性.
Tightly couple multiple translations, then use string interpolation to combine them. For the example, the phrase "Please %s to continue" and "log in" could be marked separately for translation, then combined. The "log in" is localized, then wrapped in the HREF, then inserted into the translated phrase, which keeps the %s in translation to mark where the link should go. This approach complicates the code and breaks the independence of translation strings.
还有其他选择吗?其他人是如何解决这个问题的?
Are there any other options? How have others solved this problem?
解决方案 2 就是您想要的.将整个句子发送给他们,并嵌入 HTML 标记.
Solution 2 is what you want. Send them the whole sentence, with the HTML markup embedded.
原因:
这篇关于您如何处理带有标记的文本翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!