Novis\Intl\ListFormat::join
Novis\Intl\ListFormat::join(
array<array<string>> $lists,
string $locale,
?{ type?: Type, width?: Width } $options = null,
): array<string>Joins the items of a list into one string, with the words a language uses, such as "Shop, Blog, and Wiki".
ListFormat::join takes a list of lists and returns one string for each list, in the same order. The locale decides the words and the commas. In English, the list ["Shop", "Blog", "Wiki"] gives "Shop, Blog, and Wiki". In German, it gives "Shop, Blog und Wiki". A list with one item gives that item, and an empty list gives "".
The options change the joining word. type: ListFormat::Type::Or joins with "or", as in "Shop, Blog, or Wiki". type: ListFormat::Type::Unit joins measurements, such as "3 kg, 200 g", with no "and". The default is ListFormat::Type::And. width: Width::Short and Width::Narrow use shorter forms where the language has them. In English, Width::Short gives "Shop, Blog, & Wiki".
ListFormat::join throws a LogicError when the locale tag is not valid.
Good to know: the method joins the strings as they are. It does not translate or change the items.
Parameters
$listsarray<array<string>>$localestring$options?{ type?: Type, width?: Width }default null
Return value
Returns array<string>.
Examples
Name the items in a cart
<?nvs// Shows how to write the items of a list as one sentence, in English and in German.use Novis\Intl\ListFormat;
array<array<string>> $carts = [['Shop', 'Blog', 'Wiki'], ['Shop', 'Blog'], ['Shop']];
// Each list becomes one string. This prints Shop, Blog, and Wiki, then Shop and Blog, then Shop.foreach (ListFormat::join($carts, 'en') as string $line) { echo $line, "\n";}
// German has no comma before "und". This prints Shop, Blog und Wiki.echo ListFormat::join([$carts[0]], 'de')[0], "\n";Shop, Blog, and WikiShop and BlogShopShop, Blog und WikiOffer a choice
<?nvs// Shows how to join a list with "or", for a sentence that offers a choice.use Novis\Intl\ListFormat;
array<string> $sizes = ['small', 'medium', 'large'];
// `ListFormat::Type::Or` joins with the word for "or". This prints small, medium, or large.echo 'Choose ', ListFormat::join([$sizes], 'en', { type: ListFormat::Type::Or })[0], ".\n";
// In German, this prints small, medium oder large.echo ListFormat::join([$sizes], 'de', { type: ListFormat::Type::Or })[0], "\n";Choose small, medium, or large.small, medium oder largeMeasurements and short forms
<?nvs// Shows how to join measurements, and how to use the shorter forms of a list.use Novis\Intl\ListFormat;use Novis\Intl\Width;
// `ListFormat::Type::Unit` joins parts of one measurement, with no "and". This prints 3 kg, 200 g.echo ListFormat::join([['3 kg', '200 g']], 'en', { type: ListFormat::Type::Unit })[0], "\n";
// `Width::Narrow` leaves out the commas too. This prints 1 h 5 min 3 s.echo ListFormat::join([['1 h', '5 min', '3 s']], 'en', { type: ListFormat::Type::Unit, width: Width::Narrow })[0], "\n";
// `Width::Short` uses "&" in English. This prints Shop, Blog, & Wiki.echo ListFormat::join([['Shop', 'Blog', 'Wiki']], 'en', { width: Width::Short })[0], "\n";3 kg, 200 g1 h 5 min 3 sShop, Blog, & Wiki