Using PhraseApp with Symfony 4

Sylvain Fabre
AssoConnect
Published in
2 min readSep 16, 2019

In this article, you will learn how to use PhraseApp with Symfony 4 and how Symfony’s domains can work with PhraseApp’s tags.

One of our summer projects at AssoConnect is to make our code translatable.
Right now there are only French strings all over the code and we plan to support other locales like English.

Why PhraseApp?

We have selected PhraseApp to host our translation strings.

Our choice was motivated by:
- PhraseApp supports branches and merges like Git
- PhraseApp hosts our strings like Github can host your code
- PhraseApp provides an interface for translators to work on our project
- PhraseApp has an API and a CLI client to pull and push as you do with Git

But PhraseApp lacks versioning to truly be used like a Git equivalent.

Symfony domains vs PhraseApp tags

The Symfony Translator component has a feature to split your translation strings into different parts.

Each part is called a domain:
- all strings are in the same file named [domain's name].[locale].yml
- it works like a PHP namespace: you can have two strings with the same id and a different content if each one is in its own domain.

PhraseApp has a similar feature called tags:
- each string may have one or more tags
- you can pull all the strings from a tag and save them in a single file

So you can use PhraseApp tags as Symfony domains:
- tag all the strings from one domain with the domain’s name
- pull strings from one tag to get the domain’s file

How to handle duplicate key’s name?

PhraseApp expects each string to have a unique key across all your project so you may experience issues if you have two strings with the same key across different Symfony domains.

The following files cannot be uploaded at the same time on PhraseApp as the key hello is used 2 times.

#domain_A.fr_FR.yml
hello: "Bonjour {firstname} !"
#domain_B.fr_FR.yml
hello: "Bienvenue {firstname} !"

Our solution at AssoConnect is to tag and prefix the PhraseApp keys with the Symfony domain’s name.

Using the previous example, you’ll have on PhraseApp these keys:

domain_A.hello: “Bonjour {firstname} !”
domain_B.hello: “Bienvenue {firstname} !”

We add this prefix on each push and we strip it on each pull.

Happy coding!

--

--

Sylvain Fabre
AssoConnect

Happy CTO at https://www.assoconnect.com a SaaS product to help non-profits focus on their missions. We’re hiring!