简体   繁体   English

在点击时显示任何单词的翻译

[英]Show translation of any word on click

I have a html page with a number of paragraphs on it. 我有一个HTML页面,上面有很多段落。 I want to show the translation of any word to another language via a popup when the user clicks on it. 我想在用户单击时通过弹出窗口显示任何单词到另一种语言的翻译。 It could be done like this but it seems mad: 可以这样做,但似乎很生气:

<p><span onclick="translate(this)">Once</span> <span onclick="translate(this)">upon</span> <span onclick="translate(this)">a</span> <span onclick="translate(this)">time</span>

Any better ways of doing this? 有更好的方法吗?

Additionally, could your solution be adopted to show different translations for the same word spelling, for example: 此外,是否可以采用您的解决方案来显示同一单词拼写的不同翻译,例如:

I left the building and turned left at the end of the street 我离开了建筑物,在街的尽头向左转

Here it would be nice to uniquely identify the two different lefts so that the correct translation could be offered depending on which one is clicked. 在这里最好唯一地标识两个不同的左键,以便根据单击哪个来提供正确的翻译。

Also: 也:

Winning the tournament was a piece of cake. 赢得比赛是小菜一碟。

Here it would be nice to handle "piece of cake" as a single item to translate since it is an idiom. 在这里,最好将“小菜一碟”作为一个单项进行翻译,因为这是一个成语。

At the moment I'm just using javascript and html5 but I expect that I will be using jquery-mobile. 目前,我只使用javascript和html5,但我希望我会使用jquery-mobile。 I am also open to react/redux solutions. 我也愿意对React / Redux解决方案进行响应。

I am unsure of your stack, so I'll provide a solution with JQuery. 我不确定您的堆栈,因此我将提供JQuery解决方案。

The library jquery-i18n seems to do the trick just fine, albeit a tad outdated and requiring the developer to populate translations on their own. jquery-i18n似乎可以解决问题,尽管有点过时,并且要求开发人员自行填充翻译。

Over on their GitHub repo , the usage is described: 在其GitHub存储库上 ,其用法描述如下:

Before you can do any translation you have to initialise the plugin with a 'dictionary' (basically a property list mapping keys to their translations). 在进行任何翻译之前,您必须使用“字典”(基本上是将键映射到其翻译的属性列表)初始化插件。

with the example code: 带有示例代码:

 const my_dictionary = { "some text" : "a translation", "some more text" : "another translation" } $.i18n.load(my_dictionary); $('div#example').text($.i18n._('some text')); 

If you were using React, a much more favorable solution is to create some sort of component which stores the state of the text, and can act upon an onClick event accordingly. 如果您使用的是React,一个更有利的解决方案是创建某种组件来存储文本状态,并可以相应地对onClick事件进行操作。 In fact, this react-translate-component was just a duck-duck-go away! 实际上, 这个react-translate-component简直就是鸭蛋!

Edit: I would have commented, probing for the stack in question in order to further tailor my response, yet I lack 50 reputation. 编辑:我会发表评论,对有问题的堆栈进行探测,以进一步调整我的回答,但我却没有50个声誉。

The first question is easy to do in a way like this: 第一个问题很容易以这样的方式进行:

 document.getElementById('target') .addEventListener('click', getClickedWord); function getClickedWord(e){ translate(e.target.innerHTML); } function translate(word){ console.log(word); // do rest } 
 <p id="target"> <span>Hello</span> <span>Stack</span> <span>OverFlow</span> </p> 

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

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