arb_translate: Automate ARB File Translation with Google Gemini API
Published on by Flutter News Hub
arb_translate
is a command-line tool designed to automate the generation of missing translations for ARB files. It leverages Google's Gemini API to translate strings, significantly simplifying the localization process for Flutter apps using flutter_localizations
.
Configuration
With l10n.yaml
Configuration File
- Create a Gemini API token and save it in the environment variable
ARB_TRANSLATE_API_KEY
or add it tol10n.yaml
. - (Optional) Specify the application context in
l10n.yaml
usingarb-translate-context:
.
Without l10n.yaml
Configuration File
- Set the API token directly as a command argument:
--api-key
. - Specify other parameters as command arguments, such as:
-
--arb-dir
: Directory containing the template and translated ARB files. -
--template-arb-file
: The template ARB file for translation. -
--context
: (Optional) Application context.
-
Usage
With l10n.yaml
$ arb_translate
Without l10n.yaml
$ arb_translate --arb-dir...
Code Examples
Using l10n.yaml
arb-translate-api-key: GEMINI_API_TOKEN
arb-translate-context: Sporting goods store app
Without l10n.yaml
$ arb_translate --api-key GEMINI_API_TOKEN --arb-dir ./arb --template-arb-file my_template.arb --context "Sporting goods store app"
Benefits
- Automates translation of missing ARB file entries.
- Integrates seamlessly with Flutter apps using
flutter_localizations
. - Simplified configuration process with
l10n.yaml
file.
Additional Notes
- In regions where Gemini API is unavailable, use Vertex AI service with
arb-translate-model-provider: vertex-ai
. - For more information and success stories, visit the LeanCode blog.