【问题标题】:Undo translation made with Jquery Translate (Google Translate API v2)撤消使用 Jquery Translate 进行的翻译(Google Translate API v2)
【发布时间】:2013-06-11 07:50:16
【问题描述】:

我一直在使用 this jQuery plugin 来帮助我与 Google Translate API v2 进行交互,到目前为止一切都运行良好。

但是我想给用户一个选项来撤消翻译,但我看不到任何明显的方法可以将语言返回到它的源(我不想're -将字符串翻译回原始语言)。

【问题讨论】:

  • 自己记住原文并用它代替翻译?
  • 看起来您需要实现自己的逻辑来取消翻译元素。您可以在任何翻译之前使用 data() 保存原始字符串,然后在需要时重新加载它。

标签: jquery jquery-plugins google-translate


【解决方案1】:

在翻译之前存储翻译的内容可能是最好的解决方案。既然您仍然使用 jquery,请查看 .html() --> http://api.jquery.com/html/

翻译示例:

var content = $('.element').html();
$('.element').translate();

我认为这是可能的,但您需要对此进行测试:

var content = $('.element').html();
var translated_content = $(content).translate();
$('.element').html(translated_content);

现在回到原来的:

$('.element').html(content);

因此,围绕此包装一些函数和一些事件侦听器并完成工作!


编辑:替代方法

在重新考虑了我上面的原始方法之后,认为隐藏原件并添加翻译副本会更容易。

注意: 使用 .clone() 具有产生具有重复 id 属性的元素的副作用,这些属性应该是唯一的。在可能的情况下,建议避免使用此属性克隆元素或使用类属性作为标识符。

功能:

function translate(object){
   var clone = $(object).clone().addClass("trans_translation"); //copies the object
   $(object).addClass("trans_original"); //adds a identifier to the original
   $(object).after(clone); //inserts the clone to the html
   $(object).next(".trans_translation").translate(); //translate the new div
   $(object).hide(); //hides to original
   $(object).next(".trans_translation").show(); //shows the translation
}

function toggle_translation(bool){
  bool = bool || false; //default parameter
  if(bool){ //if true, show translation, hide original
    $(".trans_translation").show();
    $(".trans_original").hide();
  } else { //if false (default), hide translation, show original
    $(".trans_translation").hide();
    $(".trans_original").show();
  }
}

文件准备好了:

$(document).ready(function(){
  $(".trans_translation").css( "display", "none" ); //hides the translations on default

  //example button to translate a div
  $(".translateme").click(function() {
    var object = $(this).parent(); //selects the parent of the button
    translate(object);
  });

  //example button to hide translated and show original on page
  $(".givemetheoriginal").click(function() {
    toggle_translation();
  });

});

HTML 正文:

<div class=".content">
  <p>This is just a text</p>
  <div class=".translateme">translate this div</div>
</div>
<div class=".content">
  <p>This is just a text</p>
  <div class=".translateme">translate this div</div>
</div>
<div class=".content">
  <p>This is just a text</p>
  <div class=".translateme">translate this div</div>
</div>
<div class=".givemetheoriginal">Show the original!</div>

尚未对此进行测试,但假设应该可以。

注意:糟糕,您应该先重命名翻译函数,因为翻译插件具有相同的名称。

【讨论】:

  • 你知道,当你看东西太久,然后因为没有考虑逻辑方法而觉得自己像个白痴..这就是我现在的感觉。谢谢!我已经转移到另一个项目,但明天会看看这个。非常感谢您的快速答复!
  • 看起来 jquery 插件不喜欢在变量上运行。我最终做了这个(到目前为止),感觉很啰嗦(但我不是 js 编码器) - pastebin.com/UawSvx2J
  • sessionStorage.setItem 已经派上用场了 :)
  • 在重新考虑了我建议的方法后,我认为隐藏原始内容而不是替换它可能是个好主意。将编辑我的答案并添加示例。
  • 这看起来是个不错的方法 - 我将在今天下午晚些时候进行测试。谢谢!
猜你喜欢
  • 2014-04-21
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多