如何在iOS中编程使用语言翻译函数? [Swift 5]
我知道 uitextview
ios中的文本菜单中有一个 translate
函数
(例如,请参见从Xamarin中的TextView上的选择菜单中的选项)。
我想自己使用此功能,而无需使用 translate
菜单。例如,我想翻译在 uitextview
中显示的整个句子,而不是仅翻译所选的部分。
我们可以使用特定的 func
吗?我该如何称呼此功能?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
苹果的翻译 api 不可公开提供。
https://develovement.apple.apple.com/forums/thread/650024
您可以使用Google's
Googlemlkit/translate
而不是。在iOS上使用
ML Kit
。在 podfile 中添加
googlemlkit/translate
,通过在终端中运行以下命令。阅读 instal & 入门 https://cocoapods.org/ > Cocoapods ,或者您不知道该怎么做。
Google编写了非常好的示例代码。它很简单。
从中读取一串文本的字符串。
https://develovelers.google.com/ml-kit/language/language/lageage/translation/translation/ios
信息:
您可能会发现此链接有帮助。
https://maximbilan.medium.com/googletrans.com/googletranslate-apeletranslate-api-api-in-swift-aift-aift-a3664884848848.848
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.Read the Instal & Getting started sections 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
这可以从ios18获得。请参阅文档在这里。
您可以使用 提供翻译表。
如果您需要对翻译的更多控制权,并且需要更多的fucntionition,那么您应该考虑使用
Translations
。This is available from iOS18. See docs here.
You can use the
translationPresentation
to provide a Translation sheet.If you need more control over the the translation and want more fucntionality than opening a sheet you should look at using the
TranslationSession
.