Table of Content
GitHub is central to many development teams’ workflows. In this guide, you’ll learn how to synchronize changes between files hosted on GitHub and your localization projects in Transifex. We will show you how to do this with the Transifex Client and also how to use the Client in conjunction with a Continuous Integration tool for a more automated workflow.
Although this article refers to GitHub, a similar workflow can also be used for GitLab.
Syncing a local project to Transifex with the Transifex Client
Let’s say you made changes in your code and there are new strings that need to be translated. You can use the Transifex Client to push these source changes to Transifex.
Whenever you make changes to strings in your codebase, be sure to update your localization files before committing changes. It’s a good practice to enforce a push policy to ensure everyone knows exactly when to sync their changes to Transifex.
Here are the steps to sync a local project to Transifex using the Client:
git pullto ensure your local repository is up to date with the remote GitHub repository.
Install the Transifex Client if it’s not already installed. You can find detailed instructions on how to do this here. The Transifex Client is based on the Git client and uses a similar command structure.
Create a project in Transifex if you don’t have one yet. This project will be used for localizing the resources in your GitHub repository.
From your GitHub repository root, run the
tx initcommand to initialize your local project configuration.
$ tx init
Creating .tx folder…
Transifex instance [https://www.transifex.com]:
Creating config file…
You should now have a .tx folder inside of your repository. Inside this folder, a configuration file will contain the information used to identify the project on a Transifex server.
Update the .tx/config file with the configuration for the files you want to localize in Transifex. For example, suppose your localization resources are stored in a directory called locales. In that case, your source translations are stored in a file called
en.json, and the slug of the Transifex project you created is
i18n, you can issue the following command to create the configuration for this resource:
tx set --auto-local -r i18n.enjson 'locales/<lang>.json' --source-lang en --type KEYVALUEJSON --source-file locales/en.json --execute
Now, your .tx/config should look like this:
host = https://www.transifex.com
file_filter = locales/<lang>.json
source_file = locales/en.json
source_lang = en
type = KEYVALUEJSON
Repeat the same steps for all the files you want to include in your localization process, then add the .tx folder to your Git repository using
git add. When other developers pull your changes, they can use the same configuration file to connect their Transifex clients to the Transifex project.
For more details about the different ways to set up a project with the Transifex Client, refer to this article.
When you’re ready to push your updated files to Transifex, use the
tx push command. The
-s flag pushes source files, while the
-t flag pushes translation files:
$ tx push -st
Pushing translations for resource i18n.enjson:
Pushing source file (locales/en.json)
Pushing 'de' translations (file: locales/de.json)
Pushing 'es' translations (file: locales/es.json)
To pull changes into your local project folder, use
$ tx pull -a
New translations found for the following languages: de, es
Pulling new translations for resource i18n.enjson (source: locales/en.json)
-> de: locales/de.json
-> es: locales/es.json
From here, simply stage, commit, then push the updated localization files to your GitHub repository.
Integrating the Client with Travis CI
You can automate the above process and create a continuous localization workflow by incorporating the Transifex Client into your continuous integration chain. Here's how to do it:
Follow the steps in the section above to set up your .tx/config file and commit it to your git branch.
Visit the Travis CI website and create an account if you don't have one yet.
Head to travis-ci.org/profile and enable travis-ci for your repository.
On your enabled repository, hit More options > Settings at the top right.
In the Environment Variables section, add two new secret env vars. You can use
Inside your repository, add a .travis.yml file if you don't already have one. We will install Transifex Client and push if the tests pass successfully. Once you have a .travis.yml file, add a section like this inside of it:
- pip install virtualenv
- virtualenv ~/env
- source ~/env/bin/activate
- pip install transifex-client
- sudo echo $'[https://www.transifex.com]\nhostname = https://www.transifex.com\nusername = '"$TRANSIFEX_USER"$'\npassword = '"$TRANSIFEX_PASSWORD"$'\ntoken = '"$TRANSIFEX_API_TOKEN"$'\n' > ~/.transifexrc
- tx push -s
Commit the travis.yml file to the branch you want to sync translations from. Now every time you commit changes in this branch, your content will be uploaded to Transifex, and your resources will be updated automatically.