Polyglot is a CLI tool to manage strings in Android resource files and automate translations using the Google Translate API. It helps you:
- Check if translations are normalized (e.g., sorted by key), and discover potentially unused string resources.
- Normalize translations by sorting keys automatically.
- Remove string keys across all language files at once.
- Translate new or existing strings to multiple locales using Google Translate.
Below are detailed instructions on how to build, install, configure, and use Polyglot.
- Sorting: Ensures all string keys in
strings.xml
are alphabetically sorted. - Translation: Integrates with the Google Translate API to generate localized strings automatically.
- Cleaning Up: Removes unused keys quickly across multiple language files if they are not actually referenced in Kotlin code.
- Interactive Selection: Provides an interactive UI to select your
res/
directory from multiple Android resource paths in your project.
Visit Polyglot releases page and download the latest version for your operating system.
unzip polyglot_Linux_x86_64.zip
Move the polyglot
binary to a directory in your PATH
.
sudo mv polyglot /usr/local/bin/
Tip
It is recommended to install it in /usr/local/bin
, this will ensure that polyglot will always be available on your system without interfering with other system programs.
polyglot help
Clone this repository or download the code:
git clone https://github.com/gustoliveira/polyglot.git
cd polyglot
Run directly:
go build -o polyglot main.go
go install
Generate polyglot completion to your specific shell. Is available to bash
, fish
, zsh
and powershell
polyglot completion bash > /tmp/polyglot-completion
source /tmp/polyglot-completion
Alternativally you can use Makefile target build
to install with bash
autocompletion
make build # Builds a binary called 'polyglot', install and source autocompletion
The CLI needs a Google Translate API key to handle translations. You can supply it in one of two ways:
- Environment Variable: Set
GOOGLE_TRANSLATE_KEY
:export GOOGLE_TRANSLATE_KEY="YOUR_API_KEY"
- Command Flag: Pass
--googleApiKey
to thetranslate
command:polyglot translate --googleApiKey="YOUR_API_KEY" ...
Once installed, run polyglot <command>
in the root directory of your Android project. Polyglot attempts to detect whether the current directory is an Android project by checking for typical files like build.gradle
, settings.gradle
, or an app/
directory. If these are not found, Polyglot exits with an error.
polyglot help
Checks all the resource files for:
- Key sorting: Reports if any file is not sorted.
- Unused keys: Searches for keys in your
.kt
files. If Polyglot cannot find references likeR.string.<your_key>
, that key is labeled “possibly unused.”
Run:
polyglot check
Sorts all string keys in strings.xml
files by alphabetical order across your selected resource directory. If any file is not sorted, Polyglot corrects it in place.
Run:
polyglot normalize
Removes a specified key across all strings files in a resource directory.
Flags:
--key
or-k
(required): The string key to remove.
Usage:
polyglot remove --key="example_key"
Translates a single English string (--value
, -v
) into every language variant found in your Android res/
folder (e.g., values-es
, values-fr
, etc.). It then appends or substitutes the key in each strings.xml
.
Flags:
--key
,-k
(required): The key to use for the translated string.--value
,-v
(required): The English text to translate.--googleApiKey
,-g
: Custom Google Translate API key (optional if environment variable is set).--force
: Force substitution if a key already exists.
Usage:
polyglot translate --key="welcome_message" \
--value="Welcome to our app!" \
--googleApiKey="YOUR_API_KEY" \
--force
This CLI is structured using Cobra. Each command is defined in a separate file under cmd/
. For example:
remove.go
handles theremove
command.normalize.go
handles thenormalize
command.- etc
To add a new subcommand:
- Create a new file in
cmd/
. - Define a new
*cobra.Command
. - Initialize and add it to
rootCmd
ininit()
.
Polyglot checks for any of these in the current directory to confirm you’re in an Android project:
build.gradle
settings.gradle
settings.gradle.kts
app/
If it doesn’t find at least one, the CLI will exit.
Polyglot is MIT Licensed. See the LICENSE file for details.