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 catalogs. The debug:translation command helps you to find these missing or unused translation messages templates:

アプリケーションまたはバンドルを保守するときに、翻訳メッセージを追加または削除し、メッセージ カタログを更新するのを忘れる場合があります。 debug:translation コマンドは、これらの不足または未使用の翻訳メッセージ テンプレートを見つけるのに役立ちます。
1
2
3
4
{# messages can be found when using the trans filter and tag #}
{% trans %}Symfony is great{% endtrans %}

{{ 'Symfony is great'|trans }}

Caution

注意

The extractors can't find messages translated outside templates (like form labels or controllers) unless using Translations or calling the trans() method on a translator. Dynamic translations using variables or expressions in templates are not detected either:

Translations を使用するか、トランスレータで trans() メソッドを呼び出す場合を除き、エクストラクタはテンプレートの外部 (フォームラベルやコントローラなど) で翻訳されたメッセージを見つけることができません。テンプレート内の変数または式を使用した動的変換も検出されません。
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 Translations and Translations for how to configure these). And suppose you've already setup some translations for the fr locale:

アプリケーションの default_locale が fr で、フォールバック ロケールとして en を構成したとします (これらの構成方法については、翻訳と翻訳を参照してください)。そして、fr ロケール用にいくつかの翻訳を既にセットアップしているとします。
  • XML
    XML
  • YAML
    YAML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
<!-- translations/messages.fr.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<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>

and for the en locale:

en ロケールの場合:
  • XML
    XML
  • YAML
    YAML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
<!-- translations/messages.en.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<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>

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

アプリケーションの fr ロケールですべてのメッセージを調べるには、次を実行します。
1
2
3
4
5
6
7
$ php bin/console debug:translation fr

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

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.

frlocale でメッセージを翻訳したときの結果と、フォールバック ロケール en が使用されたときの結果を示す表が表示されます。それに加えて、翻訳がフォールバック翻訳と同じ場合にも表示されます (これは、メッセージが正しく翻訳されていないことを示している可能性があります)。まだどこにも使っていません。

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

ここで、テンプレートの 1 つでメッセージを翻訳すると、次の出力が得られます。
1
2
3
4
5
6
7
$ php bin/console debug:translation fr

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

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

状態は空です。これは、メッセージが fr ロケールで翻訳され、1 つ以上のテンプレートで使用されていることを意味します。

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

fr ロケールの翻訳ファイルからメッセージ Symfony is great を削除してコマンドを実行すると、次のようになります。
1
2
3
4
5
6
7
$ php bin/console debug:translation fr

---------  ------------------  ----------------------  -------------------------------
 State      Id                  Message Preview (fr)    Fallback Message Preview (en)
---------  ------------------  ----------------------  -------------------------------
 missing    Symfony is great    Symfony is great        Symfony is great
---------  ------------------  ----------------------  -------------------------------

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.

この状態は、メッセージが fr ロケールで翻訳されていないため欠落していることを示していますが、テンプレートではまだ使用されています。さらに、fr ロケールのメッセージは、en ロケールのメッセージと同じです。翻訳されていないメッセージ ID は en ロケールでの翻訳と同じであるため、これは特殊なケースです。

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:

en ロケールの翻訳ファイルの内容を fr ロケールの翻訳ファイルにコピーしてコマンドを実行すると、次のようになります。
1
2
3
4
5
6
7
$ php bin/console debug:translation fr

----------  ------------------  ----------------------  -------------------------------
 State       Id                  Message Preview (fr)    Fallback Message Preview (en)
----------  ------------------  ----------------------  -------------------------------
 fallback    Symfony is great    Symfony is great        Symfony is great
----------  ------------------  ----------------------  -------------------------------

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 English to French and maybe you forgot to translate it.

メッセージの翻訳が frand en ロケールで同一であることがわかります。これは、このメッセージが英語からフランス語にコピーされた可能性があり、翻訳するのを忘れた可能性があることを意味します。

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:

アプリケーションに大量のメッセージがある場合、 --only-unused または --only-missing オプションを使用して、未使用または不足しているメッセージのみを表示すると便利です。
1
2
$ php bin/console debug:translation en --only-unused
$ php bin/console debug:translation en --only-missing

Debug Command Exit Codes

The exit code of the debug:translation command changes depending on the status of the translations. Use the following public constants to check it:

debug:translation コマンドの終了コードは、翻訳のステータスによって異なります。次のパブリック定数を使用して確認します。
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;

// generic failure (e.g. there are no translations)
TranslationDebugCommand::EXIT_CODE_GENERAL_ERROR;

// there are missing translations
TranslationDebugCommand::EXIT_CODE_MISSING;

// there are unused translations
TranslationDebugCommand::EXIT_CODE_UNUSED;

// some translations are using the fallback translation
TranslationDebugCommand::EXIT_CODE_FALLBACK;

These constants are defined as "bit masks", so you can combine them as follows:

これらの定数は「ビット マスク」として定義されているため、次のように組み合わせることができます。
1
2
3
if (TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED) {
    // ... there are missing and/or unused translations
}