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), discover potentially unused string resources, and incosistencies keys between resource.
- 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.
Warning
While you can use Polyglot on Linux, Windows, and MacOS, not all features are available on Windows and MacOS platforms. Only Linux has full support for now.
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
Important
Polyglot requires Go 1.23 or later to build from source. If you don't have Go installed, you can download it from the official website or using asdf.
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 selected 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.” - Missing translations between files: Report if there're keys that exists in a file and is missing in others.
Flags:
--all
: Check the resource directory for all modules.
Run:
polyglot check
Important
Searching for unused keys uses a simple regex pattern to find references in Kotlin files. It may not catch all references, especially if you use a different pattern or have complex code.
Important
Searching for unused keys is not available on Windows.
Tip
Install ripgrep to improve the search performance. It is a faster alternative to grep
and ag
that is available on most package managers.
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.
Flags:
--all
: Normalize the resource directory for all modules.
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
.
If the file is sorted, it will be added maintaining the sort property. Otherwise, it will be appended at the end.
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.--print-only
: Only print the translations instead of adding.
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.