@@ -263,6 +263,69 @@ Usage of this string is the same as with variables and select::
263
263
}
264
264
}
265
265
266
+ Ranges
267
+ ~~~~~~
268
+
269
+ Sometimes, it can be handy to use ranges for a translation. The ``intl `` component provides a way to do this, using the ``choice `` function.
270
+
271
+ .. configuration-block ::
272
+
273
+ .. code-block :: yaml
274
+
275
+ # translations/messages+intl-icu.en.yaml
276
+ balance_account : >-
277
+ {balance, choice,
278
+ -∞ < Oops! I'm down |
279
+ 0 < I still have money |
280
+ 1000 # I have a lot of money !
281
+ }
282
+
283
+ .. code-block :: xml
284
+
285
+ <!-- translations/messages+intl-icu.en.xlf -->
286
+ <?xml version =" 1.0" ?>
287
+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
288
+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
289
+ <body >
290
+ <trans-unit id =" balance_account" >
291
+ <source >balance_account</source >
292
+ <target >{balance, choice, -∞ < Oops! I'm down | 0 < I still have money | 1000 # I have a lot of money !}</target >
293
+ </trans-unit >
294
+ </body >
295
+ </file >
296
+ </xliff >
297
+
298
+ .. code-block :: php
299
+
300
+ // translations/messages+intl-icu.en.php
301
+ return [
302
+ 'balance_account' => '{balance, choice,
303
+ -∞ < Oops! I\'m down |
304
+ 0 < I still have money |
305
+ 1000 # I have a lot of money!
306
+ }',
307
+ ];
308
+
309
+ Here are some examples of what this ``choice `` formatter will render::
310
+
311
+ // prints "Oops! I'm down"
312
+ echo $translator->trans('balance_account', ['balance' => -10]);
313
+ echo $translator->trans('balance_account', ['balance' => 0]);
314
+
315
+ // prints "I still have money"
316
+ echo $translator->trans('balance_account', ['balance' => 10]);
317
+ echo $translator->trans('balance_account', ['balance' => 999]);
318
+
319
+ // prints "I have a lot of money!"
320
+ echo $translator->trans('balance_account', ['balance' => 1000]);
321
+
322
+ This formatter adds some characters used as keyword:
323
+
324
+ * Ranges must be separated by a pipe (``| ``).
325
+ * ``< `` means ``inferior to ``.
326
+ * ``# `` means ``inferior or equal to ``.
327
+ * ``∞ `` is the infinity symbol (U+221E). It can be prefixed with a minus (``- ``).
328
+
266
329
Additional Placeholder Functions
267
330
--------------------------------
268
331
0 commit comments