eugene-erg / xliff-formatter-icu-i18n-translator
XLIFF 1.2 and 2.0 translation files for eugene-erg/icu-i18n-translator: source and target in one file (Symfony, Crowdin, Lokalise, Phrase)
Package info
github.com/EugeneErg/xliff-formatter-icu-i18n-translator
pkg:composer/eugene-erg/xliff-formatter-icu-i18n-translator
Requires
- php: ^8.2
- ext-dom: *
- eugene-erg/icu-i18n-translator: ^4.4
- eugene-erg/icu-message-format-parser: ^3.0
Requires (Dev)
- ext-intl: *
- laravel/pint: ^1.32
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 16:40:58 UTC
README
XLIFF translation files for eugene-erg/icu-i18n-translator: the source language and one translation in one file, as Symfony, Crowdin, Lokalise and Phrase exchange them.
composer require eugene-erg/xliff-formatter-icu-i18n-translator
use EugeneErg\XliffFormatterIcuI18nTranslator\XliffFormatter; use EugeneErg\XliffFormatterIcuI18nTranslator\XliffVersion; $formatters = [ 'xlf' => new XliffFormatter(), // XLIFF 1.2, ICU messages 'xlf2' => new XliffFormatter(version: XliffVersion::V20), // XLIFF 2.0 ]; // the source texts and the translations the file already has, in one call $translator->addFile('xlf', 'messages', file_get_contents('translations/messages+intl-icu.fr.xlf'), 'en'); // the file as it is, with its French texts written in; notes, states and the header stay $current = file_get_contents('translations/messages+intl-icu.fr.xlf'); file_put_contents('translations/messages+intl-icu.fr.xlf', $translator->updateFile('xlf', 'messages', $current)); // a new file: the source and its French translation; missing texts are machine-translated $translator->getFile('xlf', 'messages', 'fr'); // translations from another XLIFF of the same file $translator->importTranslations('xlf', 'messages', file_get_contents('messages.de.xlf'), 'de');
Requires ext-dom.
Reading
- XLIFF 1.2 (also 1.0 and 1.1) and 2.x. The languages come from the file
(
source-language/target-language,srcLang/trgLang). The locale given toaddFile()is used only when the file does not name its target language. - A unit is keyed by
resname(1.2) orname(2.0), else by itsid. A<group>with a name or an id becomes a nested group; a group without either is merged into its parent. - A unit has no translation yet when it has no
<target>, when the target is empty while the source is not, when its 1.2 state isneworneeds-translation, or when a 2.0 segment has no target. The translator fills these in. - In 2.0, the segments and
<ignorable>parts of a unit are joined into one message. - The
<note>s of a unit (1.2) or of its<notes>(2.0) are the message's note for translators (core 4.0): the translator keeps it with the key and gives it to providers that translate whole messages (language models). - Units with
translate="no"are left out, as are elements of other namespaces (extensions). - Several
<file>elements are merged when they share their languages. A file that translates into two languages at once is refused: XLIFF has one target language per file, and so do the tools that make it. - Refused, with an exception naming the unit: two units with the same key, a DOCTYPE. External entities are never loaded.
Inline markup
Texts are XML, so inline elements stay in the messages: Click <g id="1">here</g> & go. The formatter
implements MarkupFormatterInterface (core 4.4), and the translator keeps the markup:
- providers see only the words: every tag is a placeholder that must come back once and still nest
(
Click {0}here{1} & go);<ph>,<bpt>,<ept>and<it>are codes with their native code inside,<g>,<pc>and<mrk>hold words to translate;<x/>,<bx/>,<ex/>,<sc/>,<ec/>are codes; - a target with other markup than its source is not imported (the reason is in
ImportResult::$skipped); - elements are written back as elements, in the XLIFF namespace, and a text is never laid out on lines;
- CDATA is words: Symfony's
<![CDATA[Open <file>]]>reads asOpen <file>and is written escaped.
Upgrading to 2.x
Texts are stored as XML now: &, < and > in them are &, < and >. A message with these
characters reads differently from 1.x, so after the upgrade it is a new message to the translator: its old
translations stay in the translation memory but are not linked to it, and it is translated again (or its
translations are imported again).
Machine translations: the review mark
The state of a target says who made it (core 4.3, Message::$machine):
| machine (waits for review) | a person's | |
|---|---|---|
1.2 <target state> |
needs-review-translation (and the other needs-review-*, needs-l10n, needs-adaptation) |
translated, final, signed-off |
2.0 <segment state> |
translated |
reviewed, final |
On export a provider's translation gets the machine state, a person's the done one (a target without a state is
left without it). On import the state decides: a state set to done in an editor approves the text. XLIFF 2.0 has
no state for review, so translated means a machine's translation not checked yet: a translator who works in 2.0
marks checked units reviewed.
Updating a file
updateFile() writes the texts into the file as it is:
- a source or target that already says the text is not touched; its
statechanges only to follow the review mark (below); a missing<target>andtarget-languageare added; - notes, headers, attributes, extensions and units marked
translate="no"stay; - units no longer in the source are removed, new ones are added to their group (or to the first file), laid out as the rest of the file;
- a 2.0 unit whose text changed becomes one segment;
- a file of other languages than the ones written is refused.
Writing a new file
getFile() writes a file from the translator's data, the way Symfony's XliffFileDumper writes it: one <file>,
the key in resname (1.2) or name (2.0), and a 7-character hash of the key path as id. Locales are
written as language tags (pt-BR). Only the texts and their notes are written: use updateFile() to keep the rest
of a file.
Messages
Messages are ICU MessageFormat by default, as in Symfony's messages+intl-icu.*.xlf. For other files, pass
another syntax:
use EugeneErg\IcuI18nTranslator\Syntax\TextSyntax; new XliffFormatter(new TextSyntax()); // "{" and "'" are plain characters
Development
composer check # pint, phpstan (level max), phpunit