How to use I18n::form // pluralize

In the blueprint section of projects I would like to render below the title an information of how many images that project has. Therefore i would like to use a pluralize function for image. I found that kirby has the I18n::form function to do exactly that, but I don’t know how to use it…

info: |
  {{ page.images.count }} {{ I18n::form(page.images.count, "image") }}

You could create a page method that uses I18n::form internally and use this page method in your query.

ok, I ended up adding a model function like this:


class ProjektPage extends Page
{
	public function images_count() {
		$count = $this->images()->count();
		if (($count == 0) || ($count >= 2)) {
			return $count . " Images";
		} else if ($count == 1) {
			return $count . " Image";
		}
	}
}

Also you can check out I18n::translateCount() method.

hmmm, ok interesting. But I am not sure if that is that what I was looking for.
Rails has this useful function pluralize which takes a count, and a string (for the singular word), optional a string for a plural version for the word and returns the right word for the corresponding count.
https://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize

I thought maybe kirby has such a useful function as well. If you have any Idea, where to add such a function, I might try to submit it as a pull request, if the kirby team is open for that?

The equivalent php function would be:

<?php
echo  ngettext('image', 'images', 1); // image
echo  ngettext('image', 'images', 3); // images

I think I18n::translateCount() is intended to work with templates in translation strings . That’s not what you want.

3 Likes

thanks, so I ended up shortening the code like that:

public function images_count() {
	$count = $this->images()->count();
	return $count . ngettext(' Image', ' Images', $count);
}