Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Kirby 3 posts 23 karma points
    Jan 05, 2012 @ 22:05
    David Kirby
    0

    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.
    Any help will be most appreciated.

     

    @{

        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>

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Jan 06, 2012 @ 08:34
    Sebastiaan Janssen
    0

    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.

  • David Kirby 3 posts 23 karma points
    Jan 06, 2012 @ 10:07
    David Kirby
    0

    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

     

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Jan 06, 2012 @ 10:17
    Sebastiaan Janssen
    0

    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 {
        public static string GetTranslation(string tobetranslated, string toLang)
        {
            var translatedText = string.Empty;
            
            //put what you have in your "else" statement in here, except for the Response.Write
    
            return translatedText;
        }
    }
  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Jan 06, 2012 @ 10:18
    Sebastiaan Janssen
    0

    By the way, I have a Razor Examples site available, for some more inspiration.

  • David Kirby 3 posts 23 karma points
    Jan 06, 2012 @ 11:19
    David Kirby
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft