Skip to content

Instantly share code, notes, and snippets.

@LiuGuoGY
Created April 8, 2018 10:19
Show Gist options
  • Save LiuGuoGY/b9eea726f2a25b63672cc4fc48275b27 to your computer and use it in GitHub Desktop.
Save LiuGuoGY/b9eea726f2a25b63672cc4fc48275b27 to your computer and use it in GitHub Desktop.
查看APP store 英文详情内容的中文翻译
var text = $context.data.string
var index = text.indexOf("we-clamp we-clamp--lines-3 ember-view") + 45
var endIndex = text.indexOf("</span>", index)
var newText = text.substring(index, endIndex)
var briefTmp = newText.replace(/\<br\ \/\>/g, "\n")//
var brief = $text.HTMLUnescape(briefTmp)
var index = text.indexOf("we-clamp we-clamp--lines-5 ember-view") + 45
var endIndex = text.indexOf("</span>", index)
var newText = text.substring(index, endIndex)
var updateBriefTmp = newText.replace(/\<br\ \/\>/g, "\n")
var updateBrief = $text.HTMLUnescape(updateBriefTmp)
var combineText = "更新内容:\n" + updateBrief + "\n\n\n\n\n简介:\n" + brief
googleTran(combineText)
function show(sentences) {
var text = ""
var length = sentences.length
for(var i = 0; i < length; i++){
var trans = sentences[i].trans
text += trans
}
$ui.render({
views: [{
type: "text",
props: {
text: text
},
layout: $layout.fill
}]
})
}
//谷歌翻译
function googleTran(text) {
$ui.loading("翻译中")
$http.request({
method: "POST",
url: "http://translate.google.cn/translate_a/single",
header: {
"User-Agent": "iOSTranslate",
"Content-Type": "application/x-www-form-urlencoded"
},
body: {
"dt": "t",
"q": text,
"tl": "zh-CN",
"ie": "UTF-8",
"sl": "auto",
"client": "ia",
"dj": "1"
},
handler: function(resp) {
$ui.loading(false)
show(resp.data.sentences)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment