admin管理员组

文章数量:1323355

I am trying to execute JavaScript using Winforms & I would like to fetch text from JavaScript code. I need to translate few lines using Google Translator service. I came across this nice Javascript code which translates given message & display it in the alert box:

<html>
<head>
<script type='text/javascript' src=''></script>
<script type='text/javascript'>
google.load('language','1');
function init () {
google.language.translate('How are you?', 'en', 'es', function (translated) {
    alert(translated.translation);
});
}
google.setOnLoadCallback(init);
</script>
</head>
    <body>
    </body>
</html> 

Is there any way so that I can pass any string instead of 'How are you?' & if I can fetch the translated text (from alert box or using any var) in the C# WinForms context.

I am trying to execute JavaScript using Winforms & I would like to fetch text from JavaScript code. I need to translate few lines using Google Translator service. I came across this nice Javascript code which translates given message & display it in the alert box:

<html>
<head>
<script type='text/javascript' src='http://www.google./jsapi'></script>
<script type='text/javascript'>
google.load('language','1');
function init () {
google.language.translate('How are you?', 'en', 'es', function (translated) {
    alert(translated.translation);
});
}
google.setOnLoadCallback(init);
</script>
</head>
    <body>
    </body>
</html> 

Is there any way so that I can pass any string instead of 'How are you?' & if I can fetch the translated text (from alert box or using any var) in the C# WinForms context.

Share Improve this question edited Jan 4, 2023 at 14:52 dakab 5,91510 gold badges46 silver badges70 bronze badges asked Nov 23, 2011 at 10:39 Sangram NandkhileSangram Nandkhile 18.2k19 gold badges84 silver badges116 bronze badges 6
  • 1 Please read the following URL, Google will remove the free translation API in December: URL – Niels Commented Nov 23, 2011 at 10:41
  • so you mean this code wont work anymore ? – Sangram Nandkhile Commented Nov 23, 2011 at 10:44
  • Yes exactly, the full translation API V1.0 will be offline and for the V2.0 API you need to pay. 20 dollar for 20M characters. See this url – Niels Commented Nov 23, 2011 at 10:48
  • 1 Bing offers a free api. msdn.microsoft./en-us/library/ff512387.aspx – Ash Burlaczenko Commented Nov 23, 2011 at 10:52
  • I updated my answer. You'll just have to replace 'myResults from callback' with your return variable with your response, and update the html string to include your request – Jeff Lauder Commented Nov 24, 2011 at 17:43
 |  Show 1 more ment

1 Answer 1

Reset to default 5

Ok I did a little research. So add a webbrowser to your form, then I bet this will work well for you:

    public Form1()
    {
        InitializeComponent();
        webBrowser1.ObjectForScripting = new MyScript();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string myTranslatedText = "Hello, how are you?";
        webBrowser1.DocumentText = @"
            <html>
            <head>
                <script type='text/javascript' src='http://www.google./jsapi'></script>
                <script type='text/javascript'>
                    google.load('language','1');
                    function init () {
                    google.language.translate('" + myTranslatedText + @"', 'en', 'es', function (translated) {
                        window.external.CallServerSideCode(translated.translation);
                    });
                    }
                    google.setOnLoadCallback(init);                        
                </script>
            </head>
                <body>
                </body>
            </html>";
    }
    [ComVisible(true)]
    public class MyScript
    {
        public void CallServerSideCode(string myResponse)
        {
            Console.WriteLine(myResponse); //do stuff with response
        }
    }

本文标签: Executing JavaScript code from C WinformsStack Overflow