Skip to content

Android resources formatter ​

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().

bash
composer require eugene-erg/android-resources-formatter-icu-i18n-translator
php
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 ​

AndroidXmlFormatterAndroidIcuXmlFormatter
Strings in the fileprintf: Hi %1$s, %2$d newICU: Hi {name}, {count, plural, …}
The app formats withgetString(id, args), getQuantityString()android.icu.text.MessageFormat (API 24+)
Plurals<plurals>an ICU plural inside a <string>
select, selectordinal, two pluralsrefused: native resources have noneyes

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, &amp;. Both formatters implement MarkupFormatterInterface (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 (&lt;a href=\"…\"&gt;), 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$.2f becomes {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, or formatted="false", is not a format. It is imported as text, and a text with % and no arguments is exported with formatted="false".
  • Plurals: <plurals name="files"> becomes {files, plural, one {…} other {…}}. In Android the quantity is a separate argument of getQuantityString(), 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 (%s rather 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 ​

bash
composer check    # pint --test, phpstan (level max, strict rules) and phpunit
composer coverage

License ​

MIT


From the README.md of EugeneErg/android-resources-formatter-icu-i18n-translator · composer require eugene-erg/android-resources-formatter-icu-i18n-translator