po-formatter-icu-i18n-translator
gettext PO files for eugene-erg/icu-i18n-translator: the source texts (msgid) and one translation (msgstr), plural forms included, turned into ICU MessageFormat for translation.
composer require eugene-erg/po-formatter-icu-i18n-translatoruse EugeneErg\PoFormatterIcuI18nTranslator\PoFormatter;
$formatters = ['po' => new PoFormatter(sourceLocale: 'en')];
// the source texts and the translations the file has (fuzzy ones as machine translations; empty ones and
// msgmerge's fuzzy guesses with a previous "#| msgid" are not translations)
$translator->addFile('po', 'messages', file_get_contents('locale/ru/LC_MESSAGES/messages.po'), 'en');
// the file as it is, translated where it is not: comments, references, flags and the header stay
$current = file_get_contents('locale/ru/LC_MESSAGES/messages.po');
file_put_contents('locale/ru/LC_MESSAGES/messages.po', $translator->updateFile('po', 'messages', $current));
// a translation from a template: Language and Plural-Forms are added
$translator->updateFile('po', 'messages', file_get_contents('messages.pot'), 'de');Requires ext-intl. A PO file does not name its source language: it is sourceLocale, English by default.
Plural forms
The order of msgstr[0], msgstr[1]… is the file's own Plural-Forms formula, not CLDR. The formula is read by a small parser (on eugene-erg/string-parser) and evaluated, never executed: n, integers and the C operators, nothing else. Every formula gettext knows reads (tested on all of them).
For every whole number the formula says which form shows and php-intl says the CLDR category, so:
- on import, a form becomes the category most of its numbers fall into (
one,few,many…), numbers where gettext and CLDR differ become=N, andotherof a language without whole "other" numbers (ru, pl) gets the form of large numbers; - on export, each form gets the text the ICU message shows for its numbers; if they do not agree, the message has no form in this file and is refused.
The plural variable is n, as gettext names it. A file without Plural-Forms uses the formula gettext uses for its language (CLDR data of gettext/languages).
Texts
| Flag | Text |
|---|---|
c-format, php-format | printf: argument N is {argN}; %ld, %u… keep their conversion in the name ({arg1_ld}) |
none, no-c-format | plain text: { and % are characters |
python-format, javascript-format… | refused |
The key of an entry is the one gettext makes: msgctxt and msgid joined by EOT ("menu\x04Open").
msgctxt is also the context of the message (core 4.0): "Open" of a menu and "Open" of a status are translated apart. The extracted comments (#.) are its note: the translator keeps it with the key and gives it to providers that translate whole messages (language models).
Fuzzy: machine translations
fuzzy marks a translation that waits for a person (core 4.3, Message::$machine). On export a provider's translation is written fuzzy, a person's without the flag. On import the flag decides: a fuzzy entry stays a machine translation, one whose flag a translator removed in Poedit or another editor becomes a person's, even with the same text. msgmerge's guesses (fuzzy with a previous #| msgid) are made for another text and are not read.
Updating a file
updateFile() writes the translations into the file as it is:
- an entry whose translation is already there is not touched;
- an entry that gets a translation loses its previous
#| msgid; it isfuzzywhile a provider's translation waits for a person, and without the flag once a person wrote or approved it; - comments (
#,#.,#:), flags, line wrapping and the header stay; a missingLanguageis added, andPlural-Formswhen the file has plurals; - entries no longer in the source become obsolete (
#~), asmsgmergedoes; new ones are added after the last entry; - a file of another language is refused.
getFile() writes a new file: a header with Language, Plural-Forms and UTF-8, then the entries. The msgid is the source text, the note is written as #. comments, and c-format is set on texts with arguments. Language is written as gettext writes it: de_DE, pt_BR.
A PHP project — WordPress, whose tools and Poedit check php-format — flags its new entries so:
new PoFormatter(printf: 'php-format');An entry already in a file keeps the flag it has.
Errors
PoException (a TranslatorExceptionInterface) names the line or the entry: line 120: an unclosed string, msgid "%d file": 2 plural forms where Plural-Forms says 3.
Development
composer check # pint, phpstan (level max), phpunitFrom the README.md of EugeneErg/po-formatter-icu-i18n-translator · composer require eugene-erg/po-formatter-icu-i18n-translator