Need help to edit Razor script to access Bing Translate API properly
Hi there
This is my first post on this forum and I am very interested in expanding my Razor script knowledge and wondering if any help/advice is available on this forum.
For example my Bing translate Razor code below works perfectly, but I would
like the script to instantly translate from English into all 3 languages at once when I click the Translate Button, ie.not
one-by-one by clicking the drop-down menu language option.
Instead of Request["languageChoice"], why not just make an array of languages and foreach through them, while calling the translate code for eacht one. You'll have to move the translate code into a seperate @functions { } block, but that shouldn't be a problem.
I think I've given you a bit to go on. If you know C#, then you already know how to solve this.. Remember: Razor is just HTML with a bit of C# in it. So my advise would be to figure it out in C# and then put that in Razor, here's something to get your started:
@{string tobetranslated = Request["tobeTranslated"];
var languages = new []{"es", "fr", "it"};
foreach (var language in languages)
{
<span>The translated text is: @GetTranslation(tobetranslated, language)</span>
}
}@functions {publicstaticstring GetTranslation(string tobetranslated, string toLang)
{
var translatedText = string.Empty;
//put what you have in your "else" statement in here, except for the Response.Writereturn translatedText;
}
}
Need help to edit Razor script to access Bing Translate API properly
Hi there
This is my first post on this forum and I am very interested in expanding my Razor script knowledge and wondering if any help/advice is available on this forum.
@{
string appId = "My BING API GOES IN HERE";
string fromLang = "en";
string translatedText = "";
if (IsPost) {
string tobetranslated = Request["tobeTranslated"];
string toLang = Request["languageChoice"];
if (tobetranslated == "") {
<text>
<p style="color:red;">ENTER TEXT TO BE TRANSLATED HERE !</p>
</text>
}
else
{
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId="
+ appId + "&text=" + tobetranslated + "&from=" + fromLang + "&to=" + toLang;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
WebResponse response = request.GetResponse();
Stream strm = response.GetResponseStream();
StreamReader reader = new System.IO.StreamReader(strm);
translatedText = reader.ReadToEnd();
Response.Write("The translated text is: '" + translatedText + "'.");
response.Close();
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h1>Using Windows Live Translator's HTTP API</h1>
<form method="post" action="">
<div>
<label for="tobeTranslated">Type the text you want translated:</label>
<br />
<textarea name="tobeTranslated" rows="5" cols="20" id="inputText" />
</textarea>
</div>
I want to translate to:
<select name="languageChoice">
<option value="es" >Spanish</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select>
<input type="submit" value="Translate Now!" />
</form>
</body>
</html>
Instead of Request["languageChoice"], why not just make an array of languages and foreach through them, while calling the translate code for eacht one. You'll have to move the translate code into a seperate @functions { } block, but that shouldn't be a problem.
Thanks for that Sebastiaan.
You make it sound easy !
I only started learning Razor script yesterday, and I don't yet know how to do as you kindly advised.
I was hoping that someone on this forum would show me how it's done, so that I could learn from their coding and emulate them to improve my knowledge.
David
I think I've given you a bit to go on. If you know C#, then you already know how to solve this.. Remember: Razor is just HTML with a bit of C# in it. So my advise would be to figure it out in C# and then put that in Razor, here's something to get your started:
By the way, I have a Razor Examples site available, for some more inspiration.
Thanks again Sebastiaan.
I am out for the rest of the day, so I will have a good look at your advice above when I get back home tonight.
David.
is working on a reply...