xcstrings-formatter-icu-i18n-translator
Apple String Catalogs (.xcstrings) for eugene-erg/icu-i18n-translator: every language of an app in one file. Format strings, plural and device variations and substitutions are turned into ICU MessageFormat for translation and written back the way Xcode writes them.
composer require eugene-erg/xcstrings-formatter-icu-i18n-translatoruse EugeneErg\XcstringsFormatterIcuI18nTranslator\XcstringsFormatter;
$formatters = ['xcstrings' => new XcstringsFormatter()];
// the source strings and every translation the catalog already has
$translator->addFile('xcstrings', 'Localizable', file_get_contents('Localizable.xcstrings'), 'en');
// the catalog as it is, with every language it holds translated; comments, states and other strings stay
$catalog = file_get_contents('Localizable.xcstrings');
file_put_contents('Localizable.xcstrings', $translator->updateFile('xcstrings', 'Localizable', $catalog));
// add German, leave the other languages as they are
file_put_contents('Localizable.xcstrings', $translator->updateFile('xcstrings', 'Localizable', $catalog, 'de'));
// a new catalog of the source and the languages asked for
$translator->getFile('xcstrings', 'Localizable', 'de', 'ru', 'ja');Requires ext-intl.
From Apple to ICU
| String Catalog | ICU |
|---|---|
"Hello, %@!" | Hello, {arg1}! |
%lld, %lf | {arg1, number, ::precision-integer ,_}, {arg1, number, ::.000000 ,_ rounding-mode-half-even} |
%d, %ld, %u, %s, %f … | the same, named arg1_d, arg1_ld, arg1_u, arg1_s, arg1_f |
variations.plural | {arg1, plural, one {…} other {…}} over the one argument of the string |
%#@files@ + substitutions.files | {arg1, plural, …} in place, %arg being arg1 |
variations.device | {device, select, iphone {…} mac {…} other {…}} |
zero plural form | =0 (Foundation uses it for exactly 0), or zero in a language with a CLDR zero category (Arabic, Latvian…) |
The printf argument N is the ICU variable argN. A number in ICU does not say whether it was %d, %ld or %lld, and passing a Swift Int where %d expects an Int32 prints garbage, so every specifier other than the ones Xcode writes (%@, %lld, %lf) keeps its conversion in the variable name. Translators never see the names: placeholders are protected.
The conversion of numbers is exact (printf-format-parser): %.2f is .00, no grouping unless %'lld. What ICU cannot print the same way (%x, %e, %5d) is refused.
- A string's source is its source-language localization or, for a string extracted from code, its key.
- A translation is taken only if all of it is translated: no
stringUnitwith statenewor an empty value. needs_reviewmarks a machine translation (core 4.3,Message::$machine): a translation with a unit in that state is read as a machine one; a provider's translation is writtenneeds_review, a person'stranslated. A state set back totranslatedin Xcode approves the text.- A string without arguments is text:
100% surestays100% sure. So is one with a percent sign that cannot be a format:50% off %@(% o). - Strings with
"shouldTranslate": falseare left out.
From ICU to Apple
A plural that is the whole message becomes plural variations; a plural inside text, or one whose forms use other arguments, becomes a substitution named after its argument (%#@arg1@); {device, select, …} becomes device variations, with substitutions of each device named arg1_mac, arg1_other. A bare {arg1} that a plural counts is written as a number (%lld), not as %@. Positions are written only when the arguments are not in order (%2$@ owes %1$@).
The file is written as Xcode writes it: two-space indent, " : ", keys sorted, the strings in their order. A string whose source text is its key has no source localization, as Xcode extracts it.
Refused, with an exception naming the string: other selects, named variables ({name}), exact numbers in a plural other than =0, offsets, dates, nested plurals, groups of keys.
Updating a catalog
updateFile() writes into the catalog as it is in the project:
- the languages asked for (all languages of the catalog when none are) get their texts, missing ones machine-translated;
- a localization that already says the same text stays as it is, unless its review mark has to change (a translation approved or made by a provider since);
- comments, extraction states, other languages, strings with
"shouldTranslate": false, the substitution names and the version of the file stay; - strings no longer in the source are removed, new ones are added at the end;
- a string without a comment gets the note the translator keeps for it.
A language whose texts are all new (added in Xcode, not translated yet) is a language of the catalog too.
The comment of a string is the note of its message: the translator keeps it with the key and gives it to providers that translate whole messages (language models).
getFile() writes a new catalog from the translator's texts and notes alone: comments, no extraction states, every unit translated, substitutions named after their argument, strings with "shouldTranslate": false left out.
Development
composer check # pint, phpstan (level max), phpunitFrom the README.md of EugeneErg/xcstrings-formatter-icu-i18n-translator · composer require eugene-erg/xcstrings-formatter-icu-i18n-translator