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 7 application.
XLIFF is an XML-based translation file. Let's say we have the following XLIFF file:
<?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 the XLIFF file is valid, the command displays a success message, otherwise an 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, the problem can be fixed by adding the required id
attribute to the trans-unit
element:
<?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 adding lint:xliff
command to the list of commands which executed on each build.
Leave a Comment
Cancel reply