Translating With Aichat

Today, I was working on a new feature for one of my Android apps. I added new strings, and wanted to translate them into the plethora of languages available in my app. I thought it would be great if I can automate the process somehow.

Enter aichat, a command line tool for using various LLMs from the command line. Aichat supports a “roles” feature, which allows us to have different prompts for the LLM, which can then be used accordingly. The documentation has an example for a translator, which I started by modifying to add the following:

Note that the target language might either be a language name (ex Arabic), or an ISO 639-1 language code (ex ar), or the aformentioned lagnuage code followed by an ISO 3166-1-alpha-2 region code preceded by a lowercase r (ex en-rUS).

With this, running something like: aichat -r translator:ar "Hello, World!" would result in:

مرحبا بك، يا عالم!

Now to actually automate it, we can run a command like this:

for lang in ar de es fa fr vi zh; do
   AICHAT_RESULT=$(aichat -r translator:$lang "Playback Speed:")

   # I want the new string to be added right before the stop string
   sed -i '' "/<string name=\"stop\">/a\\
  <string name=\"playback_speed\">${AICHAT_RESULT}<\\/string>\\

This will update the strings.xml for each language in the aforementioned list, adding the new string right under the stop string. Note that the sed command is for macOS, and might need to be modified for other platforms.