Google翻訳を使う
このチュートリアルでは、テキストを翻訳するGoogleのWebサービスを使用します。翻訳が必要なテキストは、URLパラメーターとして渡されます。その結果は。Webブラウザーコントロール内のDOM構造から抽出され、GuiXTに返されます。

ここでは、テキストボックスを入力として使用し、ロングテキスト変数を使用して後でそのテキストにアクセスします。プッシュボタンは、翻訳されたテキストを他のロングテキスト変数を介して2番目のテキストボックスに設定するVB.NET関数を呼び出すInputScriptを処理します。
pushbutton (20,1) “Translate” process=“translate_text.txt”
text (22,1) “english”
TextBox (20,13) (25,100) name=“translate_this”
text (27,1) “german”
TextBox (26,13) (31,100) name=“translated_text”
Inputscript “translate_text.txt”:
callvb tutorials.translator.translate “translate_this” “en” “translated_text” “de”
return
VB.NET関数の実装:
プロジェクトにguinet.dllへの参照を追加します。このプロジェクトは、必ずクラスライブラリ(DLL)としてコンパイルしてください。

次に、コンパイルされたDLLファイルおよびguinet.dll(GuiXTセットアップに付属)を、GuiXTプロファイルで指定されたディレクトリにコピーします。
VB.NETのコードは以下のようになります。その下に注記を付けてあります。
| 1 | Imports guinet |
| 2 | Public Class translator |
| 3 | Inherits System.Windows.Forms.WebBrowser |
| 4 | |
| 5 | Private done As Boolean |
| 6 | |
| 7 | Public Function translate(ByVal textidIn As String, _ |
| 8 | ByVal LangIn As String, _ |
| 9 | ByVal textidOut As String, _ |
| 10 | ByVal LangOut As String) As String |
| 11 | |
| 12 | ‘Access to GuiXT variables and SAP GUI Session |
| 13 | Dim g = New guixt |
| 14 | |
| 15 | ‘Get content of GuiXT text variable |
| 16 | Dim translateThis = g.GetText(textidIn) |
| 17 | |
| 18 | ‘ Build Google translation URL |
| 19 | Dim url = “http://www.google.com/translate_t?langpair=” _ |
| 20 | & LangIn & “|” & LangOut & “&text=” _ |
| 21 | & System.Uri.EscapeUriString(translateThis) |
| 22 | |
| 23 | done = False |
| 24 | Navigate(url) |
| 25 | |
| 26 | ‘ wait until new document is loaded |
| 27 | While Not done |
| 28 | Application.DoEvents() |
| 29 | End While |
| 30 | |
| 31 | ‘ text translation done |
| 32 | Dim text = Document.GetElementById(“result_box”).InnerText |
| 33 | |
| 34 | ‘Transfer translation back to GuiXT text variable |
| 35 | g.SetText(textidOut, text) |
| 36 | |
| 37 | Return “” |
| 38 | |
| 39 | End Function |
| 40 | |
| 41 | ‘ Document completed |
| 42 | Public Sub MyDocumentCompleted _ |
| 43 | (ByVal sender As Object, _ |
| 44 | ByVal e As WebBrowserDocumentCompletedEventArgs) _ |
| 45 | Handles Me.DocumentCompleted |
| 46 | done = True |
| 47 | End Sub |
| End Class |
注記とヒント:
14行目:
textidlnは、翻訳されるテキストが格納されたロングテキスト変数の名前です。内容を読み出すにはguixt.getTextを呼び出します。
39行目:
Google翻訳にどのくらい時間がかかるか分からないため、ブラウザーコントロールの”documentCompleted”イベントを待つ必要があります。
29行目:
サイトが完全に読み込まれると、wb.Document.GeElementByIdを介してサイトのすべての要素にアクセスできます。ここでは特に、翻訳されるテキストを含む要素result_boxに着目します。
32行目:
翻訳されたテキストは、guixt.SetTextコマンドを使用してSAP GUIウィンドウに直接表示されます。ここでは、GuiXTスクリプト内のTextBox name=コマンドで定義されたロングテキスト変数の名前が”textidOut”に格納されます。この名前は、VB.NET関数にパラメーターとして渡されます。
長いテキストに関する注意: 翻訳されるテキストを含むURLの長さには制限があるため、1度に1センテンスのみ翻訳するようにしてください。

