Check Errors in XLIFF Files using Console Command in Symfony 6

Check Errors in XLIFF Files using Console Command in Symfony 6

Symfony framework provides static code analysis commands (also known as linters) for checking some parts of application such YAML syntax, TWIG syntax, service definitions in container, etc.

This tutorial shows how to check errors in XLIFF files using console command in Symfony 6 application.

XLIFF is an XML-based translation file. Let's say we have the following XLIFF file:

translations/messages.de.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>
                <source>hello_world</source>
                <target>Hallo Welt</target>
            </trans-unit>
        </body>
    </file>
</xliff>

The lint:xliff command can be used to check if XLIFF files don’t have any syntax errors. We can provide XLIFF filename as argument:

php bin/console lint:xliff translations/messages.de.xlf

If XLIFF file is valid, command displays success message, otherwise error is printed:

ERROR  in translations/messages.de.xlf 
* Line 5, Column 0: Element '{urn:oasis:names:tc:xliff:document:1.2}trans-unit': The attribute 'id' is required but missing.
* Line 5, Column 0: Element '{urn:oasis:names:tc:xliff:document:1.2}trans-unit': Not all fields of key identity-constraint '{urn:oasis:names:tc:xliff:document:1.2}K_unit_id' evaluate to a node.

We can check a whole directory as well by giving directory name as argument:

php bin/console lint:xliff translations

In our case, problem can be fixed by adding required id attribute to trans-unit element:

translations/messages.de.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="hello_world">
                <source>hello_world</source>
                <target>Hallo Welt</target>
            </trans-unit>
        </body>
    </file>
</xliff>

If continuous integration is used, recommended to add lint:xliff command to the list of commands which executed on each build.

Leave a Comment

Cancel reply

Your email address will not be published.