Purpose Automatically translate a long text in an SAP transaction |
Solution Example The translation is done in the background:
The sales text is now created in all desired languages:
The new translations are also inserted when existing texts are changed:
Outcome:
Video
Box (7,89) (14,146) "Translation Settings"
// Input field for API Key
InputField (12,91) "API Key" (12,101) size="40" maxLength="200" name="apikey" -invisible
// Pushbutton to start the translation process
PushButton (9,91) "Translate English text to French/German/Italian" process="translate.txt" size=(2,50)
Using KEY = [apikey]
// Read text from SAP text control (asynchronously)
CallVBasync guinet.textcontrol.gettext textname:="oldtext"
// Target languages
Set V[languages] "F=FR,D=DE,I=IT" // to French, German, and Italian
// Show status message
StatusMessage title="Automatic translation..."
label translate
// Current language
Set V[current_language] "&V[languages](1-1)"
Set V[current_language2] "&V[languages](3-4)"
StatusMessage addString="Translating to &V[current_language2]"
// Create text
Enter "=TEAN"
// Set source and target language
Screen SAPLMG19.1000
Set F[Create text in] "&V[current_language]"
Set F[Copy from] "E"
Enter
Screen SAPLMGMM.4040
// Translate text
CallVB guixttranslate.translator.TranslateText _
"&U[KEY]" "oldtext" "EN" "newtext" "&V[current_language2]"
// Set new text
CallVBasync guinet.textcontrol.settext textname:="newtext"
// Next language
Set V[languages] "&V[languages](6-100)"
// Any language left?
if V[languages]
goto translate
endif
// Close the status message window
StatusMessage -remove
// No further action on this screen
Enter ?
Public Function TranslateText(apiKey As String, textname1 As String, language1 As String,
textname2 As String, language2 As String) As String
' Create an instance of the GuiXT object
Dim guixt As New guinet.guixt
' Base URL for the free API version of DeepL
Dim host As String = "https://api-free.deepl.com"
' Route for translation request, includes API key and parameters for source and target languages
Dim route As String = "/v2/translate?auth_key=" & apiKey & "&source_lang=" _
& language1 & "&target_lang=" & language2 _
& "&text=" & WebUtility.UrlEncode(guixt.GetText(textname1))
' Set security protocols for the request
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 _
Or SecurityProtocolType.Tls13
' Create the web request with the specified host and route
Dim myrequest As WebRequest = WebRequest.Create(host & route)
myrequest.Method = "POST" ' Set the method to POST
myrequest.Timeout = 10000 ' Set the timeout for the request to 10 seconds
' Get the response from the web request
Dim response As WebResponse = myrequest.GetResponse()
' Read the response stream
Dim sr As New StreamReader(response.GetResponseStream())
' Parse the result from the response
Dim result As String = sr.ReadToEnd()
sr.Close() ' Close the stream reader
' Deserialize the JSON response to an object
Dim serializer As New JavaScriptSerializer
Dim o As System.Object = serializer.Deserialize(Of Object)(result)
' Set the translated text to the specified GuiXT text variable
guixt.SetText(textname2, o.item("translations")(0).item("text").ToString)
' Return success indicator
Return "X"
End Function
VB Project (.zip file) |
Components InputAssistant + Controls |