Skip to content
Docs

Flutter & CI setup

Pull translations into a Flutter app from CI with the API.

Everyone3 min read

This page lists the variables a script needs, shows the pull script Localizator ships with, and adapts it for a Flutter app built in GitHub Actions.

The three variables

VariableValue
LOCALIZATOR_API_URLLocalizator's address, such as https://localizator.carcutter.io
LOCALIZATOR_PROJECT_IDThe project's id, also in the project's address in the app
LOCALIZATOR_API_TOKENA project API key, starting with lt_

The token used to be called LOCALIZATOR_TOKEN. The pull script still accepts the old name, but it's deprecated: rename it in your CI secrets.

Where to get them

  1. Create an API key

    In the project, open Settings › API keys and select Create API key. Name it after where it's used, such as "ci-github": the name shows in Activity when the key pushes changes. See Pull translations in CI.

  2. Copy the variables

    Under Settings › Environment variables, select Copy. You get all three lines, with the token of the newest API key, ready for a .env file or your CI's secrets. Reveal token shows the token first.

Store the token as a secret, never in the repository.

The pull script

pull-translations.sh, at the root of Localizator's repository, is the reference pull flow:

  1. Reads .env and .env.local next to the script, if they exist. Variables already set in the environment win.
  2. Calls /status and reads every langCode. The source language comes first and becomes the default locale.
  3. Downloads each language with type=json&export_empty_as=main, so empty values fall back to the source text, into .translations/<locale>.json.
  4. Writes src/i18n/config.ts with the list of locales and the default one.

It stops with an error when the project id or the token is missing. Without LOCALIZATOR_API_URL it uses http://localhost:3000, so always set it in CI.

The script is written for a TypeScript app (the config.ts it generates). For Flutter, keep the same calls and ask for ARB instead, as below.

Flutter: ARB files

Add type=arb to the download to get a Flutter ARB file:

curl -fsS -H "X-Api-Token: $LOCALIZATOR_API_TOKEN" \
  "$LOCALIZATOR_API_URL/api/v1/projects/$LOCALIZATOR_PROJECT_ID/files/download?lang_codes=de&type=arb&export_empty_as=main" \
  -o lib/l10n/app_de.arb

The file has the locale in @@locale, then one entry per key, and a @key entry with the description for keys that have one:

{
  "@@locale": "de",
  "inspectionTitle": "Fahrzeuginspektion",
  "@inspectionTitle": {
    "description": "Title of the inspection screen"
  },
  "photosCount": "{count, plural, one {# Foto} other {# Fotos}}"
}

Things to know:

  • ARB files are always flat. The nested option only applies to JSON.
  • Keys are written exactly as they are in the project. Flutter's gen-l10n turns each key into a Dart method, so name keys like inspectionTitle, not inspection.title.
  • Only descriptions are exported as metadata, not placeholder types or examples.
  • Plurals and selects are ICU MessageFormat, which Flutter understands. See Writing ICU plurals.
  • One language per request. Call /status to find which languages exist.

A pull script for a Flutter app, saved as tool/pull-translations.sh:

#!/bin/bash
set -euo pipefail

API_URL="${LOCALIZATOR_API_URL:?Set LOCALIZATOR_API_URL}"
PROJECT_ID="${LOCALIZATOR_PROJECT_ID:?Set LOCALIZATOR_PROJECT_ID}"
API_TOKEN="${LOCALIZATOR_API_TOKEN:-${LOCALIZATOR_TOKEN:-}}"
[ -n "$API_TOKEN" ] || { echo "Set LOCALIZATOR_API_TOKEN"; exit 1; }

PROJECT_URL="$API_URL/api/v1/projects/$PROJECT_ID"
OUT_DIR="lib/l10n"
mkdir -p "$OUT_DIR"

# /status is pretty-printed: one "langCode" per line.
LANG_CODES=$(curl -fsS -H "X-Api-Token: $API_TOKEN" "$PROJECT_URL/status" \
  | grep langCode | awk -F '"' '{print $4}')

for lang in $LANG_CODES; do
  file="$OUT_DIR/app_${lang//-/_}.arb"
  curl -fsS -H "X-Api-Token: $API_TOKEN" \
    "$PROJECT_URL/files/download?lang_codes=$lang&type=arb&export_empty_as=main" \
    -o "$file"
  echo "Downloaded $lang → $file"
done

curl -f makes the script fail when the API answers with an error, such as 401 for a revoked key, instead of writing the error into an ARB file.

GitHub Actions

Add the three values as repository secrets (Settings › Secrets and variables › Actions in GitHub), then pull before building, for example in .github/workflows/build.yml:

name: Build

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: subosito/flutter-action@v2
        with:
          channel: stable

      - name: Pull translations
        run: bash tool/pull-translations.sh
        env:
          LOCALIZATOR_API_URL: ${{ secrets.LOCALIZATOR_API_URL }}
          LOCALIZATOR_PROJECT_ID: ${{ secrets.LOCALIZATOR_PROJECT_ID }}
          LOCALIZATOR_API_TOKEN: ${{ secrets.LOCALIZATOR_API_TOKEN }}

      - run: flutter pub get
      - run: flutter gen-l10n
      - run: flutter build apk

With generate: true in pubspec.yaml, flutter pub get already runs gen-l10n; the separate step makes it explicit.

Pushing strings back

The same key can upload a language to Localizator, for example new source strings added in code: POST /api/v1/projects/{projectId}/files with an ARB or JSON file. Values pushed this way arrive as drafts with the cloud icon. See Load (upload) a locale.

Related