Google API - 将字符串翻译为EN语言:(

时间:2011-08-12 07:36:34

标签: javascript html google-translate

我正在尝试创建一个简单的基于Web的应用程序,它将非英语(非EN)字符串翻译成英语(EN)语言。为此,我使用谷歌的翻译API(v1-JS)。

首先,我检测div标签中提供的语言(注意页面编码设置为UTF-8)。

然后我试图翻译文本,如果它是非EN语言,然后它将显示在检测到的语言标记下面。

我能够获得检测到的语言,但翻译永远不会发生! :(

非常感谢任何帮助..

以下是代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>

<script
    src="http://www.google.com/jsapi?key=mykeyfrxwexdfwezfdhfxcewx" type="text/javascript"></script>

<script type="text/javascript">
    /*
     *  How to detect the language of text.
     */


    google.load("language", "1");

    function initialize() {
        var content = document.getElementById('content');
        // Setting the text in the div.
        content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="detected"><\/div><div id="transtext"/>';

        // Grabbing the text to translate
        var text = document.getElementById("text").innerHTML;

        // Detect the language of the text.
        google.language.detect(text, function(result) {
            var detected = document.getElementById("detected");
            // If there wasn't an error in the request
            if (!result.error) {
                var langCode = result.language;
                var langName;

                // Loop through the languages enum so that we can find the actual name of the language.
                // Learn about the languages enum here:
                // http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
                for ( var i in google.language.Languages) {
                    var thisLangCode = google.language.Languages[i];
                    if (thisLangCode == langCode) {
                        // find the language code, store the language name.
                        langName = i;
                        break;
                    }
                }

                // See the detected language.
                detected.innerHTML = 'Detected: "' + result.language
                        + '" - aka "' + langName + '"';
            }
        });

        google.language.translate(text, 'es', 'en', function(result) {
            var translated = document.getElementById("transtext");
            if (result.transtext) {
                translated.innerHTML = result.transtext;
            }
        });

    }
    google.setOnLoadCallback(initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
    <div id="content">Loading...</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

要点3分。

你实际上并没有在detect的回调中调用translate。 翻译结果不包含属性transtext。你需要的是命名翻译。 我怀疑你想在公共领域发布你的API密钥

查找以下修改后的代码

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>

<script
    src="http://www.google.com/jsapi?key=xxxxxxxxxxxxxxx>" type="text/javascript"></script>

<script type="text/javascript">
    /*
     *  How to detect the language of text.
     */


    google.load("language", "1");

    function initialize() {
        var content = document.getElementById('content');
        // Setting the text in the div.
        content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="detected"><\/div><div id="transtext"/>';

        // Grabbing the text to translate
        var text = document.getElementById("text").innerHTML;

        // Detect the language of the text.
        google.language.detect(text, function(result) {
            var detected = document.getElementById("detected");
            // If there wasn't an error in the request
            if (!result.error) {
                var langCode = result.language;
                var langName;

                // Loop through the languages enum so that we can find the actual name of the language.
                // Learn about the languages enum here:
                // http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
                for ( var i in google.language.Languages) {
                    var thisLangCode = google.language.Languages[i];
                    if (thisLangCode == langCode) {
                        // find the language code, store the language name.
                        langName = i;
                        break;
                    }
                }

                // See the detected language.
                detected.innerHTML = 'Detected: "' + result.language
                        + '" - aka "' + langName + '"';
            google.language.translate(text, result.language, 'en', function(result) {
            var translated = document.getElementById("transtext");
            if (result.translation) {
                translated.innerHTML = result.translation;
            }

        });

            }
        });


    }
    google.setOnLoadCallback(initialize);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
    <div id="content">Loading...</div>

</body>
</html>