|
| 1 | +ICU-lite MessageFormat (Plural / Select) |
| 2 | +======================================== |
| 3 | + |
| 4 | +``i18n_test.check_catalog`` only compares placeholder *sets* and ``interpolate`` |
| 5 | +does flat ``${var}`` substitution — neither can render the count-aware messages |
| 6 | +real localisation needs, e.g. ``"{count, plural, one {# item} other {# items}}"``. |
| 7 | +This implements the ICU MessageFormat subset most apps use. |
| 8 | + |
| 9 | +Pure standard library; imports no ``PySide6``. The plural/ordinal category |
| 10 | +functions are pure and the rule callables are injectable, so rendering is fully |
| 11 | +deterministic in CI. |
| 12 | + |
| 13 | +Headless API |
| 14 | +------------ |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + from je_auto_control import format_message, plural_category, ordinal_category |
| 19 | +
|
| 20 | + plural = "{count, plural, one {# item} other {# items}}" |
| 21 | + format_message(plural, {"count": 1}) # '1 item' |
| 22 | + format_message(plural, {"count": 5}) # '5 items' |
| 23 | +
|
| 24 | + select = "{g, select, male {He} female {She} other {They}} won" |
| 25 | + format_message(select, {"g": "female"}) # 'She won' |
| 26 | +
|
| 27 | + ordinal = "{place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" |
| 28 | + format_message(ordinal, {"place": 3}) # '3rd' |
| 29 | +
|
| 30 | + plural_category(2) # 'other' |
| 31 | + ordinal_category(3) # 'few' |
| 32 | +
|
| 33 | +Supported: simple ``{name}`` arguments, ``select`` (e.g. gender), ``plural`` and |
| 34 | +``selectordinal`` with the CLDR categories (``zero``/``one``/``two``/``few``/ |
| 35 | +``many``/``other``), exact ``=N`` selectors that win over a category, the ``#`` |
| 36 | +count placeholder, a plural ``offset:`` (``#`` becomes count − offset), nested |
| 37 | +arguments, and ICU apostrophe quoting (``''`` → ``'``; ``'{'`` → literal brace). |
| 38 | +``plural_rules`` / ``ordinal_rules`` let you inject custom category functions; |
| 39 | +``locale`` selects the built-ins (``en``, ``fr``). |
| 40 | + |
| 41 | +Executor commands |
| 42 | +----------------- |
| 43 | + |
| 44 | +``AC_format_message`` takes a ``pattern`` plus a JSON ``args`` object and returns |
| 45 | +``{text}``, accepting ``locale``. It is exposed as the MCP tool |
| 46 | +``ac_format_message`` and as a Script Builder command under **Data**. |
0 commit comments