以 html 标签作为选择器的字符串
我正在开发一个使用 I18N 的网站。
我有一个翻译团队在网站上检查翻译。
如果他们愿意,他们可以通过将鼠标放在要翻译的字符串上来提供新的翻译。
我需要检测 PO 文件中当前页面上使用的所有字符串,并在字符串的直接父级上添加一个类,以便我可以添加一个文本输入来提交新翻译。
因此,我创建了一个函数来存储当前页面中使用的每个翻译(PO 文件中的基本字符串和 PHP 注入的数据的最终翻译),并将它们作为 JSON 对象传递给 JavaScript。
现在,我必须在页面中找到所有这些翻译。
我的问题是在它们包含 html 标签时匹配它们。
在我的 PO 文件中:
msgid "Bonjour <strong>%s</strong>"
msgstr "Hi <strong>%s</strong>"
我的页面来源: >
<div>Hi <strong>Tristan</strong></div>
这里我需要
- 匹配
Hi Tristan
- 向直接父级添加一个类(这里是“div”节点)
- 然后我将能够附加我的文本输入以提交新翻译
我怎样才能匹配呢?我想我需要使用翻译后的字符串作为选择器。
我想避免在 PHP 处理过程中用元素包装 i18n 字符串(因为它会以某种方式改变 DOM,从而对网站的设计产生影响),但另一方面,使用来自PO 文件在浏览器端似乎也相当重。
有什么提示吗?
I'm working on a website that uses I18N.
I have a team of translators who check the translations on the website.
If they want, they can provide a new translation by putting their mouse right over the string they want to translate.
I need to detect all the strings inside my PO file that are used on the current page and add a class on the direct parent of the string so I could add a text input for submitting a new translation.
So I made a function that stores each translation used in the current page (both the base string in the PO file and the final translation with data injected by PHP) and pass them as a JSON object to the JavaScript.
Now, I have to find all of these translations in the page.
My problem is to match them when they contain an html tag.
In my PO file :
msgid "Bonjour <strong>%s</strong>"
msgstr "Hi <strong>%s</strong>"
Source of my page :
<div>Hi <strong>Tristan</strong></div>
Here I need
- to match
Hi <strong>Tristan</strong>
- add a class to the direct parent (here the "div" node)
- Then I'll be able to attach my text input to submit a new translation
How can I match that? I think I need use the translated string as selector.
I would like to avoid wrapping the i18n strings during PHP treatment with an element (Because it would somehow change the DOM and therefore have an impact on the design of the website), but on the other hand, parsing the page with the strings from the PO file seems also quite heavy on the browser-side.
Any hints ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在@lonesomeday之后,我也会使用跨度。这就是他们的目的。
这就是跨度应该在的位置:
Bonjour Tristan
<
javascript 则变为:
following @lonesomeday, I would use a span as well. that is what they're for.
this is where the span should be:
<span class='I18N'>Bonjour <strong>Tristan</strong></span>
<span class='I18N'>Hi <strong>Tristan</strong></span>
javascript then becomes: