Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Swift Plugin
Last modified Tue, 24th Nov 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /Swift/Translate
Download this example: Translate.zip
Project "Translate.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Opening()
if TranslationSessionMBS.Available then
// okay
else
MessageBox "This example needs macOS 26."
end if
End EventHandler
End Class
Class MainWindow Inherits DesktopWindow
Control StatusButton Inherits DesktopButton
ControlInstance StatusButton Inherits DesktopButton
EventHandler Sub Pressed()
var fromLang as string = PopupFromLang.SelectedRowText
var toLang as string = PopupToLang.SelectedRowText
var tag as string = fromLang+" -> "+toLang
TranslationSessionMBS.Status(fromLang, toLang, AddressOf StatusCompleted, tag)
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control PopupFromLang Inherits DesktopPopupMenu
ControlInstance PopupFromLang Inherits DesktopPopupMenu
EventHandler Sub SelectionChanged(item As DesktopMenuItem)
session = nil
End EventHandler
End Control
Control PopupToLang Inherits DesktopPopupMenu
ControlInstance PopupToLang Inherits DesktopPopupMenu
EventHandler Sub SelectionChanged(item As DesktopMenuItem)
session = nil
End EventHandler
End Control
Control InputTextField Inherits DesktopTextField
ControlInstance InputTextField Inherits DesktopTextField
End Control
Control TranslateButton Inherits DesktopButton
ControlInstance TranslateButton Inherits DesktopButton
EventHandler Sub Pressed()
if session = nil then
var fromLang as string = PopupFromLang.SelectedRowText
var toLang as string = PopupToLang.SelectedRowText
session = new TranslationSessionMBS(fromLang, toLang)
end if
if session <> nil then
if not Keyboard.AsyncOptionKey then
// Just translate one string
log "> " + InputTextField.Text
session.translateString InputTextField.Text, AddressOf translateStringCompleted
else
var requests() as TranslationRequestMBS
requests.add new TranslationRequestMBS(InputTextField.Text)
requests.add new TranslationRequestMBS("Hello World. Just a test.")
requests.add new TranslationRequestMBS("And this is just a test.")
// run all at once
session.translations Requests, AddressOf translationsCompleted
// run multiple but get a reponse when one is done
'session.translateBatch Requests, AddressOf translateStringCompleted, AddressOf translationsCompleted
end if
end if
End EventHandler
End Control
Control MatrixButton Inherits DesktopButton
ControlInstance MatrixButton Inherits DesktopButton
EventHandler Sub Pressed()
MatrixWindow.show
End EventHandler
End Control
EventHandler Sub Opening()
// check status for some combinations of languages
var Status_en_de as String = TranslationSessionMBS.Status("en", "de")
log "en -> de: "+Status_en_de
var Status_en_fr as String = TranslationSessionMBS.Status("en", "fr")
log "en -> fr: "+Status_en_fr
var Status_en_it as String = TranslationSessionMBS.Status("en", "it")
log "en -> it: "+Status_en_it
// quick test to translate something
session = new TranslationSessionMBS("en", "de")
var Translation as TranslationResponseMBS = session.translateString("Hello World")
log "Translation of Hello World to German: "+Translation.targetText
// check for available languages
TranslationSessionMBS.SupportedLanguages AddressOf SupportedLanguagesCompleted
End EventHandler
Sub Log(s as string)
List.AddRow s
List.ScrollPosition = List.LastAddedRowIndex
End Sub
Sub StatusCompleted(ErrorMessage as string, Status as String, tag as Variant)
log "Status for "+tag+": "+status
if ErrorMessage <>"" then
log "Error: "+ErrorMessage
end if
End Sub
Sub SupportedLanguagesCompleted(ErrorMessage as string, identifiers() as string, tag as Variant)
identifiers.Sort
System.DebugLog "Supported languages: "+string.FromArray(identifiers, ", ")
for each s as string in identifiers
PopupFromLang.AddRow s
PopupToLang.AddRow s
next
PopupFromLang.Enabled = true
PopupToLang.Enabled = true
StatusButton.Enabled = true
End Sub
Sub translateStringCompleted(ErrorMessage as string, Response as TranslationResponseMBS, tag as Variant)
if Response <> nil then
log Response.sourceLanguage+" -> "+Response.targetLanguage+": "+Response.targetText
end if
if ErrorMessage <>"" then
log "Error: "+ErrorMessage
end if
End Sub
Sub translationsCompleted(ErrorMessage as string, Responses() as TranslationResponseMBS, tag as Variant)
for each response as TranslationResponseMBS in Responses
log Response.sourceLanguage+" -> "+Response.targetLanguage+": "+Response.targetText
next
if ErrorMessage <>"" then
log "Error: "+ErrorMessage
end if
End Sub
Property session As TranslationSessionMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
Class MatrixWindow Inherits DesktopWindow
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
EventHandler Sub Opening()
TranslationSessionMBS.SupportedLanguages AddressOf SupportedLanguagesCompleted
End EventHandler
Sub StatusCompleted(ErrorMessage as String, Status as String, Tag as Variant)
var p as pair = tag
var x as integer = p.Left.IntegerValue + 1
var y as integer = p.Right.IntegerValue
List.CellTextAt(y, x) = status
List.CellTooltipAt(y, x) = status
if status <> "supported" then
List.CellBoldAt(y,x) = true
end if
End Sub
Sub SupportedLanguagesCompleted(ErrorMessage as string, identifiers() as string, tag as Variant)
identifiers.Sort
list.ColumnCount = identifiers.Count+1
List.HeaderAt(0) = "Languages"
var u as integer = identifiers.LastIndex
for x as integer = 0 to u
List.HeaderAt(x+1) = identifiers(x)
next
for y as integer = 0 to u
List.AddRow identifiers(y)
next
for x as integer = 0 to u
for y as integer = 0 to u
if x = y then
List.CellTextAt(y, x+1) = "-"
else
TranslationSessionMBS.Status(identifiers(x), identifiers(y), AddressOf StatusCompleted, new Pair(x, y))
end if
next
next
End Sub
End Class
End Project
Download this example: Translate.zip
The items on this page are in the following plugins: MBS Swift Plugin.