如何翻译网站中的特定内容
How to translate specific content in website
我想知道是否有办法使用 google 翻译或任何其他 app/widget/way 以编程方式将翻译嵌入网站以帮助翻译特定内容(不是一般的所有网站翻译) :
示例:
我有一个带有英文值的表单输入元素,只想将该值翻译成法语,并将其插入特定的 html 元素,然后在表单中发送该信息。
<!DOCTYPE html>
<html>
<body>
<input type="text" id="origin" value="Some text in English"/>
<button type="button" onclick="translate("origin", "destination")">Click Me to Translate!</button>
<input type="text" id="destination" value="Translated content"/>
<input type="submit"/>
</body>
</html>
objective 将使用类似于 www.translate.google.com(或其本身)的东西...是否有任何 app/widget/way 可以在不翻译所有页面的情况下做到这一点?
谢谢
这只是对我有用的一种(第一种)方式...它可能可以进行优化。
HTML 到 copy/past:
<input type="text" id="txtMsgOrigin" value="" />
<input type="text" id="txtMsgDestiny" value="" />
<button id="btnTranslate" onclick="translateSourceTarget();">Translate</button>
Javascript 文件:
(您可以分离函数以在 Document.ready 上获取令牌,因此您可以减少翻译的等待时间,因为您在 g_token 中已经有一个 access_token)
var g_token = '';
function getToken() {
var requestStr = "getTranslatorToken";
$.ajax({
url: requestStr,
type: "GET",
cache: true,
dataType: 'json',
success: function (data) {
g_token = data.access_token;
var src = $("#txtMsgOrigin").val();
translate(src, "en", "pt");
}
});
}
function translate(text, from, to) {
var p = new Object;
p.text = text;
p.from = from;
p.to = to;
p.oncomplete = 'ajaxTranslateCallback'; // <-- a major puzzle solved. Who would have guessed you register the jsonp callback as oncomplete?
p.appId = "Bearer " + g_token; // <-- another major puzzle. Instead of using the header, we stuff the token into the deprecated appId.
var requestStr = "//api.microsofttranslator.com/V2/Ajax.svc/Translate";
window.ajaxTranslateCallback = function (response) {
// Display translated text in the right textarea.
//alert(response);
$("#txtMsgDestiny").val(response);
}
$.ajax({
url: requestStr,
type: "GET",
data: p,
dataType: 'jsonp',
cache: true
});
}
function translateSourceTarget() {
// Translate the text typed by the user into the left textarea.
getToken()
}
C# 控制器:
public async Task<JToken> getTranslatorToken()
{
string clientID = ConfigurationManager.AppSettings["ClientID"].ToString();
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"].ToString();
Uri translatorAccessURI = new Uri("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
// Create form parameters that we will send to data market.
Dictionary<string, string> requestDetails = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", clientID},
{ "client_secret", clientSecret },
{ "scope", "http://api.microsofttranslator.com" }
};
FormUrlEncodedContent requestContent = new FormUrlEncodedContent(requestDetails);
// We use a HttpClient instance for Azure Marketplace request
HttpClient client = new HttpClient();
//send to data market
HttpResponseMessage dataMarketResponse = await client.PostAsync(translatorAccessURI, requestContent);
// If client authentication failed then we get a JSON response from Azure Market Place
if (!dataMarketResponse.IsSuccessStatusCode)
{
//JToken error = await dataMarketResponse.Content.ReadAsAsync<JToken>();
JToken error = await dataMarketResponse.Content.ReadAsStringAsync();
string errorType = error.Value<string>("error");
string errorDescription = error.Value<string>("error_description");
throw new HttpRequestException(string.Format("Azure market place request failed: {0} {1}", errorType, errorDescription));
}
// Get the access token to attach to the original request from the response body
JToken response = JToken.Parse(await dataMarketResponse.Content.ReadAsStringAsync());
return response;
}
是时候让我的头发重新长出来了! :-P
我想知道是否有办法使用 google 翻译或任何其他 app/widget/way 以编程方式将翻译嵌入网站以帮助翻译特定内容(不是一般的所有网站翻译) :
示例:
我有一个带有英文值的表单输入元素,只想将该值翻译成法语,并将其插入特定的 html 元素,然后在表单中发送该信息。
<!DOCTYPE html>
<html>
<body>
<input type="text" id="origin" value="Some text in English"/>
<button type="button" onclick="translate("origin", "destination")">Click Me to Translate!</button>
<input type="text" id="destination" value="Translated content"/>
<input type="submit"/>
</body>
</html>
objective 将使用类似于 www.translate.google.com(或其本身)的东西...是否有任何 app/widget/way 可以在不翻译所有页面的情况下做到这一点?
谢谢
这只是对我有用的一种(第一种)方式...它可能可以进行优化。
HTML 到 copy/past:
<input type="text" id="txtMsgOrigin" value="" />
<input type="text" id="txtMsgDestiny" value="" />
<button id="btnTranslate" onclick="translateSourceTarget();">Translate</button>
Javascript 文件: (您可以分离函数以在 Document.ready 上获取令牌,因此您可以减少翻译的等待时间,因为您在 g_token 中已经有一个 access_token)
var g_token = '';
function getToken() {
var requestStr = "getTranslatorToken";
$.ajax({
url: requestStr,
type: "GET",
cache: true,
dataType: 'json',
success: function (data) {
g_token = data.access_token;
var src = $("#txtMsgOrigin").val();
translate(src, "en", "pt");
}
});
}
function translate(text, from, to) {
var p = new Object;
p.text = text;
p.from = from;
p.to = to;
p.oncomplete = 'ajaxTranslateCallback'; // <-- a major puzzle solved. Who would have guessed you register the jsonp callback as oncomplete?
p.appId = "Bearer " + g_token; // <-- another major puzzle. Instead of using the header, we stuff the token into the deprecated appId.
var requestStr = "//api.microsofttranslator.com/V2/Ajax.svc/Translate";
window.ajaxTranslateCallback = function (response) {
// Display translated text in the right textarea.
//alert(response);
$("#txtMsgDestiny").val(response);
}
$.ajax({
url: requestStr,
type: "GET",
data: p,
dataType: 'jsonp',
cache: true
});
}
function translateSourceTarget() {
// Translate the text typed by the user into the left textarea.
getToken()
}
C# 控制器:
public async Task<JToken> getTranslatorToken()
{
string clientID = ConfigurationManager.AppSettings["ClientID"].ToString();
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"].ToString();
Uri translatorAccessURI = new Uri("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
// Create form parameters that we will send to data market.
Dictionary<string, string> requestDetails = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", clientID},
{ "client_secret", clientSecret },
{ "scope", "http://api.microsofttranslator.com" }
};
FormUrlEncodedContent requestContent = new FormUrlEncodedContent(requestDetails);
// We use a HttpClient instance for Azure Marketplace request
HttpClient client = new HttpClient();
//send to data market
HttpResponseMessage dataMarketResponse = await client.PostAsync(translatorAccessURI, requestContent);
// If client authentication failed then we get a JSON response from Azure Market Place
if (!dataMarketResponse.IsSuccessStatusCode)
{
//JToken error = await dataMarketResponse.Content.ReadAsAsync<JToken>();
JToken error = await dataMarketResponse.Content.ReadAsStringAsync();
string errorType = error.Value<string>("error");
string errorDescription = error.Value<string>("error_description");
throw new HttpRequestException(string.Format("Azure market place request failed: {0} {1}", errorType, errorDescription));
}
// Get the access token to attach to the original request from the response body
JToken response = JToken.Parse(await dataMarketResponse.Content.ReadAsStringAsync());
return response;
}
是时候让我的头发重新长出来了! :-P