Skip to main content
Version: 5.x

Runtime Utilities

Bundled with fbt come a few useful utilities for constructing strings.

intlList(...)​

\fbt\intlList() creates fbt instances with selectable conjunctions given an array.

As an example

$people = ['Adam', 'Becky', fbt('4 others', 'last item')];
\fbt\intlList($people, 'and', 'comma');
// Adam, Becky and 4 others

produces the fbt

<fbt
desc="A list of items of various types separated by commas, for example: &quot;San Francisco, London, Tokyo&quot;. {previous items} and {following items} are themselves lists that contain one or more items.">
<fbt:param name="previous items">{$output}</fbt:param>,
<fbt:param name="following items">{$items[$i]}</fbt:param>
</fbt>

recursively combining fbts. Note that genders are not used in this fbt:param instances, so they default to UNKNOWN

The conjunctions are and (default), or and none, the delimiters are comma (default), semicolon and bullet (also available as IntlList::CONJUNCTIONS and IntlList::DELIMITERS):

\fbt\intlList(['San Francisco', 'London', 'Tokyo'], 'none', 'bullet');
// San Francisco • London • Tokyo

Pass ['serialComma' => true] to use a serial (Oxford) comma with the comma delimiter and the and / or conjunctions (lists of three or more items):

\fbt\intlList(['Tokyo', 'London', 'Vienna'], 'and', 'comma', ['serialComma' => true]);
// Tokyo, London, and Vienna

formatNumber​

formatNumber formats numbers according to the viewer's locale:

use fbt\Runtime\Shared\formatNumber;

formatNumber::formatNumber(1234.5, 2); // "1234.50"
formatNumber::withThousandDelimiters(1234.5); // "1,234.5"
formatNumber::withMaxLimit(1500, 1000); // fbs "1,000+"
formatNumber::withMinLimit(3, 10); // fbs "&lt;10" (HTML-escaped when rendered)

IntlGender​

IntlGender maps genders to Gender::GENDER_CONST values usable by fbt:

use fbt\Lib\DisplayGenderConst;
use fbt\Runtime\Shared\IntlGender;

IntlGender::fromMultiple([$gender]); // $gender
IntlGender::fromMultiple([$gender1, $gender2]); // Gender::GENDER_CONST['UNKNOWN_PLURAL']
IntlGender::fromDisplayGender(DisplayGenderConst::FEMALE); // Gender::GENDER_CONST['FEMALE_SINGULAR']

intlNumUtils and intlSummarizeNumber​

There are a few utilities in both intlNumUtils and intlSummarizeNumber that are documented in the source.

In fact fbt::param and fbt::plural default to displaying numbers using intlNumUtils::formatNumberWithThousandDelimiters. You can override this behavior in fbt:param by setting the number option and using your own string in the replacement.

You can override this in fbt::plural by providing the value option.

createElement​

We use this function internally to generate HTML for FBT.

\fbt\createElement('div', 'content', ['id' => 'container']);