eugene-erg / laravel-syntax-icu-i18n-translator
Laravel message syntax (:name placeholders, a|b plurals, {0} and [2,19] conditions) for eugene-erg/icu-i18n-translator: converted to ICU and back
Package info
github.com/EugeneErg/laravel-syntax-icu-i18n-translator
pkg:composer/eugene-erg/laravel-syntax-icu-i18n-translator
Requires
- php: ^8.2
- ext-intl: *
- eugene-erg/icu-i18n-translator: ^3.0
- eugene-erg/icu-message-format-parser: ^2.0.3
- illuminate/translation: ^11.0 || ^12.0 || ^13.0
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 17:07:16 UTC
README
Laravel message syntax for eugene-erg/icu-i18n-translator:
:name placeholders and a|b plurals with {0} and [2,19] conditions, turned into ICU MessageFormat
for translation and written back as Laravel lines.
'hi' => 'Hello, :name', // Hello, {name} 'apples' => '{0} No apples|[1,*] :count apples', // {count, plural, =0 {No apples} one {# apples} other {# apples}} 'files' => ':count file|:count files', // {count, plural, one {# file} other {# files}}
It is a syntax, not a file format: give it to a file formatter.
composer require eugene-erg/laravel-syntax-icu-i18n-translator
use EugeneErg\LaravelSyntaxIcuI18nTranslator\LaravelSyntax; use EugeneErg\PhpArrayFormatterIcuI18nTranslator\PhpArrayFormatter; use EugeneErg\JsonFormatterIcuI18nTranslator\JsonFormatter; $formatters = [ 'php' => new PhpArrayFormatter(new LaravelSyntax()), // lang/en/messages.php 'json' => new JsonFormatter(new LaravelSyntax()), // lang/en.json ]; $translator->addFile('php', 'messages', file_get_contents('lang/en/messages.php'), 'en'); file_put_contents('lang/ru/messages.php', $translator->getFile('php', 'messages', 'ru'));
Requires ext-intl.
Placeholders
| Laravel | ICU |
|---|---|
:name |
{name} |
:Name, :NAME |
{Name}, {NAME}: Laravel fills each with its own case, so they stay separate variables |
:count in a plural form |
# |
{, }, ', # |
text, quoted in ICU |
A placeholder is : followed by a letter or _, then letters, digits and _: 10:30 is text.
Laravel itself replaces only the keys it is given, so Note:important is read as a placeholder here
and stays Note:important at runtime unless an important key is passed.
Plurals
A line with | is a plural over :count, as trans_choice() reads it. The conversion asks Laravel
rather than imitating it: Laravel's own MessageSelector says what each number shows, php-intl says which
CLDR category each number is in. The ICU message then shows the same text for every whole number:
- the text most numbers of a category get becomes that category (
one,few,many, …); - numbers that get something else become
=N.[2,19]is listed number by number, as ICU has no ranges; [20,*]and the plain forms become the categories, as they cover numbers without end.
{0} none|[1,19] some|[20,*] many (en)
{count, plural, =0 {none} =2 {some} … =19 {some} one {some} other {many}}
Back to Laravel, there is one form per Laravel plural index, followed by {N} and [a,b] conditions for the
numbers that differ, consecutive numbers merged into ranges. When every form is the same text, the line is
written as people write it: {0} None|[1,*] :count items. Every line is checked against MessageSelector
before it is returned, and refused if any number would show something else.
Laravel's quirks are kept, not fixed. In {0} No apples|:count apple|:count apples (en) the number 1 shows
No apples, because conditions keep their place in the form list; the ICU message says so too.
Limits, to keep messages readable and the check fast:
- at most 1000 exact numbers (
=N) per message; - condition bounds up to 100 000.
Notes:
- Whole numbers are compared from 0 to 1099 (past the largest bound plus 200, if it is higher) and at a few large numbers. Laravel rules repeat every 10 or 100, so this covers them.
- Fractions: where no whole number is
other(ru, pl, uk…), ICUothergets the form of large numbers. Laravel has no fraction rules, sootheris dropped on the way back. #shows the number as the locale writes it (10,000), Laravel shows10000. This matters only while the message is in ICU: the line written back has:countagain.- The locale goes to Laravel as it is:
MessageSelectorknowspt_BRbut notpt-BR. Use the locale strings your application uses.
What has no Laravel form
fromIcu() throws LaravelSyntaxException (a TranslatorExceptionInterface) for:
select,selectordinal, several plurals in one message, a plural inside a plural;- a plural over a variable other than
count(trans_choice fills:count), or withoffset; - dates, times and formatted numbers (
{n, number, percent}); a plain{n, number}becomes:n; |in text: Laravel would split the line on it;- a plural form that starts like a condition (
[1] …), or a condition text with spaces at its ends, which Laravel trims.
Text around a plural is moved into each form: You have {count, plural, one {# apple} other {# apples}}
becomes You have :count apple|You have :count apples.
Development
composer check # pint, phpstan (level max), phpunit
The tests compare both directions with Laravel's MessageSelector and php-intl's MessageFormatter for every
number from 0 to 999.