読者です 読者をやめる 読者になる 読者になる

プログラミング C# - 翔ソフトウェア (Sho's)

C#/.NET/ソフトウェア開発など

Google AJAX Language API

ソフトウェア開発

Google AJAX Language API』を使ったサンプル。

into
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load('language','1');
  google.setOnLoadCallback(function (){
    var languageList = document.getElementById('languageList');
    for (var language in google.language.Languages) {
      var languageOption = new Option(language, google.language.Languages[language]);
      languageList.options[languageList.options.length] = languageOption;
    }
  });

function translate() {
  var sourceText = document.getElementById('sourceText').value;
  google.language.detect(sourceText, function(detectResult) {
  if (detectResult.error) {
    alert('Error:' + error.message);
    return;
  }
  var languageList = document.getElementById('languageList');
  targetLanguage = languageList.options[languageList.selectedIndex].value;
  google.language.translate(
    sourceText,
    detectResult.language,
    targetLanguage,
    function(result) {
      if (result.error) {
        alert('Error:' + result.message);
        return;
      }
      document.getElementById('destinationText').value = result.translation;
    });
  });
}
</script>
<div>
  <dl>
    <dd><textarea id="sourceText" cols="40" rows="8"></textarea></dd>
    <dt><input type="submit" value="Translate" onclick="translate()"> into <select id="languageList"></select></dt>
    <dd><textarea id="destinationText" cols="40" rows="8"></textarea></dd>
  </dl>
</div>