eugene-erg / android-resources-formatter-icu-i18n-translator
Android string resources (strings.xml) for eugene-erg/icu-i18n-translator: printf strings, plurals and string arrays, or ICU messages inside the XML
Package info
github.com/EugeneErg/android-resources-formatter-icu-i18n-translator
pkg:composer/eugene-erg/android-resources-formatter-icu-i18n-translator
Requires
- php: ^8.2
- ext-dom: *
- ext-libxml: *
- eugene-erg/icu-i18n-translator: ^4.4.1
- eugene-erg/icu-message-format-parser: ^3.0
- eugene-erg/printf-format-parser: ^1.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:30 UTC
README
strings.xml files for
eugene-erg/icu-i18n-translator: import the strings of an app,
export translations that the app uses natively, with getString() and getQuantityString().
composer require eugene-erg/android-resources-formatter-icu-i18n-translator
use EugeneErg\AndroidResourcesFormatterIcuI18nTranslator\AndroidXmlFormatter; $translator = new Translator(/* … */, formatters: ['android' => new AndroidXmlFormatter()]); $translator->addFile('android', 'strings.xml', file_get_contents('res/values/strings.xml'), 'en'); $translator->importTranslations('android', 'strings.xml', file_get_contents('res/values-de/strings.xml'), 'de'); // a new file file_put_contents('res/values-ru/strings.xml', $translator->getFile('android', 'strings.xml', 'ru')); // or the file as it is, with the Russian texts written in: comments, tools: attributes and the rest stay $current = file_get_contents('res/values-ru/strings.xml'); file_put_contents('res/values-ru/strings.xml', $translator->updateFile('android', 'strings.xml', $current, 'ru'));
Two formatters
AndroidXmlFormatter |
AndroidIcuXmlFormatter |
|
|---|---|---|
| Strings in the file | printf: Hi %1$s, %2$d new |
ICU: Hi {name}, {count, plural, …} |
| The app formats with | getString(id, args), getQuantityString() |
android.icu.text.MessageFormat (API 24+) |
| Plurals | <plurals> |
an ICU plural inside a <string> |
select, selectordinal, two plurals |
refused: native resources have none | yes |
Native Android resources have no select: a message with one fails in AndroidXmlFormatter with the resource
name. Write such strings as ICU with AndroidIcuXmlFormatter, as Google advises for gender and other selects. The
grammatical gender of the user (Android 14, values-fr-feminine/) is a set of resource folders, not a select, and
is not supported yet.
What is handled
- Escapes are resolved on import and written back on export:
\'\"\\\n\t\@\?\uXXXX. An apostrophe is always escaped on export, because an unescaped one breaks the build. Whitespace collapses as aapt collapses it, and"quoted"spans keep their spaces. Spaces that aapt would drop are written as\u0020. - Markup stays in the text:
<b>,<i>,<a href>,<xliff:g>, CDATA,&. Both formatters implementMarkupFormatterInterface(core 4.4), so the translator keeps it: providers see only the words, every tag is a placeholder that must come back once and nest,<xliff:g>with its content (%1$s, an example) is not translated, and a translation with other markup is not imported. CDATA is words: its HTML is text, and a translation writes it escaped (<a href=\"…\">), which is the same string to Android. If a provider breaks a tag, the text is written escaped, so the app still builds. - Not imported:
translatable="false", references (@string/other,?attr/x) and other resource types (dimen,color,integer-array). These are not texts to translate. - printf ↔ ICU comes from printf-format-parser in its Java dialect.
%1$.2fbecomes{0, number, ::.00 group-off rounding-mode-half-up}, which prints exactly what Android prints. Formats without an exact ICU counterpart (%x,%5d,%e) fail with the resource name, not silently. - Plain text:
50% off, orformatted="false", is not a format. It is imported as text, and a text with%and no arguments is exported withformatted="false". - Plurals:
<plurals name="files">becomes{files, plural, one {…} other {…}}. In Android the quantity is a separate argument ofgetQuantityString(), so the selector is named after the resource and the items keep their own%1$d. All CLDR categories (zero,one,two,few,many,other) map one to one. - A plural inside a sentence (
You have {0, plural, …} in {1}) is moved to the top on export, since<plurals>covers a whole string.#of a numbered selector becomes%1$d.
What Android cannot express fails with the resource name instead of being changed: select, selectordinal, two
plurals in one string, exact numbers (=0), an offset, named variables, and # of a named selector.
Version 1.0.0
Do not use 1.0.0. It passed messages to the translator as strings, which the translator stores as plain text, so
Hello %1$s came back as Hello {0}. 2.0 works with translator 3.0, and a test runs the whole import and export
path through the real translator.
Updating a file
updateFile() (core 3.1) writes the texts into the file as it is:
- a resource that already says the same is not touched: its escaping, CDATA and printf form (
%srather than%1$s) stay; - a changed resource keeps its element and attributes (
tools:ignore…); one that changed its kind (a string that became plurals) is replaced; - comments,
translatable="false"strings, references and other resources (dimen,integer-array…) stay; - resources no longer in the source are removed; new ones are written after the resource before them in the source, in its order.
getFile() builds the file from the translator's tree: comments, tools: attributes and the resources that are
not strings are not in it.
Development
composer check # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage
License
MIT