How to programmatically use the language translation function in iOS? [Swift 5]

Viewed 209

I am aware that there is a Translate function in a text menu in UITextView in iOS (see for example Remove Translate and share options from selection menu on textview in xamarin).

enter image description here

I would like to use this function by myself without using the Translate menu. For example, I would like to translate an entire sentence displayed in a UITextView instead of translating only the selected parts.

Is there a specific func that we can use? How can I call this function?

1 Answers

Apple's translate API is not available to the public.

https://developer.apple.com/forums/thread/650024

You can use Google's GoogleMLKit/Translate instead.


Use ML Kit on iOS.

Add GoogleMLKit/Translate in your podfile by running the following command in the terminal.

pod 'GoogleMLKit/Translate', '3.1.0'

Read the Instal the Getting started section on https://cocoapods.org/ if you have not set cocoapods or you do not know how to do so.

Google has written a very good sample code. It is straight forward. Read from the Translate a string of text section onwards.

https://developers.google.com/ml-kit/language/translation/ios


info:

You may find this link helpful.

https://maximbilan.medium.com/googletranslate-api-in-swift-a36489a84508

Related