Introducing Swift Translate
translate string catalogs using ChatGPT
Today we are launching an early preview of Swift Translate, an open-source CLI Tool / Swift Package Plugin for extracting and automatically translating text in your apps that utilize Xcode String Catalogs. It was quite the endeavor to get to this point, so read on to learn more about how we got here…
To localize or not to localize?
Localizing your app can be an expensive and laboring process, especially for small development teams like ours. When we built Producer Toolkit, we knew we wanted to support as many languages as possible (obviously, talented music producers and DJs don’t exist only in English-speaking countries).
With no budget to hire a full localization team, or the resources to maintain translations as we rapidly iterated on features, we looked into ways to automate the localization process.
While there are many services out there that developers can hook into for translating your apps, none of them worked directly with the new String Catalogs format—it required extracting everything to a localization bundle than importing it back in. Furthermore, translations could take days with many of these services, and we needed to get new text in the app translated quickly to avoid holding up our release schedule.
Hopping on the AI bandwagon
With the recent advances in LLMs, we speculated that could a be solution for automating localization of our apps. We figured with the craze around AI, there was no way someone else hadn’t already thought of this—and sure enough, someone had already built a macOS app that utilized OpenAI’s GPT 3.5 / 4 models to translate strings from a Xcode localization bundle.
While the translations themselves were good (as far as we could tell by spot checking with other translation services), we started to running into some bugs and time-consuming shortcomings of the app.
One of the biggest issues was that newly translated text was not always reimporting into the Xcode project. This wouldn’t have been a big problem, except there wasn’t a CLI interface so we could automate the process to retry on failures. And while the app has a cache feature to avoid retranslating already translated strings, importing/exporting localization bundles via Xcode (or xcodebuild) was a time-consuming operation, especially if importing failed.
Oh, and it cost $30/month!
if you want something done right…
Since String Catalogs are just plaintext JSON files, importing/exporting a localization bundle seemed like an unnecessary step. You should be able to extract the strings to translate from the JSON, send them to GPT, and then put them directly back into the the catalog. We also didn’t need a whole app to do this, it could just be a simple CLI tool that we could integrate into our CI pipeline. Plus, working directly with the GPT APIs is extremely cheap (especially 3.x). You could translate entire books into multiple languages and not even come close to $30.
When our search for a new tool that worked this way came up empty, we decided to build one.
Enter Swift Translate
Swift Translate works exactly as we just described—it decodes the String Catalog JSON into a flat array of strings that can be sent to the GPT 3.5 Turbo model for translation (using your own OpenAI API key). Once all the strings are translated, it rebuilds the String Catalog model and encodes it back into JSON to be saved to disk.
To be honest, correctly decoding and encoding the String Catalog JSON structure was the most time consuming part of building this tool due to the complexity around device-type variations, plural string variations, and argument placeholders. On the other hand, working with the GPT API is pretty much the same as interacting with ChatGPT itself—just with a few extra levers to get the output you want.
A long, but prosperous, road ahead
As mentioned, this first release of Swift Translate is a very early technology preview. Like anything built on top of ChatGPT, it’s far from perfect. GPT doesn’t always return the translated text in the correct format—our prompt probably needs some adjusting. We know it’s possible to get it to a reliable point though, as the aforementioned macOS app never had any weird formatting issues in the translated text. If you’re a GPT or Prompt Guru and would like to help us out with that, we’d love to hear from you.
Swift Translate was architected in a way that makes it easy to implement other LLMs or more traditional translation services, and we have plans to do so in upcoming releases. We’re also planning on adding support for translating txt files, which will allow you to automate translating your App Store description & release notes via fastlane deliver.
We’re really excited about this project—we believe it’ll be the quickest and easiest way for more developers to localize their apps and expand their reach into new markets.
If you’d like to get involved, check out the open issues on GitHub to see where you can help—or if you have any new ideas feel free to drop a ticket in or open a PR.