You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Find Missing or Unused Translation Messages

4.1 version
Maintained Unmaintained

How to Find Missing or Unused Translation MessagesΒΆ

When maintaining an application or bundle, you may add or remove translation messages and forget to update the message catalogues. The debug:translation command helps you to find these missing or unused translation messages templates:

1
2
3
4
5
6
7
8
{# messages can be found when using the trans/transchoice filters and tags #}
{% trans %}Symfony is great{% endtrans %}

{{ 'Symfony is great'|trans }}

{{ 'Symfony is great'|transchoice(1) }}

{% transchoice 1 %}Symfony is great{% endtranschoice %}

Caution

The extractors can't find messages translated outside templates, like form labels or controllers. Dynamic translations using variables or expressions in templates are not detected either:

1
2
3
{# this translation uses a Twig variable, so it won't be detected #}
{% set message = 'Symfony is great' %}
{{ message|trans }}

Suppose your application's default_locale is fr and you have configured en as the fallback locale (see Installation and Fallback Translation Locales for how to configure these). And suppose you've already setup some translations for the fr locale:

  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- translations/messages.fr.xliff -->
    <?xml version="1.0"?>
    <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
        <file source-language="en" datatype="plaintext" original="file.ext">
            <body>
                <trans-unit id="1">
                    <source>Symfony is great</source>
                    <target>J'aime Symfony</target>
                </trans-unit>
            </body>
        </file>
    </xliff>
    
  • YAML
    1
    2
    # translations/messages.fr.yaml
    Symfony is great: J'aime Symfony
    
  • PHP
    1
    2
    3
    4
    // translations/messages.fr.php
    return array(
        'Symfony is great' => 'J\'aime Symfony',
    );
    

and for the en locale:

  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- translations/messages.en.xliff -->
    <?xml version="1.0"?>
    <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
        <file source-language="en" datatype="plaintext" original="file.ext">
            <body>
                <trans-unit id="1">
                    <source>Symfony is great</source>
                    <target>Symfony is great</target>
                </trans-unit>
            </body>
        </file>
    </xliff>
    
  • YAML
    1
    2
    # translations/messages.en.yaml
    Symfony is great: Symfony is great
    
  • PHP
    1
    2
    3
    4
    // translations/messages.en.php
    return array(
        'Symfony is great' => 'Symfony is great',
    );
    

To inspect all messages in the fr locale for the application, run:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ php bin/console debug:translation fr

+----------+------------------+----------------------+-------------------------------+
| State(s) | Id               | Message Preview (fr) | Fallback Message Preview (en) |
+----------+------------------+----------------------+-------------------------------+
| o        | Symfony is great | J'aime Symfony       | Symfony is great              |
+----------+------------------+----------------------+-------------------------------+

Legend:
  x  Missing message
  o  Unused message
  =  Same as the fallback message

It shows you a table with the result when translating the message in the fr locale and the result when the fallback locale en would be used. On top of that, it will also show you when the translation is the same as the fallback translation (this could indicate that the message was not correctly translated). Furthermore, it indicates that the message Symfony is great is unused because it is translated, but you haven't used it anywhere yet.

Now, if you translate the message in one of your templates, you will get this output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ php bin/console debug:translation fr

+----------+------------------+----------------------+-------------------------------+
| State(s) | Id               | Message Preview (fr) | Fallback Message Preview (en) |
+----------+------------------+----------------------+-------------------------------+
|          | Symfony is great | J'aime Symfony       | Symfony is great              |
+----------+------------------+----------------------+-------------------------------+

Legend:
  x  Missing message
  o  Unused message
  =  Same as the fallback message

The state is empty which means the message is translated in the fr locale and used in one or more templates.

If you delete the message Symfony is great from your translation file for the fr locale and run the command, you will get:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ php bin/console debug:translation fr

+----------+------------------+----------------------+-------------------------------+
| State(s) | Id               | Message Preview (fr) | Fallback Message Preview (en) |
+----------+------------------+----------------------+-------------------------------+
| x =      | Symfony is great | J'aime Symfony       | Symfony is great              |
+----------+------------------+----------------------+-------------------------------+

Legend:
  x  Missing message
  o  Unused message
  =  Same as the fallback message

The state indicates the message is missing because it is not translated in the fr locale but it is still used in the template. Moreover, the message in the fr locale equals to the message in the en locale. This is a special case because the untranslated message id equals its translation in the en locale.

If you copy the content of the translation file in the en locale, to the translation file in the fr locale and run the command, you will get:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ php bin/console debug:translation fr

+----------+------------------+----------------------+-------------------------------+
| State(s) | Id               | Message Preview (fr) | Fallback Message Preview (en) |
+----------+------------------+----------------------+-------------------------------+
|   =      | Symfony is great | J'aime Symfony       | Symfony is great              |
+----------+------------------+----------------------+-------------------------------+

Legend:
  x  Missing message
  o  Unused message
  =  Same as the fallback message

You can see that the translations of the message are identical in the fr and en locales which means this message was probably copied from French to English and maybe you forgot to translate it.

By default all domains are inspected, but it is possible to specify a single domain:

1
$ php bin/console debug:translation en --domain=messages

When the application has a lot of messages, it is useful to display only the unused or only the missing messages, by using the --only-unused or --only-missing options:

1
2
$ php bin/console debug:translation en --only-unused
$ php bin/console debug:translation en --only-missing

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.