sheets_i18n

A flutter tool to synchronize i18n (internationalization) arb files with google sheets.

Usage

1. Prepare a Google Sheet Document

Screenshot 2024-09-23 at 09 45 39

You can get the sample here: docs.google.com

It should look like this:

  • the first column is the message key (used to lookup the translation in the app).

  • the columns after that are the language codes.

  • the rows are for each specific translated word/sentence.

  • copy the sheet ID: https://docs.google.com/spreadsheets/d/THIS PART OF THE URL/edit?pli=1&gid=0#gid=0

2. Configure a service account

  1. Create a new service account in the Google Cloud Console
  2. Turn on the Google Sheets API
  3. Copy the service account's email address
  4. Share the google sheet with the service account and give it edit privileges Screenshot 2024-09-23 at 09 49 49
  5. Download the service account JSON (it's local path will later be added to the pubspec configuration)

⚠️ DO NOT COMMIT YOUR SERVICE ACCOUNT JSON ⚠️

3. Prerequisites with intl_translation

  1. Having setup intl_translation and flutter_localizations, incl. a MyAppLocalizations and MyAppLocalizationsDelegate class (see our example app)
  2. Having run the intl_translation commands to generate the arb files github.com/dart-lang/i18n
flutter pub run intl_translation:extract_to_arb --output-dir=lib/l10n lib/localizations.dart
  1. Create new ARB files for each locale, naming them intl_<locale>.arb (e.g., intl_de.arb for German, intl_es.arb for Spanish).

4. Install this package

flutter pub add sheets_i18n

5. Configure the pubspec.yaml

add this section:

sheets_i18n:
  service_account_path: ./path/to/service_account.json
  sheet_id: 34tv34rv324rv23rv3r43r43red89f8hs89duzfs
  localizations_file: './lib/localizations.dart' # file with the localization messages - optional - default is ./lib/main.dart
  localizations_path: './lib/l10n' # folder for arb files - optional - default is ./lib/l10n 

6. Run the sync script

flutter pub run sheets_i18n:update

This will 2-way-sync all the changes:

  • message keys that are not present in the Google Sheet will be added
  • new translations will be pulled and written to the arb files

7. Run intl_translation's arb to dart

Run the intl_translation commands to generate the dart code from the arb files github.com/dart-lang/i18n

flutter pub run intl_translation:generate_from_arb \
    --output-dir=lib/l10n --no-use-deferred-loading \
    lib/localizations.dart \
    lib/l10n/intl_en.arb lib/l10n/intl_de.arb lib/l10n/intl_cs.arb