Skip to content

[Translation] Document range translation using the ICU MessageFormat #12400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions translation/message_format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,69 @@ Usage of this string is the same as with variables and select::
}
}

Ranges
~~~~~~

Sometimes, it can be handy to use ranges for a translation. The ``intl`` component provides a way to do this, using the ``choice`` function.

.. configuration-block::

.. code-block:: yaml

# translations/messages+intl-icu.en.yaml
balance_account: >-
{balance, choice,
-∞ < Oops! I'm down |
0 < I still have money |
1000 # I have a lot of money !
}

.. code-block:: xml

<!-- translations/messages+intl-icu.en.xlf -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="balance_account">
<source>balance_account</source>
<target>{balance, choice, -∞ < Oops! I'm down | 0 < I still have money | 1000 # I have a lot of money !}</target>
</trans-unit>
</body>
</file>
</xliff>

.. code-block:: php

// translations/messages+intl-icu.en.php
return [
'balance_account' => '{balance, choice,
-∞ < Oops! I\'m down |
0 < I still have money |
1000 # I have a lot of money!
}',
];

Here are some examples of what this ``choice`` formatter will render::

// prints "Oops! I'm down"
echo $translator->trans('balance_account', ['balance' => -10]);
echo $translator->trans('balance_account', ['balance' => 0]);

// prints "I still have money"
echo $translator->trans('balance_account', ['balance' => 10]);
echo $translator->trans('balance_account', ['balance' => 999]);

// prints "I have a lot of money!"
echo $translator->trans('balance_account', ['balance' => 1000]);

This formatter adds some characters used as keyword:

* Ranges must be separated by a pipe (``|``).
* ``<`` means ``inferior to``.
* ``#`` means ``inferior or equal to``.
* ``∞`` is the infinity symbol (U+221E). It can be prefixed with a minus (``-``).

Additional Placeholder Functions
--------------------------------

Expand Down