Stringalize
← All articles

Keeping translation files in sync from the command line

· 6 min read

Translation files have two authors. Developers add strings in code and in the source-language file. Translators fill in every other language, somewhere else. Between the two, someone copies files back and forth: export from the translation tool, unzip, drag folders into place, hope nothing was overwritten.

The Stringalize command-line tool replaces that with two commands. stringalize push sends new strings to the project; stringalize pull writes every translation into the files the app already uses. This article explains how it decides what to send and what to write, so that neither side's work gets lost.

One rule: each side owns its files

Most sync problems come from both sides editing the same text. The CLI avoids them with a simple split:

  • The repository owns the strings. Keys and source text are added and changed in code, by developers. push uploads only the source-language files.
  • Stringalize owns the translations. Translators work in the editor. pull writes every other language, and never touches the source-language files.

So a push can't erase a translation someone made yesterday, and a pull can't undo a source change that hasn't been pushed yet. Both commands can run as often as you like.

Setting it up

The CLI is a single binary for macOS, Linux and Windows, with nothing else to install. On a Mac with Apple silicon:

curl -fsSL https://github.com/cbarbera80/stringalize-cli/releases/latest/download/stringalize_darwin_arm64.tar.gz | tar xz stringalize
sudo mv stringalize /usr/local/bin/

It authenticates with a project API key, created from the project's API dialog and read from an environment variable, so it never ends up in the repository:

export STRINGALIZE_KEY=sgz_…

Then, in the root of the repository:

stringalize init

init looks for translation files in the source language: String Catalogs, en.lproj tables, Android's res/values/strings.xml, Flutter's app_en.arb, JSON files in a locales or i18n folder. It writes what it finds to stringalize.yml, with the locale replaced by a placeholder:

files:
  - path: android/app/src/main/res/values-{android_locale}/strings.xml
    source: android/app/src/main/res/values/strings.xml
    tag: android
  - path: ios/MyApp/{locale}.lproj/Localizable.strings
    tag: ios

Each platform names its folders differently, so there are several placeholders: {locale} gives pt-BR.lproj, {android_locale} gives values-pt-rBR, and {locale_underscore} gives Flutter's app_pt_BR.arb. On Android the source language has no qualifier at all, which is what source is for.

Pushing strings

stringalize push

For each file, push uploads the source-language version. Strings that are new in the code are created in the project. Changed source text is updated, and, if the project is set up for it, the translations of that string are marked fuzzy so translators know to check them. Strings removed from the code stay in the project: deleting is a decision for a person, made with --delete-absent or in the editor.

An app that is already translated has its translations in the repository the first time. One command uploads them too:

stringalize push --translations

Translation files only fill in strings that come from the source file, and an empty translation never erases an existing one. After that first upload, the translations live in Stringalize.

A String Catalog is a special case, since one .xcstrings file holds every language. push removes the other languages from the copy it uploads, so only the source strings reach the project, unless you pass --translations.

Pulling translations

stringalize pull

For each file and each language in the project, pull downloads the file in its own format, with each platform's placeholder syntax: %@ for iOS, %1$s for Android, named placeholders for Flutter. It writes it to the path from the configuration, creating values-de/ or de.lproj/ when a language is new.

A file whose content hasn't changed isn't rewritten. That matters more than it sounds: git diff after a pull shows only the translations that really changed, and Xcode and Gradle don't rebuild resources for nothing.

Untranslated strings are left out of .strings and strings.xml files, so the app shows its source language for them, as it would anyway. For formats where a missing string would show as empty, such as JSON in a web app, a fallback fills the gaps:

  - path: web/src/locales/{locale}.json
    export:
      fallback: en

Several platforms, one project

A project is one set of strings, and by default pull would write all of them into every file. When an iOS app and an Android app each have their own keys, the Android strings.xml would receive iOS keys such as home.title, which isn't even a valid resource name.

Tags keep them apart. push tags every string in a file with that file's tag, and pull writes into the file only the strings with its tag. init adds a tag to each file when it finds more than one. If your platforms share their keys, give them the same tag, or remove the tags.

In CI

The commands are made to run unattended: no prompts, clear exit codes, and the key from an environment variable. Uploading new strings whenever main changes, in GitHub Actions:

- name: Push strings to Stringalize
  run: |
    curl -fsSL https://github.com/cbarbera80/stringalize-cli/releases/latest/download/stringalize_linux_amd64.tar.gz | tar xz stringalize
    ./stringalize push --tag-new needs-translation
  env:
    STRINGALIZE_KEY: ${{ secrets.STRINGALIZE_KEY }}

Before a release build, download the translations and check them:

stringalize pull
stringalize status --strict

status prints how much of each language is translated, how many strings are flagged, and how many translations fail validation, such as a missing placeholder or plural form. With --strict it exits with an error when any of those is not zero, so a release doesn't ship with half a language. A read-only key is enough for both commands.

Nothing hidden

The CLI is open source and uses only the public Stringalize API, the same calls you could make with curl: /api/import to push, /api/export to pull, /api/locales for status. It sends your translation files and your API key to the server in your configuration, and nothing else. If you prefer to script it yourself, the README describes the parameters it uses.