Telegram iOS Translate Feature — How the hell did they do it?
Telegram for iOS has just added a new feature to translate a message. When I tried that feature out, I was pretty stunned. How the hell did they do it? I’ve done a pretty extensive research and Apple simply don’t provide any public API to do this as of iOS 15. Did I miss something?
Check out their blog post to see how it looks https://telegram.org/blog/reactions-spoilers-translations
Well, Telegram is open source so I decided to take a look at the code, and voila!
Telegram-iOS/Translate.swift at master · TelegramMessenger/Telegram-iOS
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
github.com
let textView = UITextView()textView.text = texttextView.isEditable = falseif let navigationController = context.sharedContext.mainWindow?.viewController as? NavigationController, let topController = navigationController.topViewController as? ViewController { topController.view.addSubview(textView) textView.selectAll(nil) textView.perform(NSSelectorFromString(["_", "trans", "late:"].joined(separator: "")), with: nil) DispatchQueue.main.async { textView.removeFromSuperview() }}
What they do is
- Add a new
UITextView
to the top most view controller - Populate it with the text you want translated and manually select it
- Call private API
_translate:
- Remove the text view
Looking a bit deeper, they’d have some issues with crashes when the text contains emojis, so they’re filtering them out.
Anyway, this is a pretty genius way to simply add translations to your app. The only thing to consider is that you’re using private API that may stop working at any point so you should check for respondsToSelector
to prevent crashes and bear in mind the it may stop working at any time and your users are not going to be happy if this feature suddenly disappears from the app.
Ten articles before and after
YGG SEA Sticker Contest. We have an exciting competition for… – Telegram Group
Digital Privacy: A Myth or Reality..? – Telegram Group
Как закрепить сообщение в Телеграм. Что нужно сделать? – Telegram Group
How to install Telegram on Ubuntu 20.04 – Telegram Group
TON history. Telegram’s blockchain project, the TON… – Telegram Group
How to create a telegram bot in GOLANG – Telegram Group
Smart Door Lock System using esp32-cam and Telegram chatbot – Telegram Group
Minipod do Telegram – Telegram Group
An overview of Telegram monetization efforts – Telegram Group
Translate text through Google Translate using Python – Telegram Group